Skip to content

Commit

Permalink
Refactor Execution Client Status metric
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-ssvlabs committed Nov 15, 2024
1 parent b63b0b1 commit 40b1abe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
17 changes: 5 additions & 12 deletions eth/executionclient/execution_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ func (ec *ExecutionClient) Healthy(ctx context.Context) error {
start := time.Now()
sp, err := ec.client.SyncProgress(ctx)
if err != nil {
clientStatusGauge.Record(ctx, 1,
metric.WithAttributes(executionClientAddrAttribute(ec.nodeAddr)),
metric.WithAttributes(executionClientStatusAttribute(statusFailure)),
)
recordExecutionClientStatus(ctx, statusFailure, ec.nodeAddr)

return err
}
Expand All @@ -244,10 +241,8 @@ func (ec *ExecutionClient) Healthy(ctx context.Context) error {
)

if sp != nil {
clientStatusGauge.Record(ctx, 1,
metric.WithAttributes(executionClientAddrAttribute(ec.nodeAddr)),
metric.WithAttributes(executionClientStatusAttribute(statusSyncing)),
)
recordExecutionClientStatus(ctx, statusSyncing, ec.nodeAddr)

syncingDistance := sp.HighestBlock - sp.CurrentBlock
if syncingDistance <= math.MaxInt64 {
syncingDistanceGauge.Record(ctx, int64(syncingDistance), metric.WithAttributes(executionClientAddrAttribute(ec.nodeAddr)))
Expand All @@ -256,10 +251,8 @@ func (ec *ExecutionClient) Healthy(ctx context.Context) error {
return fmt.Errorf("syncing")
}

clientStatusGauge.Record(ctx, 1,
metric.WithAttributes(executionClientAddrAttribute(ec.nodeAddr)),
metric.WithAttributes(executionClientStatusAttribute(statusReady)),
)
recordExecutionClientStatus(ctx, statusReady, ec.nodeAddr)

syncingDistanceGauge.Record(ctx, 0, metric.WithAttributes(executionClientAddrAttribute(ec.nodeAddr)))

return nil
Expand Down
30 changes: 25 additions & 5 deletions eth/executionclient/observability.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package executionclient

import (
"context"
"fmt"

"github.com/ssvlabs/ssv/observability"
Expand All @@ -17,14 +18,13 @@ const (
type executionClientStatus string

const (
statusSyncing = "syncing"
statusFailure = "failure"
statusReady = "ready"
statusSyncing executionClientStatus = "syncing"
statusFailure executionClientStatus = "failure"
statusReady executionClientStatus = "ready"
)

var (
meter = otel.Meter(observabilityComponentName)

meter = otel.Meter(observabilityComponentName)
latencyHistogram = observability.GetMetric(
fmt.Sprintf("%s.latency.duration", observabilityComponentNamespace),
func(metricName string) (metric.Float64Histogram, error) {
Expand Down Expand Up @@ -76,3 +76,23 @@ func executionClientStatusAttribute(value executionClientStatus) attribute.KeyVa
eventNameAttrName := fmt.Sprintf("%s.status", observabilityComponentNamespace)
return attribute.String(eventNameAttrName, string(value))
}

func recordExecutionClientStatus(ctx context.Context, status executionClientStatus, nodeAddr string) {
clientStatusGauge.Record(ctx, 0,
metric.WithAttributes(executionClientAddrAttribute(nodeAddr)),
metric.WithAttributes(executionClientStatusAttribute(statusReady)),
)
clientStatusGauge.Record(ctx, 0,
metric.WithAttributes(executionClientAddrAttribute(nodeAddr)),
metric.WithAttributes(executionClientStatusAttribute(statusSyncing)),
)
clientStatusGauge.Record(ctx, 0,
metric.WithAttributes(executionClientAddrAttribute(nodeAddr)),
metric.WithAttributes(executionClientStatusAttribute(statusFailure)),
)

clientStatusGauge.Record(ctx, 1,
metric.WithAttributes(executionClientAddrAttribute(nodeAddr)),
metric.WithAttributes(executionClientStatusAttribute(status)),
)
}

0 comments on commit 40b1abe

Please sign in to comment.