Skip to content

Commit

Permalink
add x/metrics test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtleshell committed Aug 24, 2023
1 parent 77fa7f1 commit 1ab1a91
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
45 changes: 45 additions & 0 deletions x/metrics/abci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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)
}
72 changes: 72 additions & 0 deletions x/metrics/types/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package types_test

import (
"testing"

"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/prometheus"
"github.com/kava-labs/kava/x/metrics/types"
"github.com/stretchr/testify/require"
)

func isPrometheusGauge(g metrics.Gauge) bool {
_, ok := g.(*prometheus.Gauge)
return ok
}

var (
disabledOpts = types.TelemetryOptions{
PrometheusEnabled: false,
}
enabledOpts = types.TelemetryOptions{
PrometheusEnabled: true,
CometBFTMetricsNamespace: "cometbft",
GlobalLabelsAndValues: []string{"label1", "value1", "label2", "value2"},
}
)

func TestNewMetrics_DisabledVsEnabled(t *testing.T) {
myMetrics := types.NewMetrics(disabledOpts)
require.False(t, isPrometheusGauge(myMetrics.LatestBlockHeight))

myMetrics = types.NewMetrics(enabledOpts)
require.True(t, isPrometheusGauge(myMetrics.LatestBlockHeight))
}

type MockAppOpts struct {
store map[string]interface{}
}

func (mao *MockAppOpts) Get(key string) interface{} {
return mao.store[key]
}

func TestTelemetryOptionsFromAppOpts(t *testing.T) {
appOpts := MockAppOpts{store: make(map[string]interface{})}

// test disabled functionality
appOpts.store["instrumentation.prometheus"] = false

opts := types.TelemetryOptionsFromAppOpts(&appOpts)
require.False(t, opts.PrometheusEnabled)

// test enabled functionality
appOpts.store["instrumentation.prometheus"] = true
appOpts.store["instrumentation.namespace"] = "magic"
appOpts.store["telemetry.global-labels"] = []interface{}{}

opts = types.TelemetryOptionsFromAppOpts(&appOpts)
require.True(t, opts.PrometheusEnabled)
require.Equal(t, "magic", opts.CometBFTMetricsNamespace)
require.Len(t, opts.GlobalLabelsAndValues, 0)

appOpts.store["telemetry.global-labels"] = []interface{}{
[]interface{}{"label1", "value1"},
[]interface{}{"label2", "value2"},
}
opts = types.TelemetryOptionsFromAppOpts(&appOpts)
require.True(t, opts.PrometheusEnabled)
require.Equal(t, "magic", opts.CometBFTMetricsNamespace)
require.Len(t, opts.GlobalLabelsAndValues, 4)
require.Equal(t, enabledOpts.GlobalLabelsAndValues, opts.GlobalLabelsAndValues)
}

0 comments on commit 1ab1a91

Please sign in to comment.