Skip to content

Commit

Permalink
Merge pull request #33 from kpetremann/dry_run_mock_exclusion
Browse files Browse the repository at this point in the history
Dry run mock exclusion
  • Loading branch information
kpetremann authored Jun 27, 2023
2 parents cf5c1b5 + e25b127 commit 1c8ea7f
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 32 deletions.
22 changes: 16 additions & 6 deletions cmd/salt-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func main() {
"Apply filter on functions to monitor, separated by a comma")
healthStatesFilters := flag.String("health-states-filter", "highstate",
"Apply filter on states to monitor, separated by a comma")
ignoreTest := flag.Bool("ignore-test", false, "ignore test=True events")
ignoreMock := flag.Bool("ignore-mock", false, "ignore mock=True events")
logLevel := flag.String("log-level", "info", "log level (debug, info, warn, error, fatal, panic, disabled)")
flag.Parse()

Expand All @@ -66,15 +68,23 @@ func main() {
log.Info().Str("Commit", commit).Send()
log.Info().Str("Build time", date).Send()

var metricsConfig metrics.MetricsConfig
metricsConfig.HealthMinions = *healthMinions
metricsConfig.HealthFunctionsFilters = strings.Split(*healthFunctionsFilters, ",")
metricsConfig.HealthStatesFilters = strings.Split(*healthStatesFilters, ",")
metricsConfig := metrics.MetricsConfig{
HealthMinions: *healthMinions,
HealthFunctionsFilters: strings.Split(*healthFunctionsFilters, ","),
HealthStatesFilters: strings.Split(*healthStatesFilters, ","),
IgnoreTest: *ignoreTest,
IgnoreMock: *ignoreMock,
}

if metricsConfig.HealthMinions {
log.Info().Msg("health-minions: metrics are enabled")
log.Info().Msg("health-minions: functions filters: " + *healthFunctionsFilters)
log.Info().Msg("health-minions: states filters: " + *healthStatesFilters)
log.Info().Msgf("health-minions: functions filters: %s", *healthFunctionsFilters)
log.Info().Msgf("health-minions: states filters: %s", *healthStatesFilters)
}

if metricsConfig.IgnoreTest {
log.Info().Msg("test=True events will be ignored")
log.Info().Msg("mock=True events will be ignored")
}

listenSocket := fmt.Sprint(*listenAddress, ":", *listenPort)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/charmbracelet/bubbletea v0.24.0
github.com/charmbracelet/lipgloss v0.7.1
github.com/google/go-cmp v0.5.9
github.com/k0kubun/pp/v3 v3.2.0
github.com/prometheus/client_golang v1.15.1
github.com/prometheus/client_model v0.3.0
github.com/prometheus/common v0.42.0
Expand All @@ -26,7 +27,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
Expand Down
3 changes: 3 additions & 0 deletions internal/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ type MetricsConfig struct {

// HealthFunctionsFilter permits to limit the number of state exposed
HealthStatesFilters []string

// Ignore test=True / mock=True events
IgnoreTest, IgnoreMock bool
}
8 changes: 3 additions & 5 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package metrics
import (
"context"
"strconv"
"time"

"github.com/kpetremann/salt-exporter/internal/filters"
"github.com/kpetremann/salt-exporter/pkg/event"
Expand Down Expand Up @@ -70,7 +69,9 @@ func ExposeMetrics(ctx context.Context, eventChan <-chan event.SaltEvent, metric
log.Info().Msg("stopping event listener")
return
case event := <-eventChan:
start := time.Now()
if metricsConfig.IgnoreTest && event.IsTest || metricsConfig.IgnoreMock && event.IsMock {
return
}

switch event.Type {
case "new":
Expand Down Expand Up @@ -121,9 +122,6 @@ func ExposeMetrics(ctx context.Context, eventChan <-chan event.SaltEvent, metric
}
lastFunctionStatus.WithLabelValues(event.Data.Id, event.Data.Fun, state).Set(boolToFloat64(success))
}

elapsed := time.Since(start)
log.Debug().Str("metric conversion took", elapsed.String()).Send()
}
}
}
2 changes: 1 addition & 1 deletion internal/tui/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func defaultKeyMap() *keyMap {
),
toggleJSONYAML: key.NewBinding(
key.WithKeys("m"),
key.WithHelp("m", "toggle JSON/YAML"),
key.WithHelp("m", "toggle JSON/YAML/parsed"),
),
}
}
Expand Down
32 changes: 21 additions & 11 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
teaViewport "github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/k0kubun/pp/v3"
"github.com/kpetremann/salt-exporter/pkg/event"
)

