Skip to content

Commit

Permalink
metrics: add machine label to "cs_active_decisions"
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Feb 28, 2024
1 parent 8e9e091 commit 2983aad
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
15 changes: 10 additions & 5 deletions cmd/crowdsec/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var globalActiveDecisions = prometheus.NewGaugeVec(
Name: "cs_active_decisions",
Help: "Number of active decisions.",
},
[]string{"reason", "origin", "action"},
[]string{"reason", "origin", "action", "machine"},
)

var globalAlerts = prometheus.NewGaugeVec(
Expand Down Expand Up @@ -100,7 +100,7 @@ var globalPourHistogram = prometheus.NewHistogramVec(
[]string{"type", "source"},
)

func computeDynamicMetrics(next http.Handler, dbClient *database.Client) http.HandlerFunc {
func computeDynamicMetrics(next http.Handler, dbClient *database.Client, machine string) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//update cache metrics (stash)
cache.UpdateCacheMetrics()
Expand All @@ -126,7 +126,12 @@ func computeDynamicMetrics(next http.Handler, dbClient *database.Client) http.Ha
globalActiveDecisions.Reset()

for _, d := range decisions {
globalActiveDecisions.With(prometheus.Labels{"reason": d.Scenario, "origin": d.Origin, "action": d.Type}).Set(float64(d.Count))
globalActiveDecisions.With(prometheus.Labels{
"reason": d.Scenario,
"origin": d.Origin,
"action": d.Type,
"machine": machine,
}).Set(float64(d.Count))
}

globalAlerts.Reset()
Expand Down Expand Up @@ -181,7 +186,7 @@ func registerPrometheus(config *csconfig.PrometheusCfg) {
}
}

func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client, agentReady chan bool) {
func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client, agentReady chan bool, machine string) {
<-agentReady

if !config.Enabled {
Expand All @@ -190,7 +195,7 @@ func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client,

defer trace.CatchPanic("crowdsec/servePrometheus")

http.Handle("/metrics", computeDynamicMetrics(promhttp.Handler(), dbClient))
http.Handle("/metrics", computeDynamicMetrics(promhttp.Handler(), dbClient, machine))
log.Debugf("serving metrics after %s ms", time.Since(crowdsecT0))

if err := http.ListenAndServe(fmt.Sprintf("%s:%d", config.ListenAddr, config.ListenPort), nil); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/crowdsec/run_in_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func StartRunSvc() error {

registerPrometheus(cConfig.Prometheus)

go servePrometheus(cConfig.Prometheus, dbClient, agentReady)
machine := cConfig.API.Server.OnlineClient.GetWatcherID()

go servePrometheus(cConfig.Prometheus, dbClient, agentReady, machine)
} else {
// avoid leaking the channel
go func() {
Expand Down
5 changes: 4 additions & 1 deletion cmd/crowdsec/run_in_svc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ func WindowsRun() error {
}
}
registerPrometheus(cConfig.Prometheus)
go servePrometheus(cConfig.Prometheus, dbClient, agentReady)

machine := cConfig.API.Server.OnlineClient.GetWatcherID()

go servePrometheus(cConfig.Prometheus, dbClient, agentReady, machine)
}
return Serve(cConfig, agentReady)
}
10 changes: 10 additions & 0 deletions pkg/csconfig/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ func (o *OnlineApiClientCfg) Load() error {
return nil
}

func (o *OnlineApiClientCfg) GetWatcherID() string {
if o == nil {
return ""
}
if o.Credentials == nil {
return ""
}
return o.Credentials.Login

Check warning on line 122 in pkg/csconfig/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/api.go#L115-L122

Added lines #L115 - L122 were not covered by tests
}

func (l *LocalApiClientCfg) Load() error {
patcher := yamlpatch.NewPatcher(l.CredentialsFilePath, ".local")

Expand Down

0 comments on commit 2983aad

Please sign in to comment.