Skip to content

Commit

Permalink
fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanut Lertwarachai authored and Tanut Lertwarachai committed Feb 14, 2025
1 parent 1c9523f commit 5c8f7da
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 61 deletions.
66 changes: 26 additions & 40 deletions internal/relayermetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ var metrics *PrometheusMetrics
var globalTelemetryEnabled bool

type PrometheusMetrics struct {
Registry *prometheus.Registry
TunnelCount prometheus.Counter
PacketReceived *prometheus.CounterVec
UnrelayedPacket *prometheus.GaugeVec
TasksCount *prometheus.CounterVec
TaskExecutionTime *prometheus.SummaryVec
DestinationChainCount prometheus.Counter
ActiveTargetContract prometheus.Gauge
TxCount *prometheus.CounterVec
TxProcessTime *prometheus.SummaryVec
GasUsed *prometheus.SummaryVec
Registry *prometheus.Registry
PacketReceived *prometheus.CounterVec
UnrelayedPacket *prometheus.GaugeVec
TaskCount *prometheus.CounterVec
TaskExecutionTime *prometheus.SummaryVec
TunnelPerDestinationChain *prometheus.CounterVec
ActiveTargetContract prometheus.Gauge
TxCount *prometheus.CounterVec
TxProcessTime *prometheus.SummaryVec
GasUsed *prometheus.SummaryVec
}

func updateMetrics(updateFn func()) {
Expand All @@ -40,13 +39,6 @@ func updateMetrics(updateFn func()) {
}
}

// AddTunnellCount increments the total tunnel count metric.
func AddTunnellCount(count uint64) {
updateMetrics(func() {
metrics.TunnelCount.Add(float64(count))
})
}

// IncPacketlReceived increments the count of successfully relayed packets for a specific tunnel.
func IncPacketlReceived(tunnelID uint64) {
updateMetrics(func() {
Expand All @@ -61,10 +53,10 @@ func SetUnrelayedPacket(tunnelID uint64, unrelayedPacket float64) {
})
}

// IncTasksCount increments the total task count for a specific tunnel.
func IncTasksCount(tunnelID uint64) {
// IncTaskCount increments the total task count for a specific tunnel.
func IncTaskCount(tunnelID uint64) {
updateMetrics(func() {
metrics.TasksCount.WithLabelValues(fmt.Sprintf("%d", tunnelID)).Inc()
metrics.TaskCount.WithLabelValues(fmt.Sprintf("%d", tunnelID)).Inc()
})
}

Expand All @@ -75,10 +67,10 @@ func ObserveTaskExecutionTime(tunnelID uint64, taskExecutionTime float64) {
})
}