Expand All @@ -27,11 +28,12 @@ const (
type model struct {
eventList teaList.Model
itemsBuffer []teaList.Item
rawView teaViewport.Model
sideView teaViewport.Model
eventChan <-chan event.SaltEvent
hardFilter string
keys *keyMap
sideInfos string
sideTitle string
terminalWidth int
terminalHeight int
maxItems int
Expand Down Expand Up @@ -73,7 +75,7 @@ func NewModel(eventChan <-chan event.SaltEvent, maxItems int, filter string) mod

return model{
eventList: eventList,
rawView: rawView,
sideView: rawView,
keys: listKeys,
eventChan: eventChan,
hardFilter: filter,
Expand Down Expand Up @@ -189,7 +191,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, cmd)

m.updateSideInfos()
m.rawView, cmd = m.rawView.Update(msg)
m.sideView, cmd = m.sideView.Update(msg)
cmds = append(cmds, cmd)

if m.eventList.Index() > 0 {
Expand All @@ -205,25 +207,33 @@ func (m *model) updateSideInfos() {
if sel := m.eventList.SelectedItem(); sel != nil {
switch m.outputFormat {
case YAML:
m.sideTitle = "Raw event (YAML)"
m.sideInfos = sel.(item).eventYAML
if m.wordWrap {
m.sideInfos = strings.ReplaceAll(m.sideInfos, "\\n", " \\\n")
}
if info, err := Highlight(m.sideInfos, "yaml", theme); err != nil {
m.rawView.SetContent(m.sideInfos)
m.sideView.SetContent(m.sideInfos)
} else {
m.rawView.SetContent(info)
m.sideView.SetContent(info)
}
case JSON:
m.sideTitle = "Raw event (JSON)"
m.sideInfos = sel.(item).eventJSON
if m.wordWrap {
m.sideInfos = strings.ReplaceAll(m.sideInfos, "\\n", " \\\n")
}
if info, err := Highlight(m.sideInfos, "json", theme); err != nil {
m.rawView.SetContent(m.sideInfos)
m.sideView.SetContent(m.sideInfos)
} else {
m.rawView.SetContent(info)
m.sideView.SetContent(info)
}
case PARSED:
m.sideTitle = "Parsed event (Golang)"
eventLite := sel.(item).event
eventLite.RawBody = nil
m.sideInfos = pp.Sprint(eventLite)
m.sideView.SetContent(m.sideInfos)
}
}
}
Expand Down Expand Up @@ -267,15 +277,15 @@ func (m model) View() string {
*/

if m.sideInfos != "" {
rawTitle := rightPanelTitleStyle.Render("Raw details")
rawTitle := rightPanelTitleStyle.Render(m.sideTitle)

rightPanelStyle.Width(contentWidth)
rightPanelStyle.Height(contentHeight)

m.rawView.Width = contentWidth - rightPanelStyle.GetHorizontalFrameSize()
m.rawView.Height = contentHeight - lipgloss.Height(rawTitle) - rightPanelStyle.GetVerticalFrameSize()
m.sideView.Width = contentWidth - rightPanelStyle.GetHorizontalFrameSize()
m.sideView.Height = contentHeight - lipgloss.Height(rawTitle) - rightPanelStyle.GetVerticalFrameSize()

sideInfos := rightPanelStyle.Render(lipgloss.JoinVertical(0, rawTitle, m.rawView.View()))
sideInfos := rightPanelStyle.Render(lipgloss.JoinVertical(0, rawTitle, m.sideView.View()))
content = append(content, sideInfos)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/tui/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/alecthomas/chroma/quick"
)

const nbFormat = 2
const nbFormat = 3
const (
YAML format = iota
JSON
PARSED
)

func Highlight(content, extension, syntaxTheme string) (string, error) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type SaltEvent struct {
Data EventData
IsScheduleJob bool
RawBody []byte
IsTest bool
IsMock bool
}

// RawToJSON converts raw body to JSON
Expand Down
8 changes: 4 additions & 4 deletions pkg/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"net"
"time"

events "github.com/kpetremann/salt-exporter/pkg/event"
"github.com/kpetremann/salt-exporter/pkg/event"
"github.com/rs/zerolog/log"
"github.com/vmihailenco/msgpack/v5"
)

type eventParser interface {
Parse(message map[string]interface{}) (events.SaltEvent, error)
Parse(message map[string]interface{}) (event.SaltEvent, error)
}

const defaultIPCFilepath = "/var/run/salt/master/master_event_pub.ipc"
Expand All @@ -23,7 +23,7 @@ type EventListener struct {
ctx context.Context

// eventChan is the channel to send events to
eventChan chan events.SaltEvent
eventChan chan event.SaltEvent

// iPCFilepath is filepath to the salt-master event bus
iPCFilepath string
Expand Down Expand Up @@ -85,7 +85,7 @@ func (e *EventListener) Reconnect() {
// NewEventListener creates a new EventListener
//
// The events will be sent to eventChan.
func NewEventListener(ctx context.Context, eventParser eventParser, eventChan chan events.SaltEvent) *EventListener {
func NewEventListener(ctx context.Context, eventParser eventParser, eventChan chan event.SaltEvent) *EventListener {
e := EventListener{
ctx: ctx,
eventChan: eventChan,
Expand Down
Loading

0 comments on commit 1c8ea7f

Please sign in to comment.