Skip to content

Commit

Permalink
fee metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
aalu1418 committed Apr 19, 2024
1 parent 30f9ac5 commit f7d0d10
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
39 changes: 39 additions & 0 deletions pkg/monitoring/metrics/fees.go
Original file line number Diff line number Diff line change
@@ -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())
}
45 changes: 45 additions & 0 deletions pkg/monitoring/metrics/fees_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
16 changes: 9 additions & 7 deletions pkg/monitoring/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 9 additions & 1 deletion pkg/monitoring/types/txdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f7d0d10

Please sign in to comment.