// AddDestinationChainCount increments the total number of destination chains observed.
func AddDestinationChainCount(count uint64) {
// IncTunnelPerDestinationChain increments the total number of tunnels per specify destination chain.
func IncTunnelPerDestinationChain(destinationChain string) {
updateMetrics(func() {
metrics.DestinationChainCount.Add(float64(count))
metrics.TunnelPerDestinationChain.WithLabelValues(destinationChain).Inc()
})
}

Expand All @@ -104,9 +96,9 @@ func IncTxCount(tunnelID uint64) {
}

// ObserveTxProcessTime tracks transaction processing time in seconds with millisecond precision.
func ObserveTxProcessTime(chainName string, taskExecutionTime float64) {
func ObserveTxProcessTime(chainName string, txProcessTime float64) {
updateMetrics(func() {
metrics.TxProcessTime.WithLabelValues(chainName).Observe(taskExecutionTime)
metrics.TxProcessTime.WithLabelValues(chainName).Observe(txProcessTime)
})
}

Expand All @@ -119,18 +111,12 @@ func ObserveGasUsed(tunnelID uint64, gasUsed uint64) {

func NewPrometheusMetrics() *PrometheusMetrics {
tunnelLabels := []string{"tunnel_id"}
txCountLabels := []string{"tunnel_id"}
txProcessTimeLabels := []string{"chain_name"}
gasUsedLabels := []string{"tunnel_id"}
chainNameLabels := []string{"chain_name"}

registry := prometheus.NewRegistry()
registerer := promauto.With(registry)
metrics = &PrometheusMetrics{
Registry: registry,
TunnelCount: registerer.NewCounter(prometheus.CounterOpts{
Name: "falcon_tunnel_count_total",
Help: "Total number of observed tunnels",
}),
PacketReceived: registerer.NewCounterVec(prometheus.CounterOpts{
Name: "falcon_packet_received_total",
Help: "Total number of packets received",
Expand All @@ -139,7 +125,7 @@ func NewPrometheusMetrics() *PrometheusMetrics {
Name: "falcon_unrelayed_packet_count",
Help: "Number of unrelayed packets",
}, tunnelLabels),
TasksCount: registerer.NewCounterVec(prometheus.CounterOpts{
TaskCount: registerer.NewCounterVec(prometheus.CounterOpts{
Name: "falcon_task_count_total",
Help: "Total number of observed tasks",
}, tunnelLabels),
Expand All @@ -152,18 +138,18 @@ func NewPrometheusMetrics() *PrometheusMetrics {
0.99: 0.001,
},
}, tunnelLabels),
DestinationChainCount: registerer.NewCounter(prometheus.CounterOpts{
Name: "falcon_destination_chain_count_total",
TunnelPerDestinationChain: registerer.NewCounterVec(prometheus.CounterOpts{
Name: "falcon_tunnel_per_destination_chain",
Help: "Total number of destination chains",
}),
}, chainNameLabels),
ActiveTargetContract: registerer.NewGauge(prometheus.GaugeOpts{
Name: "falcon_active_target_chain_contract_count",
Help: "Number of active target chain contracts",
}),
TxCount: registerer.NewCounterVec(prometheus.CounterOpts{
Name: "falcon_tx_count_total",
Help: "Total number of transactions per tunnel",
}, txCountLabels),
}, tunnelLabels),
TxProcessTime: registerer.NewSummaryVec(prometheus.SummaryOpts{
Name: "falcon_tx_process_time",
Help: "Transaction processing time in milliseconds",
Expand All @@ -172,7 +158,7 @@ func NewPrometheusMetrics() *PrometheusMetrics {
0.9: 0.01,
0.99: 0.001,
},
}, txProcessTimeLabels),
}, chainNameLabels),
GasUsed: registerer.NewSummaryVec(prometheus.SummaryOpts{
Name: "falcon_gas_used_per_tx",
Help: "Gas used per transaction",
Expand All @@ -181,7 +167,7 @@ func NewPrometheusMetrics() *PrometheusMetrics {
0.9: 0.01,
0.99: 0.001,
},
}, gasUsedLabels),
}, tunnelLabels),
}
return metrics
}
Expand Down
8 changes: 3 additions & 5 deletions relayer/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,17 +570,13 @@ func (a *App) Start(

// initialize the tunnel relayer
tunnelRelayers := []*TunnelRelayer{}
// track destination chain names
chainNames := make(map[string]bool)

for _, tunnel := range tunnels {
chainProvider, ok := a.TargetChains[tunnel.TargetChainID]
if !ok {
return fmt.Errorf("target chain provider not found: %s", tunnel.TargetChainID)
}

chainNames[tunnel.TargetChainID] = true

tr := NewTunnelRelayer(
a.Log,
tunnel.ID,
Expand All @@ -590,6 +586,9 @@ func (a *App) Start(
chainProvider,
)
tunnelRelayers = append(tunnelRelayers, &tr)

// update the metric for the number of tunnels per destination chain
relayermetrics.IncTunnelPerDestinationChain(tunnel.TargetChainID)
}

// start the tunnel relayers
Expand All @@ -605,7 +604,6 @@ func (a *App) Start(
isSyncTunnelsAllowed,
a.BandClient,
a.TargetChains,
chainNames,
)

return scheduler.Start(ctx)
Expand Down
20 changes: 4 additions & 16 deletions relayer/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Scheduler struct {

BandClient band.Client
ChainProviders chains.ChainProviders
ChainNames map[string]bool
}

// NewScheduler creates a new Scheduler
Expand All @@ -44,7 +43,6 @@ func NewScheduler(
isSyncTunnelsAllowed bool,
bandClient band.Client,
chainProviders chains.ChainProviders,
chainNames map[string]bool,
) *Scheduler {
return &Scheduler{
Log: log,
Expand All @@ -59,7 +57,6 @@ func NewScheduler(
penaltyTaskCh: make(chan Task, penaltyTaskChSize),
BandClient: bandClient,
ChainProviders: chainProviders,
ChainNames: chainNames,
}
}

Expand All @@ -71,9 +68,6 @@ func (s *Scheduler) Start(ctx context.Context) error {
syncTunnelTicker := time.NewTicker(s.SyncTunnelsInterval)
defer syncTunnelTicker.Stop()

relayermetrics.AddTunnellCount(uint64(len(s.TunnelRelayers)))
relayermetrics.AddDestinationChainCount(uint64(len(s.ChainNames)))

// execute once we start the scheduler.
s.Execute(ctx)

Expand Down Expand Up @@ -120,7 +114,7 @@ func (s *Scheduler) Execute(ctx context.Context) {
go s.TriggerTunnelRelayer(ctx, task)

// record metrics for the task execution for the current tunnel relayer
relayermetrics.IncTasksCount(tr.TunnelID)
relayermetrics.IncTaskCount(tr.TunnelID)
}
}

Expand Down Expand Up @@ -190,9 +184,6 @@ func (s *Scheduler) SyncTunnels(ctx context.Context) {
return
}

oldTunnelRelayerCount := len(s.TunnelRelayers)
oldDestinationChainCount := len(s.ChainNames)

for i := s.BandLatestTunnel; i < len(tunnels); i++ {
chainProvider, ok := s.ChainProviders[tunnels[i].TargetChainID]
if !ok {
Expand All @@ -213,11 +204,12 @@ func (s *Scheduler) SyncTunnels(ctx context.Context) {
chainProvider,
)

s.ChainNames[tunnels[i].TargetChainID] = true

s.TunnelRelayers = append(s.TunnelRelayers, &tr)
s.isErrorOnHolds = append(s.isErrorOnHolds, false)

// update the metric for the number of tunnels per destination chain
relayermetrics.IncTunnelPerDestinationChain(tunnels[i].TargetChainID)

s.Log.Info(
"New tunnel synchronized successfully",
zap.String("chain_name", tunnels[i].TargetChainID),
Expand All @@ -226,10 +218,6 @@ func (s *Scheduler) SyncTunnels(ctx context.Context) {
}

s.BandLatestTunnel = len(tunnels)

// update metrics for the number of destination chains and tunnels after synchronization
relayermetrics.AddDestinationChainCount(uint64(len(s.ChainNames) - oldDestinationChainCount))
relayermetrics.AddTunnellCount(uint64(len(s.TunnelRelayers) - oldTunnelRelayerCount))
}

// calculatePenaltyInterval applies exponential backoff with a max limit
Expand Down

0 comments on commit 5c8f7da

Please sign in to comment.