Skip to content

Commit

Permalink
Feat: add listener_name as attribute (#3050)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwasse authored Aug 23, 2024
1 parent c1e56ba commit b65003c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ethergo/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type chainListener struct {
blockWait uint64
// otelRecorder is the recorder for the otel metrics.
otelRecorder iOtelRecorder
name string
}

var (
Expand Down Expand Up @@ -94,7 +95,7 @@ func (c *chainListener) Listen(ctx context.Context, handler HandleLog) (err erro
}

if c.otelRecorder == nil {
c.otelRecorder, err = newOtelRecorder(c.handler, int(c.chainID))
c.otelRecorder, err = newOtelRecorder(c.handler, int(c.chainID), c.name)
if err != nil {
return fmt.Errorf("could not create otel recorder: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions ethergo/listener/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func WithBlockWait(wait uint64) Option {
// WithName sets the listener name.
func WithName(name string) Option {
return func(c *chainListener) {
c.name = name
c.store.SetListenerName(name)
}
}
7 changes: 6 additions & 1 deletion ethergo/listener/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ type otelRecorder struct {
lastBlockFetchTime *time.Time
// chainID is the chain ID for the listener.
chainID int
// listenerName is the name of the listener.
listenerName string
}

func newOtelRecorder(meterHandler metrics.Handler, chainID int) (_ iOtelRecorder, err error) {
func newOtelRecorder(meterHandler metrics.Handler, chainID int, name string) (_ iOtelRecorder, err error) {
or := otelRecorder{
metrics: meterHandler,
meter: meterHandler.Meter(meterName),
lastBlock: nil,
lastBlockFetchTime: nil,
chainID: chainID,
listenerName: name,
}

or.lastBlockGauge, err = or.meter.Int64ObservableGauge("last_block")
Expand Down Expand Up @@ -78,6 +81,7 @@ func (o *otelRecorder) recordLastBlock(_ context.Context, observer metric.Observ

opts := metric.WithAttributes(
attribute.Int(metrics.ChainID, o.chainID),
attribute.String("listener_name", o.listenerName),
)
observer.ObserveInt64(o.lastBlockGauge, int64(*o.lastBlock), opts)

Expand All @@ -92,6 +96,7 @@ func (o *otelRecorder) recordLastFetchedBlockAge(_ context.Context, observer met
age := time.Since(*o.lastBlockFetchTime).Seconds()
opts := metric.WithAttributes(
attribute.Int(metrics.ChainID, o.chainID),
attribute.String("listener_name", o.listenerName),
)
observer.ObserveFloat64(o.lastFetchedBlockAgeGauge, age, opts)

Expand Down

0 comments on commit b65003c

Please sign in to comment.