|
| 1 | +package block |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/go-kit/kit/metrics" |
| 5 | + "github.com/go-kit/kit/metrics/discard" |
| 6 | + "github.com/go-kit/kit/metrics/prometheus" |
| 7 | + stdprometheus "github.com/prometheus/client_golang/prometheus" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + // MetricsSubsystem is a subsystem shared by all metrics exposed by this |
| 12 | + // package. |
| 13 | + MetricsSubsystem = "sequencer" |
| 14 | +) |
| 15 | + |
| 16 | +// Metrics contains metrics exposed by this package. |
| 17 | +type Metrics struct { |
| 18 | + // Height of the chain. |
| 19 | + Height metrics.Gauge |
| 20 | + |
| 21 | + // Number of transactions. |
| 22 | + NumTxs metrics.Gauge |
| 23 | + // Size of the block. |
| 24 | + BlockSizeBytes metrics.Gauge |
| 25 | + // Total number of transactions. |
| 26 | + TotalTxs metrics.Gauge |
| 27 | + // The latest block height. |
| 28 | + CommittedHeight metrics.Gauge `metrics_name:"latest_block_height"` |
| 29 | +} |
| 30 | + |
| 31 | +// PrometheusMetrics returns Metrics build using Prometheus client library. |
| 32 | +// Optionally, labels can be provided along with their values ("foo", |
| 33 | +// "fooValue"). |
| 34 | +func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { |
| 35 | + labels := []string{} |
| 36 | + for i := 0; i < len(labelsAndValues); i += 2 { |
| 37 | + labels = append(labels, labelsAndValues[i]) |
| 38 | + } |
| 39 | + return &Metrics{ |
| 40 | + Height: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ |
| 41 | + Namespace: namespace, |
| 42 | + Subsystem: MetricsSubsystem, |
| 43 | + Name: "height", |
| 44 | + Help: "Height of the chain.", |
| 45 | + }, labels).With(labelsAndValues...), |
| 46 | + NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ |
| 47 | + Namespace: namespace, |
| 48 | + Subsystem: MetricsSubsystem, |
| 49 | + Name: "num_txs", |
| 50 | + Help: "Number of transactions.", |
| 51 | + }, labels).With(labelsAndValues...), |
| 52 | + BlockSizeBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ |
| 53 | + Namespace: namespace, |
| 54 | + Subsystem: MetricsSubsystem, |
| 55 | + Name: "block_size_bytes", |
| 56 | + Help: "Size of the block.", |
| 57 | + }, labels).With(labelsAndValues...), |
| 58 | + TotalTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ |
| 59 | + Namespace: namespace, |
| 60 | + Subsystem: MetricsSubsystem, |
| 61 | + Name: "total_txs", |
| 62 | + Help: "Total number of transactions.", |
| 63 | + }, labels).With(labelsAndValues...), |
| 64 | + CommittedHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ |
| 65 | + Namespace: namespace, |
| 66 | + Subsystem: MetricsSubsystem, |
| 67 | + Name: "latest_block_height", |
| 68 | + Help: "The latest block height.", |
| 69 | + }, labels).With(labelsAndValues...), |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// NopMetrics returns no-op Metrics. |
| 74 | +func NopMetrics() *Metrics { |
| 75 | + return &Metrics{ |
| 76 | + Height: discard.NewGauge(), |
| 77 | + NumTxs: discard.NewGauge(), |
| 78 | + BlockSizeBytes: discard.NewGauge(), |
| 79 | + TotalTxs: discard.NewGauge(), |
| 80 | + CommittedHeight: discard.NewGauge(), |
| 81 | + } |
| 82 | +} |
0 commit comments