Skip to content

Commit 2f9960e

Browse files
authored
add gas price metrics (#423)
1 parent 0425d3e commit 2f9960e

File tree

5 files changed

+249
-10
lines changed

5 files changed

+249
-10
lines changed

monitoring/pkg/monitoring/exporter_transmission_details.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package monitoring
33
import (
44
"context"
55
"fmt"
6+
"math/big"
67

78
relayMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring"
89
)
@@ -47,6 +48,23 @@ func (p *transmissionDetailsExporter) Export(ctx context.Context, data interface
4748
}
4849

4950
for _, t := range transmissionsEnvelope.Transmissions {
51+
// gas price
52+
divisor := new(big.Int).Exp(new(big.Int).SetUint64(10), new(big.Int).SetUint64(18), nil) // 10^18
53+
gasPriceInSTRK := new(big.Int).Div(t.GasPrice, divisor)
54+
p.metrics.SetTransmissionGasPrice(
55+
toFloat64(gasPriceInSTRK),
56+
p.feedConfig.ContractAddress,
57+
p.feedConfig.GetID(),
58+
p.chainConfig.GetChainID(),
59+
p.feedConfig.GetContractStatus(),
60+
p.feedConfig.GetContractType(),
61+
p.feedConfig.Name,
62+
p.feedConfig.Path,
63+
p.chainConfig.GetNetworkID(),
64+
p.chainConfig.GetNetworkName(),
65+
)
66+
67+
// observation length
5068
observationLength := float64(t.ObservationLength)
5169
p.metrics.SetReportObservations(
5270
observationLength,
@@ -64,6 +82,17 @@ func (p *transmissionDetailsExporter) Export(ctx context.Context, data interface
6482
}
6583

6684
func (p *transmissionDetailsExporter) Cleanup(_ context.Context) {
85+
p.metrics.CleanupTransmissionGasPrice(
86+
p.feedConfig.GetContractAddress(),
87+
p.feedConfig.GetID(),
88+
p.chainConfig.GetChainID(),
89+
p.feedConfig.GetContractStatus(),
90+
p.feedConfig.GetContractType(),
91+
p.feedConfig.GetName(),
92+
p.feedConfig.GetPath(),
93+
p.chainConfig.GetNetworkID(),
94+
p.chainConfig.GetNetworkName(),
95+
)
6796
p.metrics.CleanupReportObservations(
6897
p.feedConfig.GetContractAddress(),
6998
p.feedConfig.GetID(),
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package monitoring
2+
3+
import (
4+
"context"
5+
"math/big"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/smartcontractkit/chainlink-starknet/monitoring/pkg/monitoring/mocks"
11+
12+
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring"
13+
)
14+
15+
func TestTransmissionDetailsExporter(t *testing.T) {
16+
chainConfig := generateChainConfig()
17+
feedConfig := generateFeedConfig()
18+
19+
mockMetrics := mocks.NewMetrics(t)
20+
factory := NewTransmissionDetailsExporterFactory(mockMetrics)
21+
22+
gasPrice, ok := new(big.Int).SetString("10000000000000000000", 10)
23+
require.True(t, ok)
24+
25+
envelope := TransmissionsEnvelope{
26+
Transmissions: []TransmissionInfo{{
27+
GasPrice: gasPrice, // 10 STRK (10^19 FRI)
28+
ObservationLength: 123,
29+
},
30+
},
31+
}
32+
33+
mockMetrics.On(
34+
"SetTransmissionGasPrice",
35+
float64(10),
36+
feedConfig.ContractAddress,
37+
feedConfig.GetID(),
38+
chainConfig.GetChainID(),
39+
feedConfig.GetContractStatus(),
40+
feedConfig.GetContractType(),
41+
feedConfig.Name,
42+
feedConfig.Path,
43+
chainConfig.GetNetworkID(),
44+
chainConfig.GetNetworkName(),
45+
).Once()
46+
47+
mockMetrics.On(
48+
"SetReportObservations",
49+
float64(123),
50+
feedConfig.ContractAddress,
51+
feedConfig.GetID(),
52+
chainConfig.GetChainID(),
53+
feedConfig.GetContractStatus(),
54+
feedConfig.GetContractType(),
55+
feedConfig.Name,
56+
feedConfig.Path,
57+
chainConfig.GetNetworkID(),
58+
chainConfig.GetNetworkName(),
59+
).Once()
60+
61+
exporter, err := factory.NewExporter(commonMonitoring.ExporterParams{
62+
ChainConfig: chainConfig,
63+
FeedConfig: feedConfig,
64+
})
65+
require.NoError(t, err)
66+
67+
exporter.Export(context.Background(), envelope)
68+
69+
// cleanup
70+
mockMetrics.On(
71+
"CleanupReportObservations",
72+
feedConfig.ContractAddress,
73+
feedConfig.GetID(),
74+
chainConfig.GetChainID(),
75+
feedConfig.GetContractStatus(),
76+
feedConfig.GetContractType(),
77+
feedConfig.Name,
78+
feedConfig.Path,
79+
chainConfig.GetNetworkID(),
80+
chainConfig.GetNetworkName(),
81+
).Once()
82+
mockMetrics.On(
83+
"CleanupTransmissionGasPrice",
84+
feedConfig.ContractAddress,
85+
feedConfig.GetID(),
86+
chainConfig.GetChainID(),
87+
feedConfig.GetContractStatus(),
88+
feedConfig.GetContractType(),
89+
feedConfig.Name,
90+
feedConfig.Path,
91+
chainConfig.GetNetworkID(),
92+
chainConfig.GetNetworkName(),
93+
).Once()
94+
95+
exporter.Cleanup(context.Background())
96+
97+
}

monitoring/pkg/monitoring/metrics.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
)
99

1010
// Metrics is an interface for prometheus metrics. Makes testing easier.
11+
//
12+
//go:generate mockery --name Metrics --output ./mocks/
1113
type Metrics interface {
14+
SetTransmissionGasPrice(answer float64, contractAddress, feedID, chainID, contractStatus, contractType, feedName, feedPath, networkID, networkName string)
15+
CleanupTransmissionGasPrice(contractAddress, feedID, chainID, contractStatus, contractType, feedName, feedPath, networkID, networkName string)
1216
SetReportObservations(answer float64, accountAddress, feedID, chainID, contractStatus, contractType, feedName, feedPath, networkID, networkName string)
1317
CleanupReportObservations(accountAddress, feedID, chainID, contractStatus, contractType, feedName, feedPath, networkID, networkName string)
1418
SetProxyAnswersRaw(answer float64, proxyContractAddress, feedID, chainID, contractStatus, contractType, feedName, feedPath, networkID, networkName string)
@@ -19,12 +23,30 @@ type Metrics interface {
1923
}
2024

2125
var (
26+
transmissionGasPrice = promauto.NewGaugeVec(
27+
prometheus.GaugeOpts{
28+
Name: "starknet_transmission_gas_price",
29+
Help: "Reports gas price (units STRK) reported with transmission",
30+
},
31+
[]string{
32+
"contract_address",
33+
"feed_id",
34+
"chain_id",
35+
"contract_status",
36+
"contract_type",
37+
"feed_name",
38+
"feed_path",
39+
"network_id",
40+
"network_name",
41+
},
42+
)
2243
reportObservations = promauto.NewGaugeVec(
2344
prometheus.GaugeOpts{
2445
Name: "report_observations",
2546
Help: "Reports # of observations included in a transmission report",
2647
},
2748
[]string{
49+
// uses "account_address" instead of "contract_address" for consistency with solana dashboards
2850
"account_address",
2951
"feed_id",
3052
"chain_id",
@@ -68,6 +90,37 @@ type defaultMetrics struct {
6890
log relayMonitoring.Logger
6991
}
7092

93+
func (d *defaultMetrics) SetTransmissionGasPrice(answer float64, contractAddress, feedID, chainID, contractStatus, contractType, feedName, feedPath, networkID, networkName string) {
94+
transmissionGasPrice.With(prometheus.Labels{
95+
"contract_address": contractAddress,
96+
"feed_id": feedID,
97+
"chain_id": chainID,
98+
"contract_status": contractStatus,
99+
"contract_type": contractType,
100+
"feed_name": feedName,
101+
"feed_path": feedPath,
102+
"network_id": networkID,
103+
"network_name": networkName,
104+
}).Set(answer)
105+
}
106+
107+
func (d *defaultMetrics) CleanupTransmissionGasPrice(contractAddress, feedID, chainID, contractStatus, contractType, feedName, feedPath, networkID, networkName string) {
108+
labels := prometheus.Labels{
109+
"contract_address": contractAddress,
110+
"feed_id": feedID,
111+
"chain_id": chainID,
112+
"contract_status": contractStatus,
113+
"contract_type": contractType,
114+
"feed_name": feedName,
115+
"feed_path": feedPath,
116+
"network_id": networkID,
117+
"network_name": networkName,
118+
}
119+
if !transmissionGasPrice.Delete(labels) {
120+
d.log.Errorw("failed to delete metric", "name", "starknet_transmission_gas_price", "labels", labels)
121+
}
122+
}
123+
71124
func (d *defaultMetrics) SetBalance(answer float64, contractAddress, alias, networkId, networkName, chainID string) {
72125
contractBalance.With(prometheus.Labels{
73126
"contract_address": contractAddress,

monitoring/pkg/monitoring/mocks/Metrics.go

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

monitoring/pkg/monitoring/source_contract_balance_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,9 @@ import (
1313
)
1414

1515
func TestContractBalancesSource(t *testing.T) {
16-
// This test makes sure that the mapping between the response from the ocr2.Client
17-
// method calls and the output of the Proxy source is correct.
18-
1916
chainConfig := generateChainConfig()
2017
nodeConfig := generateNodeConfig()
2118

22-
// ocr2Reader := ocr2Mocks.NewOCR2Reader(t)
23-
// ocr2Reader.On(
24-
// "LatestRoundData",
25-
// mock.Anything, // ctx
26-
// proxyContractAddressFelt,
27-
// ).Return(ocr2ClientLatestRoundDataResponseForProxy, nil).Once()
28-
2919
erc20Reader := erc20Mocks.NewERC20Reader(t)
3020

3121
for _, x := range nodeConfig {

0 commit comments

Comments
 (0)