metrics.go 3.86 KB
Newer Older
Hamdi Allam's avatar
Hamdi Allam committed
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
package etl

import (
	"math/big"

	"github.com/ethereum-optimism/optimism/op-service/metrics"
	"github.com/ethereum/go-ethereum/common"
	"github.com/prometheus/client_golang/prometheus"
)

var (
	MetricsNamespace string = "etl"
)

type Metricer interface {
	RecordInterval() (done func(err error))

	// Batch Extraction
	RecordBatchLatestHeight(height *big.Int)
	RecordBatchHeaders(size int)
	RecordBatchLog(contractAddress common.Address)

	// Indexed Batches
	RecordIndexedLatestHeight(height *big.Int)
	RecordIndexedHeaders(size int)
	RecordIndexedLogs(size int)
}

type etlMetrics struct {
Hamdi Allam's avatar
Hamdi Allam committed
30 31
	intervalTick     prometheus.Counter
	intervalDuration prometheus.Histogram
Hamdi Allam's avatar
Hamdi Allam committed
32

Hamdi Allam's avatar
Hamdi Allam committed
33 34 35
	batchFailures     prometheus.Counter
	batchLatestHeight prometheus.Gauge
	batchHeaders      prometheus.Counter
Hamdi Allam's avatar
Hamdi Allam committed
36 37
	batchLogs         *prometheus.CounterVec

Hamdi Allam's avatar
Hamdi Allam committed
38 39 40
	indexedLatestHeight prometheus.Gauge
	indexedHeaders      prometheus.Counter
	indexedLogs         prometheus.Counter
Hamdi Allam's avatar
Hamdi Allam committed
41 42
}

Hamdi Allam's avatar
Hamdi Allam committed
43
func NewMetrics(registry *prometheus.Registry, subsystem string) Metricer {
Hamdi Allam's avatar
Hamdi Allam committed
44 45
	factory := metrics.With(registry)
	return &etlMetrics{
Hamdi Allam's avatar
Hamdi Allam committed
46
		intervalTick: factory.NewCounter(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
47
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
48
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
49 50 51
			Name:      "intervals_total",
			Help:      "number of times the etl has run its extraction loop",
		}),
Hamdi Allam's avatar
Hamdi Allam committed
52
		intervalDuration: factory.NewHistogram(prometheus.HistogramOpts{
Hamdi Allam's avatar
Hamdi Allam committed
53
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
54
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
55 56 57
			Name:      "interval_seconds",
			Help:      "duration elapsed for during the processing loop",
		}),
Hamdi Allam's avatar
Hamdi Allam committed
58
		batchFailures: factory.NewCounter(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
59
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
60
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
61 62 63
			Name:      "failures_total",
			Help:      "number of times the etl encountered a failure to extract a batch",
		}),
Hamdi Allam's avatar
Hamdi Allam committed
64
		batchLatestHeight: factory.NewGauge(prometheus.GaugeOpts{
Hamdi Allam's avatar
Hamdi Allam committed
65
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
66
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
67 68 69
			Name:      "height",
			Help:      "the latest block height observed by an etl interval",
		}),
Hamdi Allam's avatar
Hamdi Allam committed
70
		batchHeaders: factory.NewCounter(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
71
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
72
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
73 74 75 76 77
			Name:      "headers_total",
			Help:      "number of headers observed by the etl",
		}),
		batchLogs: factory.NewCounterVec(prometheus.CounterOpts{
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
78
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
79 80 81 82 83
			Name:      "logs_total",
			Help:      "number of logs observed by the etl",
		}, []string{
			"contract",
		}),
Hamdi Allam's avatar
Hamdi Allam committed
84
		indexedLatestHeight: factory.NewGauge(prometheus.GaugeOpts{
Hamdi Allam's avatar
Hamdi Allam committed
85
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
86
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
87 88 89
			Name:      "indexed_height",
			Help:      "the latest block height indexed into the database",
		}),
Hamdi Allam's avatar
Hamdi Allam committed
90
		indexedHeaders: factory.NewCounter(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
91
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
92
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
93 94 95
			Name:      "indexed_headers_total",
			Help:      "number of headers indexed by the etl",
		}),
Hamdi Allam's avatar
Hamdi Allam committed
96
		indexedLogs: factory.NewCounter(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
97
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
98
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
99 100 101 102 103 104
			Name:      "indexed_logs_total",
			Help:      "number of logs indexed by the etl",
		}),
	}
}

Hamdi Allam's avatar
Hamdi Allam committed
105 106 107
func (m *etlMetrics) RecordInterval() func(error) {
	m.intervalTick.Inc()
	timer := prometheus.NewTimer(m.intervalDuration)
Hamdi Allam's avatar
Hamdi Allam committed
108 109
	return func(err error) {
		if err != nil {
Hamdi Allam's avatar
Hamdi Allam committed
110
			m.batchFailures.Inc()
Hamdi Allam's avatar
Hamdi Allam committed
111 112 113 114 115 116
		}

		timer.ObserveDuration()
	}
}

Hamdi Allam's avatar
Hamdi Allam committed
117 118
func (m *etlMetrics) RecordBatchLatestHeight(height *big.Int) {
	m.batchLatestHeight.Set(float64(height.Uint64()))
Hamdi Allam's avatar
Hamdi Allam committed
119 120
}

Hamdi Allam's avatar
Hamdi Allam committed
121 122
func (m *etlMetrics) RecordBatchHeaders(size int) {
	m.batchHeaders.Add(float64(size))
Hamdi Allam's avatar
Hamdi Allam committed
123 124
}

Hamdi Allam's avatar
Hamdi Allam committed
125 126
func (m *etlMetrics) RecordBatchLog(contractAddress common.Address) {
	m.batchLogs.WithLabelValues(contractAddress.String()).Inc()
Hamdi Allam's avatar
Hamdi Allam committed
127 128
}

Hamdi Allam's avatar
Hamdi Allam committed
129 130
func (m *etlMetrics) RecordIndexedLatestHeight(height *big.Int) {
	m.indexedLatestHeight.Set(float64(height.Uint64()))
Hamdi Allam's avatar
Hamdi Allam committed
131 132
}

Hamdi Allam's avatar
Hamdi Allam committed
133 134
func (m *etlMetrics) RecordIndexedHeaders(size int) {
	m.indexedHeaders.Add(float64(size))
Hamdi Allam's avatar
Hamdi Allam committed
135 136
}

Hamdi Allam's avatar
Hamdi Allam committed
137 138
func (m *etlMetrics) RecordIndexedLogs(size int) {
	m.indexedLogs.Add(float64(size))
Hamdi Allam's avatar
Hamdi Allam committed
139
}