Skip to content

Commit

Permalink
feat: add latest block height metric (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Jan 2, 2025
1 parent 1094419 commit f28db26
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 24 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ linters:
- funlen
- nlreturn
- wrapcheck
- gomnd
- cyclop
- err113
- exhaustruct
Expand Down
3 changes: 0 additions & 3 deletions assets/cosmovisor/upgrades/v15/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package v15

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEmpty(t *testing.T) {
t.Parallel()
assert.True(t, true)
}
9 changes: 1 addition & 8 deletions cmd/cosmos-node-exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -18,7 +17,6 @@ func TestValidateConfigNoConfigProvided(t *testing.T) {

os.Args = []string{"cmd", "validate-config"}
main()
assert.True(t, true)
}

//nolint:paralleltest // disabled
Expand All @@ -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
Expand All @@ -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
Expand All @@ -64,7 +59,6 @@ func TestStartNoConfigProvided(t *testing.T) {

os.Args = []string{"cmd"}
main()
assert.True(t, true)
}

//nolint:paralleltest // disabled
Expand All @@ -77,5 +71,4 @@ func TestStartConfigProvided(t *testing.T) {

os.Args = []string{"cmd", "--config", "../assets/config-invalid.toml"}
main()
assert.True(t, true)
}
3 changes: 1 addition & 2 deletions pkg/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions pkg/constants/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package constants

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEmpty(t *testing.T) {
t.Parallel()
assert.True(t, true)
}
5 changes: 5 additions & 0 deletions pkg/generators/node_status_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
17 changes: 11 additions & 6 deletions pkg/generators/node_status_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 9 additions & 1 deletion pkg/metrics/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
),
Expand Down
1 change: 1 addition & 0 deletions pkg/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f28db26

Please sign in to comment.