metrics.go 5.68 KB
Newer Older
1 2 3
package metrics

import (
4
	"net"
5
	"net/http"
6 7
	"strconv"
	"time"
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	"github.com/ethereum/go-ethereum/common"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

const metricsNamespace = "indexer"

type Metrics struct {
	SyncHeight *prometheus.GaugeVec

	DepositsCount *prometheus.CounterVec

	WithdrawalsCount *prometheus.CounterVec

	StateBatchesCount prometheus.Counter

	L1CatchingUp prometheus.Gauge

	L2CatchingUp prometheus.Gauge

	SyncPercent *prometheus.GaugeVec

	UpdateDuration *prometheus.SummaryVec

	CachedTokensCount *prometheus.CounterVec

36 37 38 39 40 41
	HTTPRequestsCount prometheus.Counter

	HTTPResponsesCount *prometheus.CounterVec

	HTTPRequestDurationSecs prometheus.Summary

42 43 44 45 46 47 48 49 50 51 52 53
	tokenAddrs map[string]string
}

func NewMetrics(monitoredTokens map[string]string) *Metrics {
	mts := make(map[string]string)
	mts["0x0000000000000000000000000000000000000000"] = "ETH"
	for addr, symbol := range monitoredTokens {
		mts[addr] = symbol
	}

	return &Metrics{
		SyncHeight: promauto.NewGaugeVec(prometheus.GaugeOpts{
Matthew Slipper's avatar
Matthew Slipper committed
54
			Name:      "sync_height",
55
			Help:      "The max height of the indexer's last batch of L1/L1 blocks.",
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
			Namespace: metricsNamespace,
		}, []string{
			"chain",
		}),

		DepositsCount: promauto.NewCounterVec(prometheus.CounterOpts{
			Name:      "deposits_count",
			Help:      "The number of deposits indexed.",
			Namespace: metricsNamespace,
		}, []string{
			"symbol",
		}),

		WithdrawalsCount: promauto.NewCounterVec(prometheus.CounterOpts{
			Name:      "withdrawals_count",
			Help:      "The number of withdrawals indexed.",
			Namespace: metricsNamespace,
		}, []string{
			"symbol",
		}),

Matthew Slipper's avatar
Matthew Slipper committed
77
		StateBatchesCount: promauto.NewCounter(prometheus.CounterOpts{
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
			Name:      "state_batches_count",
			Help:      "The number of state batches indexed.",
			Namespace: metricsNamespace,
		}),

		L1CatchingUp: promauto.NewGauge(prometheus.GaugeOpts{
			Name:      "l1_catching_up",
			Help:      "Whether or not L1 is far behind the chain tip.",
			Namespace: metricsNamespace,
		}),

		L2CatchingUp: promauto.NewGauge(prometheus.GaugeOpts{
			Name:      "l2_catching_up",
			Help:      "Whether or not L2 is far behind the chain tip.",
			Namespace: metricsNamespace,
		}),

		SyncPercent: promauto.NewGaugeVec(prometheus.GaugeOpts{
			Name:      "sync_percent",
			Help:      "Sync percentage for each chain.",
			Namespace: metricsNamespace,
		}, []string{
			"chain",
		}),

		UpdateDuration: promauto.NewSummaryVec(prometheus.SummaryOpts{
			Name:       "update_duration_seconds",
			Help:       "How long each update took.",
			Namespace:  metricsNamespace,
			Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.005, 0.99: 0.001},
		}, []string{
			"chain",
		}),

Matthew Slipper's avatar
Matthew Slipper committed
112
		CachedTokensCount: promauto.NewCounterVec(prometheus.CounterOpts{
113 114 115 116 117 118 119
			Name:      "cached_tokens_count",
			Help:      "How many tokens are in the cache",
			Namespace: metricsNamespace,
		}, []string{
			"chain",
		}),

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		HTTPRequestsCount: promauto.NewCounter(prometheus.CounterOpts{
			Name:      "http_requests_count",
			Help:      "How many HTTP requests this instance has seen",
			Namespace: metricsNamespace,
		}),

		HTTPResponsesCount: promauto.NewCounterVec(prometheus.CounterOpts{
			Name:      "http_responses_count",
			Help:      "How many HTTP responses this instance has served",
			Namespace: metricsNamespace,
		}, []string{
			"status_code",
		}),

		HTTPRequestDurationSecs: promauto.NewSummary(prometheus.SummaryOpts{
			Name:       "http_request_duration_secs",
			Help:       "How long each HTTP request took",
			Namespace:  metricsNamespace,
			Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.005, 0.99: 0.001},
		}),

141 142 143 144 145 146 147 148 149
		tokenAddrs: mts,
	}
}

func (m *Metrics) SetL1SyncHeight(height uint64) {
	m.SyncHeight.WithLabelValues("l1").Set(float64(height))
}

func (m *Metrics) SetL2SyncHeight(height uint64) {
Matthew Slipper's avatar
Matthew Slipper committed
150
	m.SyncHeight.WithLabelValues("l2").Set(float64(height))
151 152 153 154 155 156 157 158 159 160 161
}

func (m *Metrics) RecordDeposit(addr common.Address) {
	sym := m.tokenAddrs[addr.String()]
	if sym == "" {
		sym = "UNKNOWN"
	}

	m.DepositsCount.WithLabelValues(sym).Inc()
}

162
func (m *Metrics) RecordWithdrawal(addr common.Address) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	sym := m.tokenAddrs[addr.String()]
	if sym == "" {
		sym = "UNKNOWN"
	}

	m.WithdrawalsCount.WithLabelValues(sym).Inc()
}

func (m *Metrics) RecordStateBatches(count int) {
	m.StateBatchesCount.Add(float64(count))
}

func (m *Metrics) SetL1CatchingUp(state bool) {
	var catchingUp float64
	if state {
		catchingUp = 1
	}
	m.L1CatchingUp.Set(catchingUp)
}

func (m *Metrics) SetL2CatchingUp(state bool) {
	var catchingUp float64
	if state {
		catchingUp = 1
	}
	m.L2CatchingUp.Set(catchingUp)
}

func (m *Metrics) SetL1SyncPercent(height uint64, head uint64) {
	m.SyncPercent.WithLabelValues("l1").Set(float64(height) / float64(head))
}

func (m *Metrics) SetL2SyncPercent(height uint64, head uint64) {
	m.SyncPercent.WithLabelValues("l2").Set(float64(height) / float64(head))
}

func (m *Metrics) IncL1CachedTokensCount() {
	m.CachedTokensCount.WithLabelValues("l1").Inc()
}

func (m *Metrics) IncL2CachedTokensCount() {
	m.CachedTokensCount.WithLabelValues("l2").Inc()
}

207 208 209 210 211 212 213 214 215
func (m *Metrics) RecordHTTPRequest() {
	m.HTTPRequestsCount.Inc()
}

func (m *Metrics) RecordHTTPResponse(code int, dur time.Duration) {
	m.HTTPResponsesCount.WithLabelValues(strconv.Itoa(code)).Inc()
	m.HTTPRequestDurationSecs.Observe(float64(dur) / float64(time.Second))
}

216 217 218 219
func (m *Metrics) Serve(hostname string, port uint64) (*http.Server, error) {
	mux := http.NewServeMux()
	mux.Handle("/metrics", promhttp.Handler())
	srv := new(http.Server)
220
	srv.Addr = net.JoinHostPort(hostname, strconv.FormatUint(port, 10))
221 222 223 224
	srv.Handler = mux
	err := srv.ListenAndServe()
	return srv, err
}