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

import (
4
	"fmt"
5 6
	"strings"

7 8 9 10
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

11
type Base struct {
12
	// subsystemName stores the name that will prefix all metrics. This can be
13
	// used by drivers to further extend the core metrics and ensure they use the
14 15 16
	// same prefix.
	subsystemName string

17 18
	// balanceETH tracks the amount of ETH in the submitter's account.
	balanceETH prometheus.Gauge
19

20 21
	// batchSizeBytes tracks the size of batch submission transactions.
	batchSizeBytes prometheus.Summary
22

23
	// numElementsPerBatch tracks the number of L2 transactions in each batch
24
	// submission.
25
	numElementsPerBatch prometheus.Summary
26

27 28
	// submissionTimestamp tracks the time at which each batch was confirmed.
	submissionTimestamp prometheus.Gauge
29

30 31
	// submissionGasUsedWei tracks the amount of gas used to submit each batch.
	submissionGasUsedWei prometheus.Gauge
32

33 34
	// batchsSubmitted tracks the total number of successful batch submissions.
	batchesSubmitted prometheus.Counter
35

36 37
	// failedSubmissions tracks the total number of failed batch submissions.
	failedSubmissions prometheus.Counter
38

39
	// batchTxBuildTimeMs tracks the duration it takes to construct a batch
40
	// transaction.
41
	batchTxBuildTimeMs prometheus.Gauge
42

43
	// batchConfirmationTimeMs tracks the duration it takes to confirm a batch
44
	// transaction.
45
	batchConfirmationTimeMs prometheus.Gauge
46 47
}

48 49
func NewBase(serviceName, subServiceName string) *Base {
	subsystem := MakeSubsystemName(serviceName, subServiceName)
50
	return &Base{
51
		subsystemName: subsystem,
52
		balanceETH: promauto.NewGauge(prometheus.GaugeOpts{
53
			Name:      "balance_eth",
54 55 56
			Help:      "ETH balance of the batch submitter",
			Subsystem: subsystem,
		}),
57
		batchSizeBytes: promauto.NewSummary(prometheus.SummaryOpts{
58 59 60 61
			Name:       "batch_size_bytes",
			Help:       "Size of batches in bytes",
			Subsystem:  subsystem,
			Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
62
		}),
63
		numElementsPerBatch: promauto.NewSummary(prometheus.SummaryOpts{
64 65 66 67
			Name:       "num_elements_per_batch",
			Help:       "Number of elements in each batch",
			Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
			Subsystem:  subsystem,
68
		}),
69
		submissionTimestamp: promauto.NewGauge(prometheus.GaugeOpts{
70
			Name:      "submission_timestamp_ms",
71
			Help:      "Timestamp of last batch submitter submission",
72 73
			Subsystem: subsystem,
		}),
74
		submissionGasUsedWei: promauto.NewGauge(prometheus.GaugeOpts{
75
			Name:      "submission_gas_used_wei",
76 77 78
			Help:      "Gas used to submit each batch",
			Subsystem: subsystem,
		}),
79
		batchesSubmitted: promauto.NewCounter(prometheus.CounterOpts{
80
			Name:      "batches_submitted",
81 82 83
			Help:      "Count of batches submitted",
			Subsystem: subsystem,
		}),
84
		failedSubmissions: promauto.NewCounter(prometheus.CounterOpts{
85
			Name:      "failed_submissions",
86 87 88
			Help:      "Count of failed batch submissions",
			Subsystem: subsystem,
		}),
89
		batchTxBuildTimeMs: promauto.NewGauge(prometheus.GaugeOpts{
90
			Name:      "batch_tx_build_time_ms",
91 92 93
			Help:      "Time to construct batch transactions",
			Subsystem: subsystem,
		}),
94
		batchConfirmationTimeMs: promauto.NewGauge(prometheus.GaugeOpts{
95
			Name:      "batch_confirmation_time_ms",
96 97 98 99 100
			Help:      "Time to confirm batch transactions",
			Subsystem: subsystem,
		}),
	}
}
101

102 103 104 105 106
// SubsystemName returns the subsystem name for the metrics group.
func (b *Base) SubsystemName() string {
	return b.subsystemName
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
// BalanceETH tracks the amount of ETH in the submitter's account.
func (b *Base) BalanceETH() prometheus.Gauge {
	return b.balanceETH
}

// BatchSizeBytes tracks the size of batch submission transactions.
func (b *Base) BatchSizeBytes() prometheus.Summary {
	return b.batchSizeBytes
}

// NumElementsPerBatch tracks the number of L2 transactions in each batch
// submission.
func (b *Base) NumElementsPerBatch() prometheus.Summary {
	return b.numElementsPerBatch
}

// SubmissionTimestamp tracks the time at which each batch was confirmed.
func (b *Base) SubmissionTimestamp() prometheus.Gauge {
	return b.submissionTimestamp
}

// SubmissionGasUsedWei tracks the amount of gas used to submit each batch.
func (b *Base) SubmissionGasUsedWei() prometheus.Gauge {
	return b.submissionGasUsedWei
}

// BatchsSubmitted tracks the total number of successful batch submissions.
func (b *Base) BatchesSubmitted() prometheus.Counter {
	return b.batchesSubmitted
}

// FailedSubmissions tracks the total number of failed batch submissions.
func (b *Base) FailedSubmissions() prometheus.Counter {
	return b.failedSubmissions
}

// BatchTxBuildTimeMs tracks the duration it takes to construct a batch
// transaction.
func (b *Base) BatchTxBuildTimeMs() prometheus.Gauge {
	return b.batchTxBuildTimeMs
}

// BatchConfirmationTimeMs tracks the duration it takes to confirm a batch
// transaction.
func (b *Base) BatchConfirmationTimeMs() prometheus.Gauge {
	return b.batchConfirmationTimeMs
}

155 156 157 158 159
// MakeSubsystemName builds the subsystem name for a group of metrics, which
// prometheus will use to prefix all metrics in the group. If two non-empty
// strings are provided, they are joined with an underscore. If only one
// non-empty string is provided, that name will be used alone. Otherwise an
// empty string is returned after converting the characters to lower case.
160
//
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
// NOTE: This method panics if spaces are included in either string.
func MakeSubsystemName(serviceName string, subServiceName string) string {
	var subsystem string
	switch {
	case serviceName != "" && subServiceName != "":
		subsystem = fmt.Sprintf("%s_%s", serviceName, subServiceName)
	case serviceName != "":
		subsystem = serviceName
	default:
		subsystem = subServiceName
	}

	if strings.ContainsAny(subsystem, " ") {
		panic(fmt.Sprintf("metrics name \"%s\"cannot have spaces", subsystem))
	}

	return strings.ToLower(subsystem)
178
}