metrics.go 3.14 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 20 21

	// Indexed Batches
	RecordIndexedLatestHeight(height *big.Int)
	RecordIndexedHeaders(size int)
22
	RecordIndexedLog(contractAddress common.Address)
Hamdi Allam's avatar
Hamdi Allam committed
23 24 25
}

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

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

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

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

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

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

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

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