Skip to content

Add machine label to cs_active_decisions metric#4568

Open
kushiemoon-dev wants to merge 5 commits into
crowdsecurity:masterfrom
kushiemoon-dev:2784-machine-label-active-decisions
Open

Add machine label to cs_active_decisions metric#4568
kushiemoon-dev wants to merge 5 commits into
crowdsecurity:masterfrom
kushiemoon-dev:2784-machine-label-active-decisions

Conversation

@kushiemoon-dev

@kushiemoon-dev kushiemoon-dev commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds a machine label to the cs_active_decisions Prometheus GaugeVec, so each time series shows which enrolled machine owns the alert behind those active decisions.
  • Resolves the owning machine via Decision -> Alert -> Machine using the native alert_decisions FK column (no raw SQL join), folding rows that share (reason, origin, action, machine). Falls back to "N/A" for decisions whose alert has no owning machine.
  • Existing consumers (cscli metrics) are unaffected: the new label only splits existing rows, the summed totals per (reason, origin, action) stay identical.

Test plan

  • go build ./...
  • go test ./pkg/database/... (existing suite + 4 new regression tests: machine-owned alert, no-owning-machine fallback, folding across alerts owned by the same machine, splitting across different machines)
  • go vet ./pkg/metrics/... ./cmd/crowdsec/...

Related: #2784

Extend DecisionsByScenario struct with Machine and AlertDecisions fields.
Rewrite QueryDecisionCountByScenario to group by (scenario, origin, type,
machine) instead of (scenario, origin, type). Implement two helper functions:
resolveMachinesByAlertID to look up machine IDs for alert references, and
foldByMachine to re-aggregate results by machine. This preserves the DB query
cost profile (single GROUP BY + COUNT) while enabling per-machine metrics.

Closes crowdsecurity#2784
The AlertDecisions field was missing the sql:"alert_decisions" tag,
causing QueryDecisionCountByScenario to fail with column scan errors.
- Add "machine" to GlobalActiveDecisions label list in pkg/metrics/global.go
- Pass d.Machine value in cs_active_decisions metric write in cmd/crowdsec/metrics.go

This completes Task 2 of wiring the machine label through the metric definition
and its write site, following Task 1's addition of the Machine field to
DecisionsByScenario.
… folding

Covers QueryDecisionCountByScenario's machine resolution: owned-alert
resolution, "N/A" fallback for ownerless alerts, folding of same-machine
decisions across separate alerts, and correct splitting across distinct
machines (with a sum-of-counts invariant guarding against regressions in
existing per-scenario totals).
@github-actions

Copy link
Copy Markdown

@kushiemoon-dev: There are no 'kind' label on this PR. You need a 'kind' label to generate the release automatically.

  • /kind feature
  • /kind enhancement
  • /kind refactoring
  • /kind fix
  • /kind chore
  • /kind dependencies
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@github-actions

Copy link
Copy Markdown

@kushiemoon-dev: There are no area labels on this PR. You can add as many areas as you see fit.

  • /area agent
  • /area local-api
  • /area cscli
  • /area appsec
  • /area security
  • /area configuration
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@kushiemoon-dev

Copy link
Copy Markdown
Contributor Author

/kind feature

@kushiemoon-dev

Copy link
Copy Markdown
Contributor Author

/area agent

@kushiemoon-dev

Copy link
Copy Markdown
Contributor Author

/kind enhancement

@github-actions github-actions Bot added kind/enhancement New feature or request and removed kind/feature labels Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent kind/enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant