Skip to content

Commit

Permalink
feat!: change time metrics (#55)
Browse files Browse the repository at this point in the history
* feat!: change time metrics

* chore: fix linting

* chore: add readme on building from source
  • Loading branch information
freak12techno authored Jul 1, 2024
1 parent a190503 commit e0e08a3
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 16 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ After that, you should unzip it, and you are ready to go:

```sh
wget <the link from the releases page>
tar xvfz <the filename you've just downloaded>
tar <the filename you've just downloaded>
./cosmos-node-exporter <params>
```
Alternatively, install `golang` (>1.18), clone the repo and build it:
```
git clone https://github.com/QuokkaStake/cosmos-node-exporter
cd cosmos-node-exporter
# This will generate a `cosmos-node-exporter` binary file in the repository folder
make build
# This will generate a `missed-blocks-checker` binary file in $GOPATH/bin
```
To run it in detached mode in background, first, we have to copy the file to the system apps folder:
```sh
Expand Down
12 changes: 12 additions & 0 deletions assets/cosmovisor/upgrades/v15/fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package v15

import (
"testing"

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

func TestEmpty(t *testing.T) {
t.Parallel()
assert.True(t, true)
}
4 changes: 2 additions & 2 deletions pkg/clients/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (t *RPC) GetUpgradePlan(ctx context.Context) (*upgradeTypes.Plan, query_inf
return response.Plan, upgradePlanQuery, nil
}

func (t *RPC) GetEstimateTimeTillBlock(ctx context.Context, height int64) (time.Time, query_info.QueryInfo, error) {
func (t *RPC) GetEstimateBlockTime(ctx context.Context, height int64) (time.Time, query_info.QueryInfo, error) {
childCtx, span := t.Tracer.Start(
ctx,
"Fetching estimate time till block",
Expand Down Expand Up @@ -183,5 +183,5 @@ func (t *RPC) GetEstimateTimeTillBlock(ctx context.Context, height int64) (time.
secondsTillEstimatedBlock := int64(float64(blocksTillEstimatedBlock) * blockTime)
durationTillEstimatedBlock := time.Duration(secondsTillEstimatedBlock * int64(time.Second))

return time.Now().Add(durationTillEstimatedBlock), upgradeTimeQuery, nil
return latestBlock.Result.Block.Header.Time.Add(durationTillEstimatedBlock), upgradeTimeQuery, nil
}
9 changes: 4 additions & 5 deletions pkg/clients/tendermint/tendermint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestTendermintTimeSinceLatestBlockFailCurrent(t *testing.T) {
tracer := tracing.InitNoopTracer()
rpc := NewRPC(config, *logger, tracer)

_, queryInfo, err := rpc.GetEstimateTimeTillBlock(context.Background(), 21078108)
_, queryInfo, err := rpc.GetEstimateBlockTime(context.Background(), 21078108)
require.Error(t, err)
assert.False(t, queryInfo.Success)
require.ErrorContains(t, err, "custom error")
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestTendermintTimeSinceLatestBlockFailOlder(t *testing.T) {
tracer := tracing.InitNoopTracer()
rpc := NewRPC(config, *logger, tracer)

_, queryInfo, err := rpc.GetEstimateTimeTillBlock(context.Background(), 21078108)
_, queryInfo, err := rpc.GetEstimateBlockTime(context.Background(), 21078108)
require.Error(t, err)
assert.False(t, queryInfo.Success)
require.ErrorContains(t, err, "custom error")
Expand Down Expand Up @@ -222,11 +222,10 @@ func TestTendermintTimeSinceLatestBlockSuccess(t *testing.T) {
tracer := tracing.InitNoopTracer()
rpc := NewRPC(config, *logger, tracer)

currentTime := time.Now()
timeTillBlock, queryInfo, err := rpc.GetEstimateTimeTillBlock(context.Background(), 21078108)
timeTillBlock, queryInfo, err := rpc.GetEstimateBlockTime(context.Background(), 21078108)
require.NoError(t, err)
require.True(t, queryInfo.Success)
assert.Equal(t, "1h41m29s", timeTillBlock.Sub(currentTime).Round(time.Second).String())
assert.Equal(t, "2024-06-29T19:21:21Z", timeTillBlock.UTC().Format(time.RFC3339))
}

//nolint:paralleltest // disabled due to httpmock usage
Expand Down
12 changes: 12 additions & 0 deletions pkg/constants/constants_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package constants

import (
"testing"

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

func TestEmpty(t *testing.T) {
t.Parallel()
assert.True(t, true)
}
6 changes: 3 additions & 3 deletions pkg/metrics/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func NewManager() *Manager {
[]string{"node"},
),

MetricNameTimeSinceLatestBlock: prometheus.NewGaugeVec(
MetricNameLatestBlockTime: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: constants.MetricsPrefix + "time_since_latest_block",
Help: "Time since latest block, in seconds",
Name: constants.MetricsPrefix + "latest_block_time",
Help: "Unix timestamp of the time of the latest block",
},
[]string{"node"},
),
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type MetricName string
const (
MetricNameCosmovisorVersion MetricName = "cosmovisor_version"
MetricNameCatchingUp MetricName = "catching_up"
MetricNameTimeSinceLatestBlock MetricName = "time_since_latest_block"
MetricNameLatestBlockTime MetricName = "latest_block_time"
MetricNameNodeInfo MetricName = "node_info"
MetricNameTendermintVersion MetricName = "tendermint_version"
MetricNameVotingPower MetricName = "voting_power"
Expand Down
5 changes: 2 additions & 3 deletions pkg/queriers/node_stats/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"main/pkg/metrics"
"main/pkg/query_info"
"main/pkg/utils"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -57,9 +56,9 @@ func (n *Querier) Get(ctx context.Context) ([]metrics.MetricInfo, []query_info.Q
Value: utils.BoolToFloat64(status.Result.SyncInfo.CatchingUp),
},
{
MetricName: metrics.MetricNameTimeSinceLatestBlock,
MetricName: metrics.MetricNameLatestBlockTime,
Labels: map[string]string{},
Value: time.Since(status.Result.SyncInfo.LatestBlockTime).Seconds(),
Value: float64(status.Result.SyncInfo.LatestBlockTime.Unix()),
},
{
MetricName: metrics.MetricNameNodeInfo,
Expand Down
2 changes: 1 addition & 1 deletion pkg/queriers/upgrades/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (u *Querier) Get(ctx context.Context) ([]metrics.MetricInfo, []query_info.Q
})

// Calculate upgrade estimated time
upgradeTime, upgradeTimeQuery, err := u.Tendermint.GetEstimateTimeTillBlock(childCtx, upgrade.Height)
upgradeTime, upgradeTimeQuery, err := u.Tendermint.GetEstimateBlockTime(childCtx, upgrade.Height)
queryInfos = append(queryInfos, upgradeTimeQuery)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/queriers/upgrades/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ func TestUpgradesQuerierTendermintCosmovisorOk(t *testing.T) {
upgradeTime := metrics[3]
assert.NotEmpty(t, upgradeTime.Labels["info"])
assert.Equal(t, "v1.5.0", upgradeTime.Labels["name"])
assert.InDelta(t, 1642330177, upgradeTime.Value, 0.01)

upgradePresent := metrics[4]
assert.NotEmpty(t, upgradePresent.Labels["info"])
Expand Down

0 comments on commit e0e08a3

Please sign in to comment.