8000 chore: Add decimals to metrics by davidterpay · Pull Request #111 · skip-mev/connect · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

chore: Add decimals to metrics #111

Merged
merged 2 commits into from
Feb 12, 2024
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
37 changes: 25 additions & 12 deletions oracle/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package metrics

import (
"fmt"

"github.com/prometheus/client_golang/prometheus"

"github.com/skip-mev/slinky/oracle/config"
Expand All @@ -11,10 +13,11 @@ const (
ProviderLabel = "provider"
// ProviderTypeLabel is a label for the type of provider (WS, API, etc.)
ProviderTypeLabel = "type"
// PairIDLabel is the.
// PairIDLabel is the currency pair for which the metric applies.
PairIDLabel = "pair"
// OracleSubsystem is a subsystem shared by all metrics exposed by this
// package.
// DecimalsLabel is the number of decimal points associated with the price.
DecimalsLabel = "decimals"
// OracleSubsystem is a subsystem shared by all metrics exposed by this package.
OracleSubsystem = "oracle"
)

Expand All @@ -27,10 +30,10 @@ type Metrics interface {
AddTick()

// UpdatePrice price updates the price for the given pairID for the provider.
UpdatePrice(name, handlerType, pairID string, price float64)
UpdatePrice(name, handlerType, pairID string, decimals int, price float64)

// UpdateAggregatePrice updates the aggregated price for the given pairID.
UpdateAggregatePrice(pairID string, price float64)
UpdateAggregatePrice(pairID string, decimals int, price float64)
}

// OracleMetricsImpl is a Metrics implementation that does nothing.
Expand Down Expand Up @@ -61,12 +64,12 @@ func NewMetrics() Metrics {
Namespace: OracleSubsystem,
Name: "provider_price",
Help: "Price gauge for a given currency pair on a provider",
}, []string{ProviderLabel, ProviderTypeLabel, PairIDLabel}),
}, []string{ProviderLabel, ProviderTypeLabel, PairIDLabel, DecimalsLabel}),
aggregatePrices: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: OracleSubsystem,
Name: "aggregate_price",
Help: "Aggregate price for a given currency pair",
}, []string{PairIDLabel}),
}, []string{PairIDLabel, DecimalsLabel}),
}

// register the above metrics
Expand All @@ -89,11 +92,11 @@ func (m *noOpOracleMetrics) AddTick() {
}

// UpdatePrice price updates the price for the given pairID for the provider.
func (m *noOpOracleMetrics) UpdatePrice(_, _, _ string, _ float64) {
func (m *noOpOracleMetrics) UpdatePrice(_, _, _ string, _ int, _ float64) {
}

// UpdateAggregatePrice updates the aggregated price for the given pairID.
func (m *noOpOracleMetrics) UpdateAggregatePrice(_ string, _ float64) {
func (m *noOpOracleMetrics) UpdateAggregatePrice(string, int, float64) {
}

// AddTick increments the total number of ticks that have been processed by the oracle.
Expand All @@ -102,19 +105,29 @@ func (m *OracleMetricsImpl) AddTick() {
}

// UpdatePrice price updates the price for the given pairID for the provider.
func (m *OracleMetricsImpl) UpdatePrice(providerName, handlerType, pairID string, price float64) {
func (m *OracleMetricsImpl) UpdatePrice(
providerName, handlerType, pairID string,
decimals int,
price float64,
) {
m.prices.With(prometheus.Labels{
ProviderLabel: providerName,
ProviderTypeLabel: handlerType,
PairIDLabel: pairID,
DecimalsLabel: fmt.Sprintf("%d", decimals),
},
).Set(price)
}

// UpdateAggregatePrice updates the aggregated price for the given pairID.
func (m *OracleMetricsImpl) UpdateAggregatePrice(pairID string, price float64) {
func (m *OracleMetricsImpl) UpdateAggregatePrice(
pairID string,
decimals int,
price float64,
) {
m.aggregatePrices.With(prometheus.Labels{
PairIDLabel: pairID,
PairIDLabel: pairID,
DecimalsLabel: fmt.Sprintf("%d", decimals),
},
).Set(price)
}
14 changes: 7 additions & 7 deletions oracle/metrics/mocks/mock_metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,14 @@ func (o *OracleImpl) fetchPrices(provider providertypes.Provider[oracletypes.Cur
for pair, result := range prices {
floatValue, _ := result.Value.Float64() // we ignore the accuracy in this conversion

// update price metric
o.metrics.UpdatePrice(provider.Name(), string(provider.Type()), pair.String(), floatValue)
// Update price metrics.
o.metrics.UpdatePrice(
provider.Name(),
string(provider.Type()),
strings.ToLower(pair.String()),
pair.Decimals(),
floatValue,
)

// If the price is older than the update interval, skip it.
diff := time.Now().UTC().Sub(result.Timestamp)
Expand Down Expand Up @@ -260,7 +266,13 @@ func (o *OracleImpl) GetPrices() map[oracletypes.CurrencyPair]*big.Int {
// set metrics in background
go func() {
for cp, price := range prices {
o.metrics.UpdateAggregatePrice(strings.ToLower(cp.String()), float64(price.Int64()))
floatValue, _ := price.Float64() // we ignore the accuracy in this conversion

o.metrics.UpdateAggregatePrice(
strings.ToLower(cp.String()),
cp.Decimals(),
floatValue,
)
}
}()

Expand Down
0