Skip to content

Commit

Permalink
Add relayer version metric + dashboard. (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitryhil authored Apr 17, 2024
1 parent 0b69927 commit e384e9c
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ INTEGRATION_TESTS_DIR:=$(ROOT_DIR)/integration-tests
RELAYER_DIR:=$(ROOT_DIR)/relayer
BUILD_DIR?=$(ROOT_DIR)/build
GIT_TAG:=$(shell git describe --tags --exact-match 2>/dev/null)
ifeq ($(GIT_TAG),)
GIT_TAG:=devel
endif
GIT_SHA:=$(shell git rev-parse HEAD)
DOCKER_PUSH_TAG?=$(shell git describe --tags --exact-match 2>/dev/null || git rev-parse HEAD)
LD_FLAGS:="-extldflags=-static \
Expand Down
102 changes: 100 additions & 2 deletions infra/composer/grafana/dashboards/relayer.json
Original file line number Diff line number Diff line change
Expand Up @@ -1609,9 +1609,107 @@
],
"title": "Malicious behaviour",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unitScale": true
},
"overrides": []
},
"gridPos": {
"h": 7,
"w": 8,
"x": 0,
"y": 40
},
"id": 19,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "10.3.3",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"editorMode": "code",
"expr": "relayer_version{relayer_coreum_address=~\"$relayer_coreum_address\"}",
"format": "time_series",
"instant": false,
"legendFormat": "{{relayer_coreum_address}} | {{version}} ",
"range": true,
"refId": "A"
}
],
"title": "Relayer version",
"type": "timeseries"
}
],
"refresh": "auto",
"refresh": "5s",
"schemaVersion": 39,
"tags": [],
"templating": {
Expand Down Expand Up @@ -1715,7 +1813,7 @@
]
},
"time": {
"from": "now-1h",
"from": "now-5m",
"to": "now"
},
"timepicker": {},
Expand Down
57 changes: 55 additions & 2 deletions integration-tests/processes/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

coreumintegration "github.com/CoreumFoundation/coreum/v4/testutil/integration"
integrationtests "github.com/CoreumFoundation/coreumbridge-xrpl/integration-tests"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/buildinfo"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/coreum"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/metrics"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/runner"
Expand Down Expand Up @@ -761,7 +762,7 @@ func TestRelayerActivityMetrics(t *testing.T) {
runnerEnv.RunnerComponents[0].MetricsRegistry.RelayerActivityGaugeVec,
map[string]string{
metrics.RelayerCoremAddressLabel: relayer.CoreumAddress,
metrics.ContractActionLabel: "save_evidence",
metrics.ActionLabel: "save_evidence",
},
2,
)
Expand All @@ -772,7 +773,59 @@ func TestRelayerActivityMetrics(t *testing.T) {
runnerEnv.RunnerComponents[0].MetricsRegistry.RelayerActivityGaugeVec,
map[string]string{
metrics.RelayerCoremAddressLabel: relayer.CoreumAddress,
metrics.ContractActionLabel: "save_signature",
metrics.ActionLabel: "save_signature",
},
1,
)
}
}

