Skip to content

Commit

Permalink
Merge pull request #1311 from flanksource/metric-panic
Browse files Browse the repository at this point in the history
fix: recover panic on metric creation
  • Loading branch information
moshloop authored Oct 11, 2023
2 parents 5d9711a + 56049c3 commit 21d920f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions checks/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import (

var collectorMap = make(map[string]prometheus.Collector)

func getOrAddPrometheusMetric(name, metricType string, labelNames []string) (prometheus.Collector, error) {
func getOrAddPrometheusMetric(name, metricType string, labelNames []string) (collector prometheus.Collector, e any) {
defer func() {
e = recover()
}()
key := name + metricType + strings.Join(labelNames, ",")
if collector, exists := collectorMap[key]; exists {
return collector, nil
}
var collector prometheus.Collector

switch metricType {
case "histogram":
collector = prometheus.NewHistogramVec(
Expand All @@ -35,7 +38,6 @@ func getOrAddPrometheusMetric(name, metricType string, labelNames []string) (pro
default:
return nil, fmt.Errorf("unknown metric type %s", metricType)
}

collectorMap[key] = collector
return collector, prometheus.Register(collector)
}
Expand Down Expand Up @@ -112,8 +114,9 @@ func exportCheckMetrics(ctx *context.Context, results pkg.Results) {
}

var collector prometheus.Collector
if collector, err = getOrAddPrometheusMetric(spec.Name, spec.Type, labelNames); err != nil {
r.ErrorMessage(err)
var e any
if collector, e = getOrAddPrometheusMetric(spec.Name, spec.Type, labelNames); e != nil {
r.ErrorMessage(fmt.Errorf("failed to create metric %s (%s) %s: %s", spec.Name, spec.Type, labelNames, e))
continue
}

Expand Down

0 comments on commit 21d920f

Please sign in to comment.