Skip to content

Commit

Permalink
fix: remove time.Sleep from metrics init
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Nov 27, 2024
1 parent 4319c92 commit bf39a99
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
v1 "github.com/flanksource/canary-checker/api/v1"
"github.com/flanksource/canary-checker/checks"
"github.com/flanksource/canary-checker/pkg/jobs/canary"
"github.com/flanksource/canary-checker/pkg/metrics"
"github.com/flanksource/canary-checker/pkg/prometheus"
"github.com/flanksource/canary-checker/pkg/runner"
"github.com/flanksource/canary-checker/pkg/telemetry"
Expand Down Expand Up @@ -76,6 +77,9 @@ var Root = &cobra.Command{
connection.HTTPConnection{URL: prometheus.PrometheusURL},
)
}

// Setup metrics since v1.AdditionalCheckMetricLabels is set now
metrics.SetupMetrics()
},
}

Expand Down
15 changes: 5 additions & 10 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
v1 "github.com/flanksource/canary-checker/api/v1"
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/canary-checker/pkg/runner"

"github.com/flanksource/duty/context"
"github.com/flanksource/duty/types"
cmap "github.com/orcaman/concurrent-map"
Expand All @@ -19,18 +20,12 @@ func init() {
CustomCounters = make(map[string]*prometheus.CounterVec)
CustomGauges = make(map[string]*prometheus.GaugeVec)
CustomHistograms = make(map[string]*prometheus.HistogramVec)

// Register the metrics with a delay because
// v1.AdditionalCheckMetricLabels is nil during init.
go func() {
time.Sleep(time.Second)

slices.Sort(v1.AdditionalCheckMetricLabels)
setupMetrics()
}()
}

func setupMetrics() {
// SetupMetrics is called from cmd/root PreRun since we take v1.AdditionalCheckMetricLabels from CLI
func SetupMetrics() {
slices.Sort(v1.AdditionalCheckMetricLabels)

RequestLatency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "canary_check_duration",
Expand Down

0 comments on commit bf39a99

Please sign in to comment.