mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-29 17:55:19 +00:00
6c565343f7
* feat(x/metrics): add module for emiting custom chain metrics (#1668)
* initialize x/metrics with metrics collection
* include global labels in x/metrics metrics
* add x/metrics spec
* add x/metrics test coverage
* update changelog
(cherry picked from commit 9a0aed7626
)
# Conflicts:
# CHANGELOG.md
* fix changlog conflicts
---------
Co-authored-by: Robert Pirtle <Astropirtle@gmail.com>
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package metrics_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
kitmetrics "github.com/go-kit/kit/metrics"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
"github.com/kava-labs/kava/x/metrics"
|
|
"github.com/kava-labs/kava/x/metrics/types"
|
|
)
|
|
|
|
type MockGauge struct {
|
|
value float64
|
|
}
|
|
|
|
func (mg *MockGauge) With(labelValues ...string) kitmetrics.Gauge { return mg }
|
|
func (mg *MockGauge) Set(value float64) { mg.value = value }
|
|
func (*MockGauge) Add(_ float64) {}
|
|
|
|
func ctxWithHeight(height int64) sdk.Context {
|
|
tApp := app.NewTestApp()
|
|
tApp.InitializeFromGenesisStates()
|
|
return tApp.NewContext(false, tmproto.Header{Height: height})
|
|
}
|
|
|
|
func TestBeginBlockEmitsLatestHeight(t *testing.T) {
|
|
gauge := MockGauge{}
|
|
myMetrics := &types.Metrics{
|
|
LatestBlockHeight: &gauge,
|
|
}
|
|
|
|
metrics.BeginBlocker(ctxWithHeight(1), myMetrics)
|
|
require.EqualValues(t, 1, gauge.value)
|
|
|
|
metrics.BeginBlocker(ctxWithHeight(100), myMetrics)
|
|
require.EqualValues(t, 100, gauge.value)
|
|
|
|
metrics.BeginBlocker(ctxWithHeight(17e6), myMetrics)
|
|
require.EqualValues(t, 17e6, gauge.value)
|
|
}
|