metrics.go 3.1 KB
Newer Older
Hamdi Allam's avatar
Hamdi Allam committed
1 2 3 4 5 6 7 8 9 10 11
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 (
Hamdi Allam's avatar
Hamdi Allam committed
12
	MetricsNamespace string = "op_indexer_etl"
Hamdi Allam's avatar
Hamdi Allam committed
13 14 15 16
)

type Metricer interface {
	RecordInterval() (done func(err error))
17
	RecordLatestHeight(height *big.Int)
Hamdi Allam's avatar
Hamdi Allam committed
18

19
	RecordEtlLatestHeight(height *big.Int)
Hamdi Allam's avatar
Hamdi Allam committed
20
	RecordIndexedHeaders(size int)
21
	RecordIndexedLog(contractAddress common.Address)
Hamdi Allam's avatar
Hamdi Allam committed
22 23 24
}

type etlMetrics struct {
Hamdi Allam's avatar
Hamdi Allam committed
25 26
	intervalTick     prometheus.Counter
	intervalDuration prometheus.Histogram
27
	intervalFailures prometheus.Counter
28
	latestHeight     prometheus.Gauge
Hamdi Allam's avatar
Hamdi Allam committed
29

30 31 32
	etlLatestHeight prometheus.Gauge
	indexedHeaders  prometheus.Counter
	indexedLogs     *prometheus.CounterVec
Hamdi Allam's avatar
Hamdi Allam committed
33 34
}

Hamdi Allam's avatar
Hamdi Allam committed
35
func NewMetrics(registry *prometheus.Registry, subsystem string) Metricer {
Hamdi Allam's avatar
Hamdi Allam committed
36 37
	factory := metrics.With(registry)
	return &etlMetrics{
Hamdi Allam's avatar
Hamdi Allam committed
38
		intervalTick: factory.NewCounter(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
39
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
40
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
41 42 43
			Name:      "intervals_total",
			Help:      "number of times the etl has run its extraction loop",
		}),
Hamdi Allam's avatar
Hamdi Allam committed
44
		intervalDuration: factory.NewHistogram(prometheus.HistogramOpts{
Hamdi Allam's avatar
Hamdi Allam committed
45
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
46
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
47 48 49
			Name:      "interval_seconds",
			Help:      "duration elapsed for during the processing loop",
		}),
50
		intervalFailures: factory.NewCounter(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
51
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
52
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
53
			Name:      "interval_failures_total",
54
			Help:      "number of times the etl encountered a failure during the processing loop",
Hamdi Allam's avatar
Hamdi Allam committed
55
		}),
Hamdi Allam's avatar
Hamdi Allam committed
56
		latestHeight: factory.NewGauge(prometheus.GaugeOpts{
57 58 59 60 61
			Namespace: MetricsNamespace,
			Subsystem: subsystem,
			Name:      "latest_height",
			Help:      "the latest height reported by the connected client",
		}),
62
		etlLatestHeight: factory.NewGauge(prometheus.GaugeOpts{
Hamdi Allam's avatar
Hamdi Allam committed
63
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
64
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
65
			Name:      "indexed_height",
66
			Help:      "the latest block height after a processing interval by the etl",
Hamdi Allam's avatar
Hamdi Allam committed
67
		}),
Hamdi Allam's avatar
Hamdi Allam committed
68
		indexedHeaders: factory.NewCounter(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
69
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
70
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
71 72 73
			Name:      "indexed_headers_total",
			Help:      "number of headers indexed by the etl",
		}),
74
		indexedLogs: factory.NewCounterVec(prometheus.CounterOpts{
Hamdi Allam's avatar
Hamdi Allam committed
75
			Namespace: MetricsNamespace,
Hamdi Allam's avatar
Hamdi Allam committed
76
			Subsystem: subsystem,
Hamdi Allam's avatar
Hamdi Allam committed
77 78
			Name:      "indexed_logs_total",
			Help:      "number of logs indexed by the etl",
79 80
		}, []string{
			"contract",
Hamdi Allam's avatar
Hamdi Allam committed
81 82 83 84
		}),
	}
}

Hamdi Allam's avatar
Hamdi Allam committed
85 86 87
func (m *etlMetrics) RecordInterval() func(error) {
	m.intervalTick.Inc()
	timer := prometheus.NewTimer(m.intervalDuration)
Hamdi Allam's avatar
Hamdi Allam committed
88 89
	return func(err error) {
		if err != nil {
90
			m.intervalFailures.Inc()
Hamdi Allam's avatar
Hamdi Allam committed
91 92 93 94 95
		}
		timer.ObserveDuration()
	}
}

96 97 98 99
func (m *etlMetrics) RecordLatestHeight(height *big.Int) {
	m.latestHeight.Set(float64(height.Uint64()))
}

100 101
func (m *etlMetrics) RecordEtlLatestHeight(height *big.Int) {
	m.etlLatestHeight.Set(float64(height.Uint64()))
Hamdi Allam's avatar
Hamdi Allam committed
102 103
}

Hamdi Allam's avatar
Hamdi Allam committed
104 105
func (m *etlMetrics) RecordIndexedHeaders(size int) {
	m.indexedHeaders.Add(float64(size))
Hamdi Allam's avatar
Hamdi Allam committed
106 107
}

108 109
func (m *etlMetrics) RecordIndexedLog(addr common.Address) {
	m.indexedLogs.WithLabelValues(addr.String()).Inc()
Hamdi Allam's avatar
Hamdi Allam committed
110
}