From f28db26064c65dc465a01e81bf182501ce28bd2b Mon Sep 17 00:00:00 2001 From: Sergey <83376337+freak12techno@users.noreply.github.com> Date: Thu, 2 Jan 2025 23:54:20 +0300 Subject: [PATCH] feat: add latest block height metric (#65) --- .golangci.yml | 1 - assets/cosmovisor/upgrades/v15/fs_test.go | 3 --- cmd/cosmos-node-exporter_test.go | 9 +-------- pkg/app_test.go | 3 +-- pkg/constants/constants_test.go | 3 --- pkg/generators/node_status_generator.go | 5 +++++ pkg/generators/node_status_generator_test.go | 17 +++++++++++------ pkg/metrics/manager.go | 10 +++++++++- pkg/metrics/types.go | 1 + 9 files changed, 28 insertions(+), 24 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 73f2dcd..3a982d6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,7 +21,6 @@ linters: - funlen - nlreturn - wrapcheck - - gomnd - cyclop - err113 - exhaustruct diff --git a/assets/cosmovisor/upgrades/v15/fs_test.go b/assets/cosmovisor/upgrades/v15/fs_test.go index 2bf8f62..0d93c55 100644 --- a/assets/cosmovisor/upgrades/v15/fs_test.go +++ b/assets/cosmovisor/upgrades/v15/fs_test.go @@ -2,11 +2,8 @@ package v15 import ( "testing" - - "github.com/stretchr/testify/assert" ) func TestEmpty(t *testing.T) { t.Parallel() - assert.True(t, true) } diff --git a/cmd/cosmos-node-exporter_test.go b/cmd/cosmos-node-exporter_test.go index 0bce840..516a925 100644 --- a/cmd/cosmos-node-exporter_test.go +++ b/cmd/cosmos-node-exporter_test.go @@ -4,7 +4,6 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -18,7 +17,6 @@ func TestValidateConfigNoConfigProvided(t *testing.T) { os.Args = []string{"cmd", "validate-config"} main() - assert.True(t, true) } //nolint:paralleltest // disabled @@ -31,7 +29,6 @@ func TestValidateConfigFailedToLoad(t *testing.T) { os.Args = []string{"cmd", "validate-config", "--config", "../assets/config-not-found.toml"} main() - assert.True(t, true) } //nolint:paralleltest // disabled @@ -44,14 +41,12 @@ func TestValidateConfigInvalid(t *testing.T) { os.Args = []string{"cmd", "validate-config", "--config", "../assets/config-invalid.toml"} main() - assert.True(t, true) } //nolint:paralleltest // disabled -func TestValidateConfigValid(t *testing.T) { +func TestValidateConfigValid(_ *testing.T) { os.Args = []string{"cmd", "validate-config", "--config", "../assets/config-valid.toml"} main() - assert.True(t, true) } //nolint:paralleltest // disabled @@ -64,7 +59,6 @@ func TestStartNoConfigProvided(t *testing.T) { os.Args = []string{"cmd"} main() - assert.True(t, true) } //nolint:paralleltest // disabled @@ -77,5 +71,4 @@ func TestStartConfigProvided(t *testing.T) { os.Args = []string{"cmd", "--config", "../assets/config-invalid.toml"} main() - assert.True(t, true) } diff --git a/pkg/app_test.go b/pkg/app_test.go index e282cc3..1a8c684 100644 --- a/pkg/app_test.go +++ b/pkg/app_test.go @@ -62,12 +62,11 @@ func TestAppFailToStart(t *testing.T) { } //nolint:paralleltest // disabled -func TestAppStopOperation(t *testing.T) { +func TestAppStopOperation(_ *testing.T) { filesystem := &fs.TestFS{} app := NewApp(filesystem, "config-valid.toml", "1.2.3") app.Stop() - assert.True(t, true) } //nolint:paralleltest // disabled diff --git a/pkg/constants/constants_test.go b/pkg/constants/constants_test.go index 7aa22a5..9a50794 100644 --- a/pkg/constants/constants_test.go +++ b/pkg/constants/constants_test.go @@ -2,11 +2,8 @@ package constants import ( "testing" - - "github.com/stretchr/testify/assert" ) func TestEmpty(t *testing.T) { t.Parallel() - assert.True(t, true) } diff --git a/pkg/generators/node_status_generator.go b/pkg/generators/node_status_generator.go index 57ad843..089aa8b 100644 --- a/pkg/generators/node_status_generator.go +++ b/pkg/generators/node_status_generator.go @@ -31,6 +31,11 @@ func (g *NodeStatusGenerator) Get(state fetchers.State) []metrics.MetricInfo { Labels: map[string]string{}, Value: utils.BoolToFloat64(status.Result.SyncInfo.CatchingUp), }, + { + MetricName: metrics.MetricNameLatestBlockHeight, + Labels: map[string]string{}, + Value: float64(status.Result.SyncInfo.LatestBlockHeight), + }, { MetricName: metrics.MetricNameLatestBlockTime, Labels: map[string]string{}, diff --git a/pkg/generators/node_status_generator_test.go b/pkg/generators/node_status_generator_test.go index faf5771..5bd61ea 100644 --- a/pkg/generators/node_status_generator_test.go +++ b/pkg/generators/node_status_generator_test.go @@ -74,29 +74,34 @@ func TestNodeStatusGeneratorOk(t *testing.T) { generator := NewNodeStatusGenerator() metrics := generator.Get(state) - assert.Len(t, metrics, 5) + assert.Len(t, metrics, 6) catchingUp := metrics[0] assert.Empty(t, catchingUp.Labels) assert.Zero(t, catchingUp.Value) - timeSinceLatest := metrics[1] - assert.Empty(t, timeSinceLatest.Labels) + latestBlockHeight := metrics[1] + assert.Empty(t, latestBlockHeight.Labels) + assert.InDelta(t, float64(21076916), latestBlockHeight.Value, 0.01) - nodeInfo := metrics[2] + latestBlockTime := metrics[2] + assert.Empty(t, latestBlockTime.Labels) + assert.InDelta(t, 1719681623, latestBlockTime.Value, 0.01) + + nodeInfo := metrics[3] assert.Equal(t, map[string]string{ "chain": "cosmoshub-4", "moniker": "freak12techno", }, nodeInfo.Labels) assert.InDelta(t, 1, nodeInfo.Value, 0.01) - tendermintVersion := metrics[3] + tendermintVersion := metrics[4] assert.Equal(t, map[string]string{ "version": "0.37.6", }, tendermintVersion.Labels) assert.InDelta(t, 1, tendermintVersion.Value, 0.01) - votingPower := metrics[4] + votingPower := metrics[5] assert.Empty(t, votingPower.Labels) assert.Zero(t, votingPower.Value) } diff --git a/pkg/metrics/manager.go b/pkg/metrics/manager.go index 808f24c..8f5cf54 100644 --- a/pkg/metrics/manager.go +++ b/pkg/metrics/manager.go @@ -30,10 +30,18 @@ func NewManager() *Manager { []string{"node"}, ), + MetricNameLatestBlockHeight: prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: constants.MetricsPrefix + "latest_block_height", + Help: "Height of the node's latest block", + }, + []string{"node"}, + ), + MetricNameLatestBlockTime: prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: constants.MetricsPrefix + "latest_block_time", - Help: "Unix timestamp of the time of the latest block", + Help: "Unix timestamp of the time of the node's latest block", }, []string{"node"}, ), diff --git a/pkg/metrics/types.go b/pkg/metrics/types.go index 4bf1355..b0ca250 100644 --- a/pkg/metrics/types.go +++ b/pkg/metrics/types.go @@ -5,6 +5,7 @@ type MetricName string const ( MetricNameCosmovisorVersion MetricName = "cosmovisor_version" MetricNameCatchingUp MetricName = "catching_up" + MetricNameLatestBlockHeight MetricName = "latest_block_height" MetricNameLatestBlockTime MetricName = "latest_block_time" MetricNameNodeInfo MetricName = "node_info" MetricNameTendermintVersion MetricName = "tendermint_version"