Skip to content

Commit

Permalink
Added pint_rule_file_owner metric
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Dec 7, 2022
1 parent 1eb1303 commit c61328b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
4 changes: 4 additions & 0 deletions cmd/pint/tests/0042_watch_metrics.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ pint_problem{filename="rules/unknown.yml",kind="recording",name="broken",owner="
# HELP pint_problems Total number of problems reported by pint
# TYPE pint_problems gauge
pint_problems
# HELP pint_rule_file_owner This is a boolean metric that describes who is the configured owner for given rule file
# TYPE pint_rule_file_owner gauge
pint_rule_file_owner{filename="rules/alice.yml",owner="alice"}
pint_rule_file_owner{filename="rules/bob.yml",owner="bob"}
# HELP pint_rules_parsed_total Total number of rules parsed since startup
# TYPE pint_rules_parsed_total counter
pint_rules_parsed_total{kind="alerting"}
Expand Down
3 changes: 3 additions & 0 deletions cmd/pint/tests/0054_watch_metrics_prometheus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ pint_prometheus_query_errors_total{endpoint="/api/v1/status/config",name="prom1"
pint_prometheus_query_errors_total{endpoint="/api/v1/status/config",name="prom2",reason="connection/error"}
pint_prometheus_query_errors_total{endpoint="/api/v1/status/flags",name="prom1",reason="api/client_error"}
pint_prometheus_query_errors_total{endpoint="/api/v1/status/flags",name="prom2",reason="connection/error"}
# HELP pint_rule_file_owner This is a boolean metric that describes who is the configured owner for given rule file
# TYPE pint_rule_file_owner gauge
pint_rule_file_owner{filename="rules/2.yml",owner="bob and alice"}
# HELP pint_rules_parsed_total Total number of rules parsed since startup
# TYPE pint_rules_parsed_total counter
pint_rules_parsed_total{kind="alerting"}
Expand Down
44 changes: 33 additions & 11 deletions cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,23 @@ func startTimer(ctx context.Context, cfg config.Config, workers int, interval ti
}

type problemCollector struct {
lock sync.Mutex
cfg config.Config
paths []string
summary *reporter.Summary
problem *prometheus.Desc
problems *prometheus.Desc
minSeverity checks.Severity
maxProblems int
lock sync.Mutex
cfg config.Config
paths []string
fileOwners map[string]string
summary *reporter.Summary
problem *prometheus.Desc
problems *prometheus.Desc
fileOwnersMetric *prometheus.Desc
minSeverity checks.Severity
maxProblems int
}

func newProblemCollector(cfg config.Config, paths []string, minSeverity checks.Severity, maxProblems int) *problemCollector {
return &problemCollector{
cfg: cfg,
paths: paths,
cfg: cfg,
paths: paths,
fileOwners: map[string]string{},
problem: prometheus.NewDesc(
"pint_problem",
"Prometheus rule problem reported by pint",
Expand All @@ -232,6 +235,12 @@ func newProblemCollector(cfg config.Config, paths []string, minSeverity checks.S
[]string{},
prometheus.Labels{},
),
fileOwnersMetric: prometheus.NewDesc(
"pint_rule_file_owner",
"This is a boolean metric that describes who is the configured owner for given rule file",
[]string{"filename", "owner"},
prometheus.Labels{},
),
minSeverity: minSeverity,
maxProblems: maxProblems,
}
Expand All @@ -248,8 +257,17 @@ func (c *problemCollector) scan(ctx context.Context, workers int) error {
s := checkRules(ctx, workers, c.cfg, entries)

c.lock.Lock()
defer c.lock.Unlock()

c.summary = &s
c.lock.Unlock()

fileOwners := map[string]string{}
for _, entry := range entries {
if entry.Owner != "" {
fileOwners[entry.ReportedPath] = entry.Owner
}
}
c.fileOwners = fileOwners

return nil
}
Expand All @@ -266,6 +284,10 @@ func (c *problemCollector) Collect(ch chan<- prometheus.Metric) {
return
}

for filename, owner := range c.fileOwners {
ch <- prometheus.MustNewConstMetric(c.fileOwnersMetric, prometheus.GaugeValue, 1, filename, owner)
}

done := map[string]prometheus.Metric{}
keys := []string{}

Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.37.0

### Added

- Added `pint_rule_file_owner` metric.

## v0.36.0

### Added
Expand Down

0 comments on commit c61328b

Please sign in to comment.