metrics.go 2.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
package metrics

import (
	"context"

	"github.com/ethereum-optimism/optimism/op-node/eth"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/ethereum/go-ethereum/log"
	"github.com/prometheus/client_golang/prometheus"

	opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
14
	txmetrics "github.com/ethereum-optimism/optimism/op-service/txmgr/metrics"
15 16 17 18 19 20 21 22 23 24 25
)

const Namespace = "op_proposer"

type Metricer interface {
	RecordInfo(version string)
	RecordUp()

	// Records all L1 and L2 block events
	opmetrics.RefMetricer

26 27 28
	// Record Tx metrics
	txmetrics.TxMetricer

29 30 31 32 33 34 35 36 37
	RecordL2BlocksProposed(l2ref eth.L2BlockRef)
}

type Metrics struct {
	ns       string
	registry *prometheus.Registry
	factory  opmetrics.Factory

	opmetrics.RefMetrics
38
	txmetrics.TxMetrics
39

40 41
	info prometheus.GaugeVec
	up   prometheus.Gauge
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
}

var _ Metricer = (*Metrics)(nil)

func NewMetrics(procName string) *Metrics {
	if procName == "" {
		procName = "default"
	}
	ns := Namespace + "_" + procName

	registry := opmetrics.NewRegistry()
	factory := opmetrics.With(registry)

	return &Metrics{
		ns:       ns,
		registry: registry,
		factory:  factory,

		RefMetrics: opmetrics.MakeRefMetrics(ns, factory),
61
		TxMetrics:  txmetrics.MakeTxMetrics(ns, factory),
62

63
		info: *factory.NewGaugeVec(prometheus.GaugeOpts{
64 65 66 67 68 69
			Namespace: ns,
			Name:      "info",
			Help:      "Pseudo-metric tracking version and config info",
		}, []string{
			"version",
		}),
70
		up: factory.NewGauge(prometheus.GaugeOpts{
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
			Namespace: ns,
			Name:      "up",
			Help:      "1 if the op-proposer has finished starting up",
		}),
	}
}

func (m *Metrics) Serve(ctx context.Context, host string, port int) error {
	return opmetrics.ListenAndServe(ctx, m.registry, host, port)
}

func (m *Metrics) StartBalanceMetrics(ctx context.Context,
	l log.Logger, client *ethclient.Client, account common.Address) {
	opmetrics.LaunchBalanceMetrics(ctx, l, m.registry, m.ns, client, account)
}

// RecordInfo sets a pseudo-metric that contains versioning and
// config info for the op-proposer.
func (m *Metrics) RecordInfo(version string) {
90
	m.info.WithLabelValues(version).Set(1)
91 92 93 94 95
}

// RecordUp sets the up metric to 1.
func (m *Metrics) RecordUp() {
	prometheus.MustRegister()
96
	m.up.Set(1)
97 98 99 100 101 102 103 104 105 106
}

const (
	BlockProposed = "proposed"
)

// RecordL2BlocksProposed should be called when new L2 block is proposed
func (m *Metrics) RecordL2BlocksProposed(l2ref eth.L2BlockRef) {
	m.RecordL2Ref(BlockProposed, l2ref)
}
107 108 109 110

func (m *Metrics) Document() []opmetrics.DocumentedMetric {
	return m.factory.Document()
}