From f7d0d1015f93ea886583ffa5308119aa48391827 Mon Sep 17 00:00:00 2001 From: aalu1418 <50029043+aalu1418@users.noreply.github.com> Date: Thu, 18 Apr 2024 20:29:03 -0600 Subject: [PATCH] fee metrics --- pkg/monitoring/metrics/fees.go | 39 +++++++++++++++++++++++++ pkg/monitoring/metrics/fees_test.go | 45 +++++++++++++++++++++++++++++ pkg/monitoring/metrics/metrics.go | 16 +++++----- pkg/monitoring/types/txdetails.go | 10 ++++++- 4 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 pkg/monitoring/metrics/fees.go create mode 100644 pkg/monitoring/metrics/fees_test.go diff --git a/pkg/monitoring/metrics/fees.go b/pkg/monitoring/metrics/fees.go new file mode 100644 index 000000000..883b69485 --- /dev/null +++ b/pkg/monitoring/metrics/fees.go @@ -0,0 +1,39 @@ +package metrics + +import ( + commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" + + "github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" + "github.com/smartcontractkit/chainlink-solana/pkg/solana/fees" +) + +//go:generate mockery --name Fees --output ./mocks/ + +type Fees interface { + Set(txFee uint64, computeUnitPrice fees.ComputeUnitPrice, feedInput FeedInput) + Cleanup(feedInput FeedInput) +} + +var _ Fees = (*feeMetrics)(nil) + +type feeMetrics struct { + txFee simpleGauge + computeUnit simpleGauge +} + +func NewFees(log commonMonitoring.Logger) *feeMetrics { + return &feeMetrics{ + txFee: newSimpleGauge(log, types.TxFeeMetric), + computeUnit: newSimpleGauge(log, types.ComputeUnitPriceMetric), + } +} + +func (sh *feeMetrics) Set(txFee uint64, computeUnitPrice fees.ComputeUnitPrice, feedInput FeedInput) { + sh.txFee.set(float64(txFee), feedInput.ToPromLabels()) + sh.computeUnit.set(float64(computeUnitPrice), feedInput.ToPromLabels()) +} + +func (sh *feeMetrics) Cleanup(feedInput FeedInput) { + sh.txFee.delete(feedInput.ToPromLabels()) + sh.computeUnit.delete(feedInput.ToPromLabels()) +} diff --git a/pkg/monitoring/metrics/fees_test.go b/pkg/monitoring/metrics/fees_test.go new file mode 100644 index 000000000..9e2818fb9 --- /dev/null +++ b/pkg/monitoring/metrics/fees_test.go @@ -0,0 +1,45 @@ +package metrics + +import ( + "testing" + + "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/smartcontractkit/chainlink-common/pkg/logger" + + "github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" + "github.com/smartcontractkit/chainlink-solana/pkg/solana/fees" +) + +func TestFees(t *testing.T) { + lgr := logger.Test(t) + m := NewFees(lgr) + + // fetching gauges + gFees, ok := gauges[types.TxFeeMetric] + require.True(t, ok) + gComputeUnits, ok := gauges[types.ComputeUnitPriceMetric] + require.True(t, ok) + + v0 := 1 + v1 := 10 + l := FeedInput{NetworkID: t.Name()} + + // set gauge + assert.NotPanics(t, func() { + m.Set(uint64(v0), fees.ComputeUnitPrice(v1), l) + }) + num := testutil.ToFloat64(gFees.With(l.ToPromLabels())) + assert.Equal(t, float64(v0), num) + num = testutil.ToFloat64(gComputeUnits.With(l.ToPromLabels())) + assert.Equal(t, float64(v1), num) + + // cleanup gauges + assert.Equal(t, 1, testutil.CollectAndCount(gFees)) + assert.Equal(t, 1, testutil.CollectAndCount(gComputeUnits)) + assert.NotPanics(t, func() { m.Cleanup(l) }) + assert.Equal(t, 0, testutil.CollectAndCount(gFees)) + assert.Equal(t, 0, testutil.CollectAndCount(gComputeUnits)) +} diff --git a/pkg/monitoring/metrics/metrics.go b/pkg/monitoring/metrics/metrics.go index 32d2d44f8..e258854bd 100644 --- a/pkg/monitoring/metrics/metrics.go +++ b/pkg/monitoring/metrics/metrics.go @@ -57,13 +57,15 @@ func init() { nodeLabels, ) - // init gauge for observation count tracking - gauges[types.ReportObservationMetric] = promauto.NewGaugeVec( - prometheus.GaugeOpts{ - Name: types.ReportObservationMetric, - }, - feedLabels, - ) + // init gauges for tx details tracking + for _, txDetailMetric := range types.TxDetailsMetrics { + gauges[txDetailMetric] = promauto.NewGaugeVec( + prometheus.GaugeOpts{ + Name: txDetailMetric, + }, + feedLabels, + ) + } // init gauge for slot height gauges[types.SlotHeightMetric] = promauto.NewGaugeVec( diff --git a/pkg/monitoring/types/txdetails.go b/pkg/monitoring/types/txdetails.go index 8f04e61a1..9c9911102 100644 --- a/pkg/monitoring/types/txdetails.go +++ b/pkg/monitoring/types/txdetails.go @@ -16,7 +16,15 @@ import ( var ( TxDetailsType = "txdetails" - ReportObservationMetric = "report_observations" + ReportObservationMetric = "sol_report_observations" + TxFeeMetric = "sol_tx_fee" + ComputeUnitPriceMetric = "sol_tx_compute_unit_price" + + TxDetailsMetrics = []string{ + ReportObservationMetric, + TxFeeMetric, + ComputeUnitPriceMetric, + } ) type TxDetails struct {