Skip to content

Commit

Permalink
Merge branch 'main' into mpetrun5/message-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Oct 23, 2024
2 parents 5c9b03a + c4b524f commit c5a8019
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
2 changes: 2 additions & 0 deletions chains/evm/transactor/monitored/monitored.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type RawTx struct {
}

// GasPrice returns transaction gas price in gwei
//
// It returns base fee for both London and legacy transactions.
func (tx *RawTx) GasPrice() *big.Int {
var gasPrice *big.Int
if len(tx.gasPrice) == 1 {
Expand Down
8 changes: 4 additions & 4 deletions observability/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func initGasView() sdkmetric.View {

type RelayerMetrics struct {
*metrics.SystemMetrics
*metrics.NetworkMetrics
*metrics.MessageMetrics
*metrics.ChainMetrics

Opts api.MeasurementOption
}
Expand All @@ -130,19 +130,19 @@ func NewRelayerMetrics(ctx context.Context, meter metric.Meter, attributes ...at
return nil, err
}

networkMetrics, err := metrics.NewNetworkMetrics(ctx, meter, opts)
messageMetrics, err := metrics.NewMessageMetrics(ctx, meter, opts)
if err != nil {
return nil, err
}

messageMetrics, err := metrics.NewMessageMetrics(ctx, meter, opts)
chainMetrics, err := metrics.NewChainMetrics(ctx, meter, opts)
if err != nil {
return nil, err
}

return &RelayerMetrics{
SystemMetrics: systemMetrics,
NetworkMetrics: networkMetrics,
ChainMetrics: chainMetrics,
MessageMetrics: messageMetrics,
Opts: opts,
}, err
Expand Down
131 changes: 131 additions & 0 deletions observability/metrics/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package metrics

import (
"context"
"math/big"
"sync"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)

type ChainMetrics struct {
opts metric.MeasurementOption

blockDeltaGauge metric.Int64ObservableGauge
blockDeltaMap map[uint8]*big.Int
processedBlockMap map[uint8]*big.Int
processedBlockGauge metric.Int64ObservableGauge
chainHeadMap map[uint8]*big.Int
chainHeadGauge metric.Int64ObservableGauge
lock sync.Mutex

gasUsedHistogram metric.Int64Histogram
gasPriceHistogram metric.Int64Histogram
}

// NewChainMetrics initializes metrics that provide insight into chain processing and activity
func NewChainMetrics(ctx context.Context, meter metric.Meter, opts metric.MeasurementOption) (*ChainMetrics, error) {
blockDeltaMap := make(map[uint8]*big.Int)
blockDeltaGauge, err := meter.Int64ObservableGauge(
"relayer.BlockDelta",
metric.WithInt64Callback(func(context context.Context, result metric.Int64Observer) error {
for domainID, delta := range blockDeltaMap {
result.Observe(delta.Int64(),
opts,
metric.WithAttributes(attribute.Int64("domainID", int64(domainID))),
)
}
return nil
}),
metric.WithDescription("Difference between chain head and current indexed block per domain"),
)
if err != nil {
return nil, err
}

chainHeadMap := make(map[uint8]*big.Int)
chainHeadGauge, err := meter.Int64ObservableGauge(
"relayer.ChainHead",
metric.WithInt64Callback(func(context context.Context, result metric.Int64Observer) error {
for domainID, head := range chainHeadMap {
result.Observe(head.Int64(),
opts,
metric.WithAttributes(attribute.Int64("domainID", int64(domainID))),
)
}
return nil
}),
metric.WithDescription("Latest block of the chain."),
)
if err != nil {
return nil, err
}

processedBlockMap := make(map[uint8]*big.Int)
processedBlockGauge, err := meter.Int64ObservableGauge(
"relayer.ProcessedBlocks",
metric.WithInt64Callback(func(context context.Context, result metric.Int64Observer) error {
for domainID, block := range processedBlockMap {
result.Observe(block.Int64(),
opts,
metric.WithAttributes(attribute.Int64("domainID", int64(domainID))),
)
}
return nil
}),
metric.WithDescription("Latest processed block."),
)
if err != nil {
return nil, err
}

gasUsedHistogram, err := meter.Int64Histogram(
"relayer.GasUsed",
metric.WithDescription("Gas used per transaction."),
metric.WithUnit("gas"),
)
if err != nil {
return nil, err
}

gasPriceHistogram, err := meter.Int64Histogram(
"relayer.GasPrice",
metric.WithDescription("Gas price distribution per transaction in gwei."),
)
if err != nil {
return nil, err
}

return &ChainMetrics{
opts: opts,
blockDeltaMap: blockDeltaMap,
chainHeadMap: chainHeadMap,
blockDeltaGauge: blockDeltaGauge,
chainHeadGauge: chainHeadGauge,
processedBlockGauge: processedBlockGauge,
processedBlockMap: processedBlockMap,
gasUsedHistogram: gasUsedHistogram,
gasPriceHistogram: gasPriceHistogram,
}, nil
}

func (m *ChainMetrics) TrackBlockDelta(domainID uint8, head *big.Int, current *big.Int) {
m.lock.Lock()
defer m.lock.Unlock()

m.blockDeltaMap[domainID] = new(big.Int).Sub(head, current)
m.processedBlockMap[domainID] = new(big.Int).Set(current)
m.chainHeadMap[domainID] = new(big.Int).Set(head)
}

func (m *ChainMetrics) TrackGasUsage(domainID uint8, gasUsed uint64, gasPrice *big.Int) {
m.gasPriceHistogram.Record(
context.Background(),
gasPrice.Int64(),
metric.WithAttributes(attribute.Int64("domainID", int64(domainID))))
m.gasUsedHistogram.Record(
context.Background(),
int64(gasUsed),
metric.WithAttributes(attribute.Int64("domainID", int64(domainID))))
}

0 comments on commit c5a8019

Please sign in to comment.