diff --git a/cmd/converge/prometheus.go b/cmd/converge/prometheus.go index cc69d44..9869529 100644 --- a/cmd/converge/prometheus.go +++ b/cmd/converge/prometheus.go @@ -159,12 +159,35 @@ func clientActive(client models.Client) { } func setupPrometheus(mux *http.ServeMux, notifications chan *models.State) { + // go routine that handles incoming events so we don't need to serialize access in some + // other way. + go func() { + for task := range prometheusChannel { + task() + } + }() + + // send an event periodically to update the agent and client durations so + // prometheus gets accurate values. + go func() { + timer := time.NewTicker(1 * time.Second) + for { + select { + case <-timer.C: + prometheusChannel <- updateDurations + } + } + }() + + // process incoming notifications from converge to update metrics. go func() { for { state := <-notifications updateMetrics(state) } }() + + // expose prometheus on a separate port. mux.Handle("/metrics", promhttp.Handler()) } @@ -191,24 +214,6 @@ func updateDurations() { } } -func init() { - go func() { - timer := time.NewTicker(1 * time.Second) - for { - select { - case <-timer.C: - prometheusChannel <- updateDurations - } - } - }() - - go func() { - for task := range prometheusChannel { - task() - } - }() -} - func updateMetricsImpl(state *models.State) { // This implemnetation has a small probability that the metric will be in a partially // initialized state. This is however unlikely. It would lead to in incorrect determination