|
| 1 | +package dontime |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/smartcontractkit/chainlink-common/pkg/beholder" |
| 7 | + "go.opentelemetry.io/otel/attribute" |
| 8 | + "go.opentelemetry.io/otel/metric" |
| 9 | + |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + promDONTimeClockDrift = promauto.NewGaugeVec(prometheus.GaugeOpts{ |
| 16 | + Name: "don_time_clock_drift_ms", |
| 17 | + Help: "Drift between node local time and generated DON Time", |
| 18 | + }, []string{"oracleID"}) |
| 19 | +) |
| 20 | + |
| 21 | +type GenericDONTimeMetrics interface { |
| 22 | + RecordClockDrift(ctx context.Context, drift int64) |
| 23 | +} |
| 24 | + |
| 25 | +var _ GenericDONTimeMetrics = &donTimeMetrics{} |
| 26 | + |
| 27 | +type donTimeMetrics struct { |
| 28 | + oracleID string |
| 29 | + donTimeCount metric.Int64Counter |
| 30 | + clockDrift metric.Int64Gauge |
| 31 | +} |
| 32 | + |
| 33 | +func NewGenericDONTimeMetrics(oracleID string) (GenericDONTimeMetrics, error) { |
| 34 | + clockDrift, err := beholder.GetMeter().Int64Gauge("don_time_clock_drift_ms") |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("failed to register clock drift metric: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + return &donTimeMetrics{ |
| 40 | + oracleID: oracleID, |
| 41 | + clockDrift: clockDrift, |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (m *donTimeMetrics) RecordClockDrift(ctx context.Context, drift int64) { |
| 46 | + promDONTimeClockDrift.WithLabelValues(m.oracleID).Set(float64(drift)) |
| 47 | + m.clockDrift.Record(ctx, drift, metric.WithAttributes( |
| 48 | + attribute.String("oracleID", m.oracleID))) |
| 49 | +} |
0 commit comments