|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/prometheus/client_golang/prometheus" |
| 10 | + "go.opentelemetry.io/otel/attribute" |
| 11 | + "go.opentelemetry.io/otel/metric" |
| 12 | + sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 13 | + |
| 14 | + "github.com/smartcontractkit/chainlink-common/pkg/beholder" |
| 15 | +) |
| 16 | + |
| 17 | +// FunctionType represents the OCR plugin function being measured |
| 18 | +type FunctionType string |
| 19 | + |
| 20 | +const ( |
| 21 | + Query FunctionType = "query" |
| 22 | + Observation FunctionType = "observation" |
| 23 | + ValidateObservation FunctionType = "validateObservation" |
| 24 | + // OCR3 specific |
| 25 | + Outcome FunctionType = "outcome" |
| 26 | + // OCR3.1 specific |
| 27 | + ObservationQuorum FunctionType = "observationQuorum" |
| 28 | + StateTransition FunctionType = "stateTransition" |
| 29 | + Committed FunctionType = "committed" |
| 30 | + // Common |
| 31 | + Reports FunctionType = "reports" |
| 32 | + ShouldAccept FunctionType = "shouldAccept" |
| 33 | + ShouldTransmit FunctionType = "shouldTransmit" |
| 34 | +) |
| 35 | + |
| 36 | +// PluginMetrics holds OTEL metrics for OCR plugin instrumentation |
| 37 | +type PluginMetrics struct { |
| 38 | + plugin string |
| 39 | + configDigest string |
| 40 | + |
| 41 | + durations metric.Int64Histogram |
| 42 | + reportsGenerated metric.Int64Counter |
| 43 | + sizes metric.Int64Histogram |
| 44 | + status metric.Int64Gauge |
| 45 | +} |
| 46 | + |
| 47 | +// NewPluginMetrics creates metrics with the given prefix (e.g., "platform_ocr3_reporting_plugin" or "platform_ocr3_1_reporting_plugin") |
| 48 | +func NewPluginMetrics(metricPrefix, plugin, configDigest string) (*PluginMetrics, error) { |
| 49 | + durations, err := beholder.GetMeter().Int64Histogram(metricPrefix+"_duration_ms", metric.WithUnit("ms")) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to create duration histogram: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + reportsGenerated, err := beholder.GetMeter().Int64Counter(metricPrefix+"_reports_processed", metric.WithUnit("1")) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("failed to create reports counter: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + sizes, err := beholder.GetMeter().Int64Histogram(metricPrefix+"_data_sizes", metric.WithUnit("By")) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to create sizes histogram: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + status, err := beholder.GetMeter().Int64Gauge(metricPrefix + "_status") |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to create status gauge: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + return &PluginMetrics{ |
| 70 | + plugin: plugin, |
| 71 | + configDigest: configDigest, |
| 72 | + durations: durations, |
| 73 | + reportsGenerated: reportsGenerated, |
| 74 | + sizes: sizes, |
| 75 | + status: status, |
| 76 | + }, nil |
| 77 | +} |
| 78 | + |
| 79 | +// RecordDuration records the duration of a function execution |
| 80 | +func (m *PluginMetrics) RecordDuration(ctx context.Context, function FunctionType, d time.Duration, success bool) { |
| 81 | + m.durations.Record(ctx, d.Milliseconds(), metric.WithAttributes( |
| 82 | + attribute.String("plugin", m.plugin), |
| 83 | + attribute.String("function", string(function)), |
| 84 | + attribute.String("success", strconv.FormatBool(success)), |
| 85 | + attribute.String("configDigest", m.configDigest), |
| 86 | + )) |
| 87 | +} |
| 88 | + |
| 89 | +// TrackReports increments the reports processed counter |
| 90 | +func (m *PluginMetrics) TrackReports(ctx context.Context, function FunctionType, count int, success bool) { |
| 91 | + m.reportsGenerated.Add(ctx, int64(count), metric.WithAttributes( |
| 92 | + attribute.String("plugin", m.plugin), |
| 93 | + attribute.String("function", string(function)), |
| 94 | + attribute.String("success", strconv.FormatBool(success)), |
| 95 | + attribute.String("configDigest", m.configDigest), |
| 96 | + )) |
| 97 | +} |
| 98 | + |
| 99 | +// TrackSize records the size of data produced |
| 100 | +func (m *PluginMetrics) TrackSize(ctx context.Context, function FunctionType, size int) { |
| 101 | + m.sizes.Record(ctx, int64(size), metric.WithAttributes( |
| 102 | + attribute.String("plugin", m.plugin), |
| 103 | + attribute.String("function", string(function)), |
| 104 | + attribute.String("configDigest", m.configDigest), |
| 105 | + )) |
| 106 | +} |
| 107 | + |
| 108 | +// UpdateStatus updates the plugin status gauge (1 = up, 0 = down) |
| 109 | +func (m *PluginMetrics) UpdateStatus(ctx context.Context, up bool) { |
| 110 | + val := int64(0) |
| 111 | + if up { |
| 112 | + val = 1 |
| 113 | + } |
| 114 | + m.status.Record(ctx, val, metric.WithAttributes( |
| 115 | + attribute.String("plugin", m.plugin), |
| 116 | + attribute.String("configDigest", m.configDigest), |
| 117 | + )) |
| 118 | +} |
| 119 | + |
| 120 | +// MetricViews returns histogram bucket definitions for the given metric prefix. |
| 121 | +// Note: due to the OTEL specification, all histogram buckets must be defined when the beholder client is created. |
| 122 | +func MetricViews(metricPrefix string) []sdkmetric.View { |
| 123 | + return []sdkmetric.View{ |
| 124 | + sdkmetric.NewView( |
| 125 | + sdkmetric.Instrument{Name: metricPrefix + "_duration_ms"}, |
| 126 | + sdkmetric.Stream{Aggregation: sdkmetric.AggregationExplicitBucketHistogram{ |
| 127 | + // 5, 10, 20, 40, 80, 160, 320, 640, 1280, 2560, 5120, 10240, 20480, 40960 |
| 128 | + Boundaries: prometheus.ExponentialBuckets(5, 2, 14), |
| 129 | + }}, |
| 130 | + ), |
| 131 | + sdkmetric.NewView( |
| 132 | + sdkmetric.Instrument{Name: metricPrefix + "_data_sizes"}, |
| 133 | + sdkmetric.Stream{Aggregation: sdkmetric.AggregationExplicitBucketHistogram{ |
| 134 | + // 1KB, 2KB, 4KB, 8KB, 16KB, 32KB, 64KB, 128KB, 256KB, 512KB, 1024KB, 2048KB, 4096KB, 8192KB |
| 135 | + Boundaries: prometheus.ExponentialBuckets(1024, 2, 14), |
| 136 | + }}, |
| 137 | + ), |
| 138 | + } |
| 139 | +} |
0 commit comments