Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/metrics): add module for emiting custom chain metrics (backport #1668) #1673

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,44 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### State Machine Breaking
[\#1158](https://github.com/Kava-Labs/kava/pull/1158) Split existing auction `bid_duration` parameter into `forward_bid_duration` and `reverse_bid_duration`
- (metrics) [#1668] Adds non-state breaking x/metrics module for custom telemetry.

## [v0.18.2](https://github.com/Kava-Labs/kava/releases/tag/v0.18.2)

* Update to go 1.18, cosmos v0.45.9, and tendermint v0.34.21
* Update ci & docker to go 1.18
* Config updates for iavl changes and broadcast mode flag
* Ensure fast node is disable if config value is not set in order to
avoid fast node upgrade for nodes that do not update their app.toml
* Update to new circle ci image instead of using legacy image
* Remove previous ics23 package -- now used directly from cosmos-sdk repo

## [v0.18.1](https://github.com/Kava-Labs/kava/releases/tag/v0.18.1)

Patch for IBC Security Advisory Dragonberry: https://forum.cosmos.network/t/ibc-security-advisory-dragonberry/7702

## [v0.18.0](https://github.com/Kava-Labs/kava/releases/tag/v0.18.0)

Security upgrade patch release for kava 10.

## [v0.17.3](https://github.com/Kava-Labs/kava/releases/tag/v0.17.3)

Release of Kava EVM & the first binary with chain id `kava_2222-10`.

See Release for full details:
* [v0.17.3](https://github.com/Kava-Labs/kava/releases/tag/v0.17.3)
* [v0.17.1](https://github.com/Kava-Labs/kava/releases/tag/v0.17.1)

## [v0.16.1](https://github.com/Kava-Labs/kava/releases/tag/v0.16.1)

### State Machine Breaking

[\#1152](https://github.com/Kava-Labs/kava/pull/1152) Fix MultiSpend Proposal With Async Upgrade Time
[\#1152](https://github.com/Kava-Labs/kava/pull/1152) Fix MultiSpend Proposal With Async Upgrade Time

## [v0.16.0](https://github.com/Kava-Labs/kava/releases/tag/v0.16.0)

### State Machine Breaking
[\#1106](https://github.com/Kava-Labs/kava/pull/1106) Upgrades app to cosmos-sdk v0.44.x and adds IBC and ICS-20 modules.
[\#1106](https://github.com/Kava-Labs/kava/pull/1106) Upgrades app to cosmos-sdk v0.44.x and adds IBC and ICS-20 modules.

## [v0.13.0]

Expand Down Expand Up @@ -154,3 +179,5 @@ Bump tendermint version to 0.32.10 to address [cosmos security advisory Lavender
### Improvements

[\#257](https://github.com/Kava-Labs/kava/pulls/257) Include scripts to run large-scale simulations remotely using aws-batch

[#1668]: https://github.com/Kava-Labs/kava/pull/1668
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ format:
### Localnet ###
###############################################################################

docker-build:
DOCKER_BUILDKIT=1 $(DOCKER) build -t kava/kava:local .

build-docker-local-kava:
@$(MAKE) -C networks/local

Expand Down
8 changes: 8 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ import (
kavadistclient "github.com/kava-labs/kava/x/kavadist/client"
kavadistkeeper "github.com/kava-labs/kava/x/kavadist/keeper"
kavadisttypes "github.com/kava-labs/kava/x/kavadist/types"
metrics "github.com/kava-labs/kava/x/metrics"
metricstypes "github.com/kava-labs/kava/x/metrics/types"
pricefeed "github.com/kava-labs/kava/x/pricefeed"
pricefeedkeeper "github.com/kava-labs/kava/x/pricefeed/keeper"
pricefeedtypes "github.com/kava-labs/kava/x/pricefeed/types"
Expand Down Expand Up @@ -194,6 +196,7 @@ var (
validatorvesting.AppModuleBasic{},
evmutil.AppModuleBasic{},
bridge.AppModuleBasic{},
metrics.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -236,6 +239,7 @@ type Options struct {
MempoolAuthAddresses []sdk.AccAddress
EVMTrace string
EVMMaxGasWanted uint64
TelemetryOptions metricstypes.TelemetryOptions
}

// DefaultOptions is a sensible default Options value.
Expand Down Expand Up @@ -697,10 +701,12 @@ func NewApp(
evmutil.NewAppModule(app.evmutilKeeper, app.bankKeeper),
savings.NewAppModule(app.savingsKeeper, app.accountKeeper, app.bankKeeper),
bridge.NewAppModule(app.bridgeKeeper, app.accountKeeper),
metrics.NewAppModule(options.TelemetryOptions),
)

// Warning: Some begin blockers must run before others. Ensure the dependencies are understood before modifying this list.
app.mm.SetOrderBeginBlockers(
metricstypes.ModuleName,
// Upgrade begin blocker runs migrations on the first block after an upgrade. It should run before any other module.
upgradetypes.ModuleName,
// Capability begin blocker runs non state changing initialization.
Expand Down Expand Up @@ -783,6 +789,7 @@ func NewApp(
evmutiltypes.ModuleName,
savingstypes.ModuleName,
bridgetypes.ModuleName,
metricstypes.ModuleName,
)

// Warning: Some init genesis methods must run before others. Ensure the dependencies are understood before modifying this list
Expand Down Expand Up @@ -821,6 +828,7 @@ func NewApp(
paramstypes.ModuleName,
upgradetypes.ModuleName,
validatorvestingtypes.ModuleName,
metricstypes.ModuleName,
)

app.mm.RegisterInvariants(&app.crisisKeeper)
Expand Down
2 changes: 2 additions & 0 deletions cmd/kava/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/app/params"
metricstypes "github.com/kava-labs/kava/x/metrics/types"
)

const (
Expand Down Expand Up @@ -93,6 +94,7 @@ func (ac appCreator) newApp(
MempoolAuthAddresses: mempoolAuthAddresses,
EVMTrace: cast.ToString(appOpts.Get(ethermintflags.EVMTracer)),
EVMMaxGasWanted: cast.ToUint64(appOpts.Get(ethermintflags.EVMMaxTxGasWanted)),
TelemetryOptions: metricstypes.TelemetryOptionsFromAppOpts(appOpts),
},
baseapp.SetPruning(pruningOpts),
baseapp.SetMinGasPrices(strings.Replace(cast.ToString(appOpts.Get(server.FlagMinGasPrices)), ";", ",", -1)),
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ require (
github.com/cosmos/cosmos-sdk v0.45.9
github.com/cosmos/ibc-go/v3 v3.0.0
github.com/ethereum/go-ethereum v1.10.16
github.com/go-kit/kit v0.12.0
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/kava-labs/kava-bridge v0.2.0
github.com/prometheus/client_golang v1.12.2
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0
Expand Down Expand Up @@ -62,7 +64,6 @@ require (
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
Expand Down Expand Up @@ -113,7 +114,6 @@ require (
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
12 changes: 12 additions & 0 deletions x/metrics/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package metrics

import (
"github.com/kava-labs/kava/x/metrics/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// BeginBlocker publishes metrics at the start of each block.
func BeginBlocker(ctx sdk.Context, metrics *types.Metrics) {
metrics.LatestBlockHeight.Set(float64(ctx.BlockHeight()))
}
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)
}
132 changes: 132 additions & 0 deletions x/metrics/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package metrics

import (
"encoding/json"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/kava-labs/kava/x/metrics/types"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)

// AppModuleBasic app module basics object
type AppModuleBasic struct{}

// RegisterRESTRoutes implements module.AppModuleBasic.
func (AppModuleBasic) RegisterRESTRoutes(client.Context, *mux.Router) {}

// Name returns the module name
func (AppModuleBasic) Name() string {
return types.ModuleName
}

// RegisterLegacyAminoCodec register module codec
// Deprecated: unused but necessary to fulfill AppModuleBasic interface
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}

// DefaultGenesis default genesis state
func (AppModuleBasic) DefaultGenesis(_ codec.JSONCodec) json.RawMessage {
return []byte("{}")
}

// ValidateGenesis module validate genesis
func (AppModuleBasic) ValidateGenesis(_ codec.JSONCodec, _ client.TxEncodingConfig, _ json.RawMessage) error {
return nil
}

// RegisterInterfaces implements InterfaceModule.RegisterInterfaces
func (a AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module.
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {}

// GetTxCmd returns the root tx command for the module.
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return nil
}

// GetQueryCmd returns no root query command for the module.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return nil
}

//____________________________________________________________________________

// AppModule app module type
type AppModule struct {
AppModuleBasic
metrics *types.Metrics
}

// RegisterRESTRoutes implements module.AppModule.
func (AppModule) RegisterRESTRoutes(client.Context, *mux.Router) {}

// NewAppModule creates a new AppModule object
func NewAppModule(telemetryOpts types.TelemetryOptions) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
metrics: types.NewMetrics(telemetryOpts),
}
}

// Name module name
func (am AppModule) Name() string {
return am.AppModuleBasic.Name()
}

// RegisterInvariants register module invariants
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// Route module message route name
// Deprecated: unused but necessary to fulfill AppModule interface
func (am AppModule) Route() sdk.Route { return sdk.Route{} }

// QuerierRoute module querier route name
// Deprecated: unused but necessary to fulfill AppModule interface
func (AppModule) QuerierRoute() string { return types.ModuleName }

// LegacyQuerierHandler returns no sdk.Querier.
// Deprecated: unused but necessary to fulfill AppModule interface
func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {
return nil
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {}

// InitGenesis module init-genesis
func (am AppModule) InitGenesis(ctx sdk.Context, _ codec.JSONCodec, _ json.RawMessage) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}

// ExportGenesis module export genesis
func (am AppModule) ExportGenesis(_ sdk.Context, cdc codec.JSONCodec) json.RawMessage {
return nil
}

// BeginBlock module begin-block
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
BeginBlocker(ctx, am.metrics)
}

// EndBlock module end-block
func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}
36 changes: 36 additions & 0 deletions x/metrics/spec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
order: 0
title: "Metrics Overview"
parent:
title: "metrics"
-->

# `metrics`


## Abstract

`x/metrics` is a stateless module that does not affect consensus. It captures chain metrics and emits them when the `instrumentation.prometheus` option is enabled in `config.toml`.

## Precision

The metrics emitted by `x/metrics` are `float64`s. They use `github.com/go-kit/kit/metrics` Prometheus gauges. Cosmos-sdk's `telemetry` package was not used because, at the time of writing, it only supports `float32`s and so does not maintain accurate representations of ints larger than ~16.8M. With `float64`s, integers may be accurately represented up to ~9e15.

## Metrics

The following metrics are defined:
* `cometbft_blocksync_latest_block_height` - this emulates the blocksync `latest_block_height` metric in CometBFT v0.38+. The `cometbft` namespace comes from the `instrumentation.namespace` config.toml value.

## Metric Labels

All metrics emitted have the labels defined in app.toml's `telemetry.global-labels` field. This is the same field used by cosmos-sdk's `telemetry` package.

example:
```toml
# app.toml
[telemetry]
global-labels = [
["chain_id", "kava_2222-10"],
["my_label", "my_value"],
]
```
Loading