From 00b99f0874bffae89b8d5e918dc9b0e952b4317e Mon Sep 17 00:00:00 2001 From: David Nix Date: Thu, 11 May 2023 14:33:28 -0600 Subject: [PATCH 1/3] refactor subsystems --- metrics/cosmos.go | 9 +++++++-- metrics/metrics.go | 7 +------ metrics/static.go | 3 ++- metrics/static_test.go | 16 ++++++++-------- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/metrics/cosmos.go b/metrics/cosmos.go index 0e399f9..1dce30c 100644 --- a/metrics/cosmos.go +++ b/metrics/cosmos.go @@ -12,10 +12,15 @@ type Cosmos struct { } func NewCosmos() *Cosmos { + const ( + cosmosSubsystem = "cosmos" + cosmosValSubsystem = cosmosSubsystem + "_val" + ) + return &Cosmos{ heightGauge: prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: prometheus.BuildFQName(Namespace, CosmosSubsystem, "latest_block_height"), + Name: prometheus.BuildFQName(namespace, cosmosSubsystem, "latest_block_height"), Help: "Latest block height of a cosmos node.", }, // labels @@ -23,7 +28,7 @@ func NewCosmos() *Cosmos { ), valJailGauge: prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: prometheus.BuildFQName(Namespace, CosmosValSubsystem, "latest_jailed_status"), + Name: prometheus.BuildFQName(namespace, cosmosValSubsystem, "latest_jailed_status"), Help: "0 if the validator is not jailed. 1 if the validator is jailed. 2 if the validator is tombstoned.", }, // labels diff --git a/metrics/metrics.go b/metrics/metrics.go index 8e3363d..fe412a6 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -1,8 +1,3 @@ package metrics -const ( - Namespace = "sl_exporter" - NoSubsystem = "" - CosmosSubsystem = "cosmos" - CosmosValSubsystem = CosmosSubsystem + "_val" -) +const namespace = "sl_exporter" diff --git a/metrics/static.go b/metrics/static.go index 7e9e831..48d4342 100644 --- a/metrics/static.go +++ b/metrics/static.go @@ -16,10 +16,11 @@ type StaticSample struct { // BuildStatic returns static metrics func BuildStatic(gauges []StaticGauge) []prometheus.Collector { + const subsystem = "static" metrics := make([]prometheus.Collector, len(gauges)) for i, g := range gauges { gaugeVec := prometheus.NewGaugeVec( - prometheus.GaugeOpts{Name: prometheus.BuildFQName(Namespace, NoSubsystem, g.Name), Help: g.Description}, + prometheus.GaugeOpts{Name: prometheus.BuildFQName(namespace, subsystem, g.Name), Help: g.Description}, g.Labels, ) for _, sample := range g.Samples { diff --git a/metrics/static_test.go b/metrics/static_test.go index 7ae2ae3..e449345 100644 --- a/metrics/static_test.go +++ b/metrics/static_test.go @@ -41,13 +41,13 @@ func TestBuildStatic(t *testing.T) { r := httptest.NewRecorder() h.ServeHTTP(r, stubRequest) - const want = `# HELP sl_exporter_gauge_1 desc_1 -# TYPE sl_exporter_gauge_1 gauge -sl_exporter_gauge_1{chain="agoric-1",denom="ubld"} 1 -sl_exporter_gauge_1{chain="cosmoshub-4",denom="uatom"} 2 -# HELP sl_exporter_gauge_2 desc_2 -# TYPE sl_exporter_gauge_2 gauge -sl_exporter_gauge_2 3` + const want = `# HELP sl_exporter_static_gauge_1 desc_1 +# TYPE sl_exporter_static_gauge_1 gauge +sl_exporter_static_gauge_1{chain="agoric-1",denom="ubld"} 1 +sl_exporter_static_gauge_1{chain="cosmoshub-4",denom="uatom"} 2 +# HELP sl_exporter_static_gauge_2 desc_2 +# TYPE sl_exporter_static_gauge_2 gauge +sl_exporter_static_gauge_2 3` require.Equal(t, want, strings.TrimSpace(r.Body.String())) }) @@ -58,7 +58,7 @@ sl_exporter_gauge_2 3` t.Run("invalid labels", func(t *testing.T) { gauges := []StaticGauge{ { - Name: "sl_exporter_gauge_1", + Name: "sl_exporter_static_gauge_1", Description: "desc_1", Labels: []string{"chain", "denom"}, Samples: []StaticSample{ From b5956eb2e473147f7ccdb45df63ab40c81f15438 Mon Sep 17 00:00:00 2001 From: David Nix Date: Thu, 11 May 2023 14:38:44 -0600 Subject: [PATCH 2/3] Shorten constants --- metrics/cosmos.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/metrics/cosmos.go b/metrics/cosmos.go index 1dce30c..b37f22b 100644 --- a/metrics/cosmos.go +++ b/metrics/cosmos.go @@ -13,14 +13,14 @@ type Cosmos struct { func NewCosmos() *Cosmos { const ( - cosmosSubsystem = "cosmos" - cosmosValSubsystem = cosmosSubsystem + "_val" + subsystem = "cosmos" + valSubsystem = subsystem + "_val" ) return &Cosmos{ heightGauge: prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: prometheus.BuildFQName(namespace, cosmosSubsystem, "latest_block_height"), + Name: prometheus.BuildFQName(namespace, subsystem, "latest_block_height"), Help: "Latest block height of a cosmos node.", }, // labels @@ -28,7 +28,7 @@ func NewCosmos() *Cosmos { ), valJailGauge: prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: prometheus.BuildFQName(namespace, cosmosValSubsystem, "latest_jailed_status"), + Name: prometheus.BuildFQName(namespace, valSubsystem, "latest_jailed_status"), Help: "0 if the validator is not jailed. 1 if the validator is jailed. 2 if the validator is tombstoned.", }, // labels From befd042e98256f4eb23360a683115e5618fd10f9 Mon Sep 17 00:00:00 2001 From: David Nix Date: Thu, 11 May 2023 14:40:06 -0600 Subject: [PATCH 3/3] Move back to pkg level consts Easier for developer to see all the subsystems. --- metrics/cosmos.go | 9 ++------- metrics/metrics.go | 7 ++++++- metrics/static.go | 3 +-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/metrics/cosmos.go b/metrics/cosmos.go index b37f22b..0855ab5 100644 --- a/metrics/cosmos.go +++ b/metrics/cosmos.go @@ -12,15 +12,10 @@ type Cosmos struct { } func NewCosmos() *Cosmos { - const ( - subsystem = "cosmos" - valSubsystem = subsystem + "_val" - ) - return &Cosmos{ heightGauge: prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: prometheus.BuildFQName(namespace, subsystem, "latest_block_height"), + Name: prometheus.BuildFQName(namespace, cosmosSubsystem, "latest_block_height"), Help: "Latest block height of a cosmos node.", }, // labels @@ -28,7 +23,7 @@ func NewCosmos() *Cosmos { ), valJailGauge: prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: prometheus.BuildFQName(namespace, valSubsystem, "latest_jailed_status"), + Name: prometheus.BuildFQName(namespace, cosmosValSubsystem, "latest_jailed_status"), Help: "0 if the validator is not jailed. 1 if the validator is jailed. 2 if the validator is tombstoned.", }, // labels diff --git a/metrics/metrics.go b/metrics/metrics.go index fe412a6..122ec5b 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -1,3 +1,8 @@ package metrics -const namespace = "sl_exporter" +const ( + namespace = "sl_exporter" + staticSubsystem = "static" + cosmosSubsystem = "cosmos" + cosmosValSubsystem = cosmosSubsystem + "_val" +) diff --git a/metrics/static.go b/metrics/static.go index 48d4342..f37af70 100644 --- a/metrics/static.go +++ b/metrics/static.go @@ -16,11 +16,10 @@ type StaticSample struct { // BuildStatic returns static metrics func BuildStatic(gauges []StaticGauge) []prometheus.Collector { - const subsystem = "static" metrics := make([]prometheus.Collector, len(gauges)) for i, g := range gauges { gaugeVec := prometheus.NewGaugeVec( - prometheus.GaugeOpts{Name: prometheus.BuildFQName(namespace, subsystem, g.Name), Help: g.Description}, + prometheus.GaugeOpts{Name: prometheus.BuildFQName(namespace, staticSubsystem, g.Name), Help: g.Description}, g.Labels, ) for _, sample := range g.Samples {