-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77fa7f1
commit 1ab1a91
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |