Skip to content

Commit 4b36fea

Browse files
scripts/metricsgen: add the initial version of metricsgen (tendermint#8479)
This pull requests adds a new tool, metricsgen, for generating Tendermint metrics constructors from `Metrics` struct definitions. This tool aims to reduce the amount of boilerplate required to add additional metrics to Tendermint. Its working is fairly simple, it parses the go ast, extracts field information, and uses this field information to execute a go template. This pull request also adds a proof-of-concept of the tool's output and working by using it to generate the [indexer metrics](https://github.com/tendermint/tendermint/pull/8479/files#diff-4b0c597b6fa05332a2f9a8e0ce079e360602942fae99dc5485f1edfe71c0a29e) using `//go:generate` directives and a simple `make` target. The next steps for this tool are documented in tendermint#8485 and tendermint#8486, which detail using the tool to generate the `metrics.md` documentation file and using the tool to migrate away from `go-kit`.
1 parent b52b8f2 commit 4b36fea

File tree

11 files changed

+809
-50
lines changed

11 files changed

+809
-50
lines changed

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ mockery:
251251
go generate -run="./scripts/mockery_generate.sh" ./...
252252
.PHONY: mockery
253253

254+
###############################################################################
255+
### Metrics ###
256+
###############################################################################
257+
258+
metrics: testdata-metrics
259+
go generate -run="scripts/metricsgen" ./...
260+
.PHONY: metrics
261+
262+
# By convention, the go tool ignores subdirectories of directories named
263+
# 'testdata'. This command invokes the generate command on the folder directly
264+
# to avoid this.
265+
testdata-metrics:
266+
ls ./scripts/metricsgen/testdata | xargs -I{} go generate -run="scripts/metricsgen" ./scripts/metricsgen/testdata/{}
267+
.PHONY: testdata-metrics
268+
254269
###############################################################################
255270
### Local testnet using docker ###
256271
###############################################################################

internal/state/indexer/metrics.gen.go

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/state/indexer/metrics.go

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package indexer
22

33
import (
44
"github.com/go-kit/kit/metrics"
5-
"github.com/go-kit/kit/metrics/discard"
6-
7-
prometheus "github.com/go-kit/kit/metrics/prometheus"
8-
stdprometheus "github.com/prometheus/client_golang/prometheus"
95
)
106

7+
//go:generate go run github.com/tendermint/tendermint/scripts/metricsgen -struct=Metrics
8+
119
// MetricsSubsystem is a the subsystem label for the indexer package.
1210
const MetricsSubsystem = "indexer"
1311

@@ -25,49 +23,3 @@ type Metrics struct {
2523
// Number of transactions indexed.
2624
TransactionsIndexed metrics.Counter
2725
}
28-
29-
// PrometheusMetrics returns Metrics build using Prometheus client library.
30-
// Optionally, labels can be provided along with their values ("foo",
31-
// "fooValue").
32-
func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
33-
labels := []string{}
34-
for i := 0; i < len(labelsAndValues); i += 2 {
35-
labels = append(labels, labelsAndValues[i])
36-
}
37-
return &Metrics{
38-
BlockEventsSeconds: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
39-
Namespace: namespace,
40-
Subsystem: MetricsSubsystem,
41-
Name: "block_events_seconds",
42-
Help: "Latency for indexing block events.",
43-
}, labels).With(labelsAndValues...),
44-
TxEventsSeconds: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
45-
Namespace: namespace,
46-
Subsystem: MetricsSubsystem,
47-
Name: "tx_events_seconds",
48-
Help: "Latency for indexing transaction events.",
49-
}, labels).With(labelsAndValues...),
50-
BlocksIndexed: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
51-
Namespace: namespace,
52-
Subsystem: MetricsSubsystem,
53-
Name: "blocks_indexed",
54-
Help: "Number of complete blocks indexed.",
55-
}, labels).With(labelsAndValues...),
56-
TransactionsIndexed: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
57-
Namespace: namespace,
58-
Subsystem: MetricsSubsystem,
59-
Name: "transactions_indexed",
60-
Help: "Number of transactions indexed.",
61-
}, labels).With(labelsAndValues...),
62-
}
63-
}
64-
65-
// NopMetrics returns an indexer metrics stub that discards all samples.
66-
func NopMetrics() *Metrics {
67-
return &Metrics{
68-
BlockEventsSeconds: discard.NewHistogram(),
69-
TxEventsSeconds: discard.NewHistogram(),
70-
BlocksIndexed: discard.NewCounter(),
71-
TransactionsIndexed: discard.NewCounter(),
72-
}
73-
}

0 commit comments

Comments
 (0)