Skip to content

Commit 8fd2a23

Browse files
committed
Add metrics
1 parent e5cda5f commit 8fd2a23

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

pkg/workflows/dontime/metrics.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

pkg/workflows/dontime/plugin.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
type Plugin struct {
2323
mu sync.RWMutex
2424

25+
metrics GenericDONTimeMetrics
2526
store *Store
2627
config ocr3types.ReportingPluginConfig
2728
offChainConfig *pb.Config
@@ -49,7 +50,13 @@ func NewPlugin(store *Store, config ocr3types.ReportingPluginConfig, lggr logger
4950
return nil, errors.New("execution removal time must be positive")
5051
}
5152

53+
metrics, err := NewGenericDONTimeMetrics(string(config.OracleID))
54+
if err != nil {
55+
return nil, fmt.Errorf("failed to register DON Time metrics: %w", err)
56+
}
57+
5258
return &Plugin{
59+
metrics: metrics,
5360
store: store,
5461
config: config,
5562
offChainConfig: offchainCfg,
@@ -168,7 +175,12 @@ func (p *Plugin) Outcome(_ context.Context, outctx ocr3types.OutcomeContext, _ t
168175
donTime = outcome.Timestamp + p.minTimeIncrease
169176
}
170177

171-
p.lggr.Infow("New DON Time", "donTime", donTime)
178+
driftMs := time.Now().UTC().UnixMilli() - donTime
179+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
180+
defer cancel()
181+
p.metrics.RecordClockDrift(ctx, driftMs)
182+
p.lggr.Infow("New DON Time", "donTime", donTime, "clockDriftMs", driftMs)
183+
172184
outcome.Timestamp = donTime
173185

174186
for id, numRequests := range observationCounts {

0 commit comments

Comments
 (0)