-
Notifications
You must be signed in to change notification settings - Fork 18
/
metrics.go
55 lines (48 loc) · 1.59 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package system
import (
"context"
"fmt"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/circleci/ex/o11y"
"github.com/circleci/ex/worker"
)
type MetricProducer interface {
// MetricName The name for this group of metrics
//(Name might be cleaner, but is much more likely to conflict in implementations)
MetricName() string
// Gauges are instantaneous name value pairs
Gauges(context.Context) map[string]float64
}
func traceMetrics(ctx context.Context, producers []MetricProducer) {
metrics := o11y.FromContext(ctx).MetricsProvider()
for _, producer := range producers {
traceMetric(ctx, metrics, producer)
}
}
func traceMetric(ctx context.Context, provider o11y.MetricsProvider, producer MetricProducer) {
producerName := strings.Replace(producer.MetricName(), "-", "_", -1)
for f, v := range producer.Gauges(ctx) {
scopedField := fmt.Sprintf("gauge.%s.%s", producerName, f)
_ = provider.Gauge(scopedField, v, []string{}, 1)
}
}
// metrics reporter returns a function that is expected to be used in a call to errgroup.Go
// that func starts a worker that periodically calls and publishes the gauges from the producers.
func metricsReporter(ctx context.Context, mps []MetricProducer, gps []GaugeProducer) func() error {
return func() error {
cfg := worker.Config{
Name: "metric-loop",
MaxWorkTime: time.Second,
NoWorkBackOff: backoff.NewConstantBackOff(time.Second * 10),
WorkFunc: func(ctx context.Context) error {
traceMetrics(ctx, mps)
emitGauges(ctx, gps)
return worker.ErrShouldBackoff
},
}
worker.Run(ctx, cfg)
return nil
}
}