func TestRelayerVersionMetrics(t *testing.T) {
t.Parallel()

ctx, chains := integrationtests.NewTestingContext(t)

envCfg := DefaultRunnerEnvConfig()
envCfg.RelayersCount = 2
envCfg.SigningThreshold = 2
runnerEnv := NewRunnerEnv(ctx, t, envCfg, chains)
runnerEnv.StartAllRunnerProcesses()
runnerEnv.StartAllRunnerPeriodicMetricCollectors()
runnerEnv.AllocateTickets(ctx, t, uint32(200))

coreumSenderAddress := chains.Coreum.GenAccount()
chains.Coreum.FundAccountWithOptions(ctx, t, coreumSenderAddress, coreumintegration.BalancesOptions{
Amount: sdkmath.NewIntWithDecimal(1, 6),
})

xrplSenderAddressAddress := chains.XRPL.GenAccount(ctx, t, 10)

valueToSendFromXRPLToCoreum, err := rippledata.NewValue("1.0", true)
require.NoError(t, err)

amountToSendFromXRPLtoCoreum := rippledata.Amount{
Value: valueToSendFromXRPLToCoreum,
Currency: xrpl.XRPTokenCurrency,
Issuer: xrpl.XRPTokenIssuer,
}
// save evidence to generate the tx with memo
runnerEnv.SendFromXRPLToCoreum(
ctx,
t,
xrplSenderAddressAddress.String(),
amountToSendFromXRPLtoCoreum,
coreumSenderAddress,
)

for _, relayer := range runnerEnv.BootstrappingConfig.Relayers {
awaitGaugeVecMetricState(
ctx,
t,
runnerEnv,
runnerEnv.RunnerComponents[0].MetricsRegistry.RelayerVersion,
map[string]string{
metrics.RelayerCoremAddressLabel: relayer.CoreumAddress,
metrics.VersionLabel: buildinfo.VersionTag,
},
1,
)
Expand Down
4 changes: 3 additions & 1 deletion relayer/coreum/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (

const (
contractLabel = "coreumbridge-xrpl"
// RelayerCoreumMemoPrefix is memo prefix for the relayer transaction.
RelayerCoreumMemoPrefix = "Coreum XRPL bridge relayer version:"

eventAttributeAction = "action"
eventAttributeHash = "hash"
Expand Down Expand Up @@ -1800,7 +1802,7 @@ func (c *ContractClient) getTxFactory() client.Factory {
WithKeybase(c.clientCtx.Keyring()).
WithChainID(c.clientCtx.ChainID()).
WithTxConfig(c.clientCtx.TxConfig()).
WithMemo(fmt.Sprintf("Coreum XRPL bridge relayer version: %s", buildinfo.VersionTag)).
WithMemo(fmt.Sprintf("%s %s", RelayerCoreumMemoPrefix, buildinfo.VersionTag)).
WithSimulateAndExecute(true)
}

Expand Down
55 changes: 47 additions & 8 deletions relayer/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type PeriodicCollector struct {
registry *Registry
xrplRPCClient XRPLRPCClient
contractClient ContractClient
clientContext client.Context
feemodelClient feemodeltypes.QueryClient
coreumBankClient banktypes.QueryClient
cometServiceClient sdktxtypes.ServiceClient
Expand All @@ -90,6 +91,7 @@ type PeriodicCollector struct {
transactionEvidencesCachedKeys map[string]struct{}
relayersBalancesCachedKeys map[string]struct{}
relayerActivityCachedKeys map[string]struct{}
relayerVersionCachedKeys map[string]struct{}
cacheMu sync.Mutex
}

Expand All @@ -113,6 +115,7 @@ func NewPeriodicCollector(
registry: registry,
xrplRPCClient: xrplRPCClient,
contractClient: contractClient,
clientContext: clientContext,
feemodelClient: feemodeltypes.NewQueryClient(clientContext),
coreumBankClient: banktypes.NewQueryClient(clientContext),
cometServiceClient: sdktxtypes.NewServiceClient(clientContext),
Expand All @@ -121,6 +124,7 @@ func NewPeriodicCollector(
transactionEvidencesCachedKeys: make(map[string]struct{}),
relayersBalancesCachedKeys: make(map[string]struct{}),
relayerActivityCachedKeys: make(map[string]struct{}),
relayerVersionCachedKeys: make(map[string]struct{}),
cacheMu: sync.Mutex{},
}
}
Expand All @@ -136,10 +140,10 @@ func (c *PeriodicCollector) Start(ctx context.Context) error {
transactionEvidencesMetricName: c.collectTransactionEvidences,
relayerBalancesMetricName: c.collectRelayerBalances,
fmt.Sprintf("%s/%s", freeContractTicketsMetricName, freeXRPLTicketsMetricName): c.collectFreeTickets,
bridgeStateMetricName: c.collectBridgeState,
relayerActivityMetricName: c.collectRelayerActivity,
xrplTokensCoreumSupplyMetricName: c.collectXRPLTokensCoreumSupply,
xrplBridgeAccountReservesMetricName: c.collectXRPLBridgeAccountReserves,
bridgeStateMetricName: c.collectBridgeState,
fmt.Sprintf("%s/%s", relayerActivityMetricName, relayerVersionMetricName): c.collectRelayerActivityAndVersion,
xrplTokensCoreumSupplyMetricName: c.collectXRPLTokensCoreumSupply,
xrplBridgeAccountReservesMetricName: c.collectXRPLBridgeAccountReserves,
}
return parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
for name, collector := range periodicCollectors {
Expand Down Expand Up @@ -411,12 +415,13 @@ func (c *PeriodicCollector) collectBridgeState(ctx context.Context) error {
return nil
}

func (c *PeriodicCollector) collectRelayerActivity(ctx context.Context) error {
func (c *PeriodicCollector) collectRelayerActivityAndVersion(ctx context.Context) error {
contractCfg, err := c.contractClient.GetContractConfig(ctx)
if err != nil {
return errors.Wrap(err, "failed to get contract config")
}
currentValues := make(map[string]gaugeVecValue, 0)
currentRelayerActivityValues := make(map[string]gaugeVecValue, 0)
currentRelayerVersionValues := make(map[string]gaugeVecValue, 0)
// get sequentially to prevent rate limit
for _, relayer := range contractCfg.Relayers {
relayerAddress := relayer.CoreumAddress
Expand All @@ -438,14 +443,48 @@ func (c *PeriodicCollector) collectRelayerActivity(ctx context.Context) error {
}
// fill the metric with the data
for action, count := range actionCount {
currentValues[strings.Join([]string{relayerAddress.String(), action}, "-")] = gaugeVecValue{
currentRelayerActivityValues[strings.Join([]string{relayerAddress.String(), action}, "-")] = gaugeVecValue{
keys: []string{relayerAddress.String(), action},
value: float64(count),
}
}

// fill relayer version
for _, txRes := range txResponses {
var sdkTx sdk.Tx
if err := c.clientContext.Codec().UnpackAny(txRes.Tx, &sdkTx); err != nil {
return errors.Errorf("failed to unpack sdk.Tx, tx:%v", sdkTx)
}
tx, ok := sdkTx.(*sdktxtypes.Tx)
if !ok {
continue
}
if !strings.HasPrefix(tx.Body.Memo, coreum.RelayerCoreumMemoPrefix) {
continue
}
version := strings.TrimSpace(strings.TrimPrefix(tx.Body.Memo, coreum.RelayerCoreumMemoPrefix))
if version == "" {
continue
}

currentRelayerVersionValues[strings.Join([]string{relayerAddress.String(), version}, "-")] = gaugeVecValue{
keys: []string{relayerAddress.String(), version},
value: float64(1),
}
break
}
}

c.updateGaugeVecAndCachedValues(currentValues, c.relayerActivityCachedKeys, c.registry.RelayerActivityGaugeVec)
c.updateGaugeVecAndCachedValues(
currentRelayerActivityValues,
c.relayerActivityCachedKeys,
c.registry.RelayerActivityGaugeVec,
)
c.updateGaugeVecAndCachedValues(
currentRelayerVersionValues,
c.relayerVersionCachedKeys,
c.registry.RelayerVersion,
)

return nil
}
Expand Down
20 changes: 17 additions & 3 deletions relayer/metrics/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
relayerActivityMetricName = "relayer_activity"
xrplTokensCoreumSupplyMetricName = "xrpl_tokens_coreum_supply"
xrplBridgeAccountReservesMetricName = "xrpl_bridge_account_reserves"
relayerVersionMetricName = "relayer_version"
xrplRPCDecodingErrorCounterMetricName = "xrpl_rpc_decoding_errors_total"

// XRPLCurrencyIssuerLabel is XRPL currency issuer label.
Expand All @@ -37,8 +38,10 @@ const (
RelayerCoremAddressLabel = "relayer_coreum_address"
// MaliciousBehaviourKeyLabel malicious behaviour key label.
MaliciousBehaviourKeyLabel = "malicious_behaviour_key"
// ContractActionLabel is contract action label.
ContractActionLabel = "action"
// ActionLabel is action label.
ActionLabel = "action"
// VersionLabel is version label.
VersionLabel = "version"
)

// Registry contains metrics.
Expand All @@ -58,6 +61,7 @@ type Registry struct {
BridgeStateGauge prometheus.Gauge
MaliciousBehaviourGaugeVec *prometheus.GaugeVec
RelayerActivityGaugeVec *prometheus.GaugeVec
RelayerVersion *prometheus.GaugeVec
XRPLTokensCoreumSupplyGaugeVec *prometheus.GaugeVec
XRPLBridgeAccountReservesGauge prometheus.Gauge
XRPLRPCDecodingErrorCounter prometheus.Counter
Expand Down Expand Up @@ -153,7 +157,16 @@ func NewRegistry() *Registry {
},
[]string{
RelayerCoremAddressLabel,
ContractActionLabel,
ActionLabel,
},
),
RelayerVersion: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: relayerVersionMetricName,
Help: "Relayer version",
},
[]string{
RelayerCoremAddressLabel,
VersionLabel,
},
),
XRPLTokensCoreumSupplyGaugeVec: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Expand Down Expand Up @@ -194,6 +207,7 @@ func (m *Registry) Register(registry prometheus.Registerer) error {
m.BridgeStateGauge,
m.MaliciousBehaviourGaugeVec,
m.RelayerActivityGaugeVec,
m.RelayerVersion,
m.XRPLTokensCoreumSupplyGaugeVec,
m.XRPLBridgeAccountReservesGauge,
m.XRPLRPCDecodingErrorCounter,
Expand Down

0 comments on commit e384e9c

Please sign in to comment.