-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Trajan0x <[email protected]> Co-authored-by: Daniel Wasserman <[email protected]>
- Loading branch information
1 parent
31b5f8f
commit 9779cda
Showing
6 changed files
with
155 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package listener | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/synapsecns/sanguine/core/metrics" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
const meterName = "github.com/synapsecns/sanguine/ethergo/listener" | ||
|
||
// generate an interface for otelRecorder that exports the public method. | ||
// this allows us to avoid using recordX externally anad makes the package less confusing. | ||
// | ||
// ============================================================================= | ||
// ============================================================================= | ||
// IMPORTANT: DO NOT REMOVE THIS COMMENT. | ||
// NOTICE: PLEASE MAKE SURE YOU UPDATE BOTH THE DOCS AND THE GRAFANA DASHBOARD (IF NEEDED) AFTER UPDATING METRICS. | ||
// ============================================================================= | ||
// ============================================================================= | ||
// | ||
//go:generate go run github.com/vburenin/ifacemaker -f otel.go -s otelRecorder -i iOtelRecorder -p listener -o otel_generated.go -c "autogenerated file" | ||
type otelRecorder struct { | ||
metrics metrics.Handler | ||
// meter is the metrics meter. | ||
meter metric.Meter | ||
// lastBlockGauge is the gauge for the last block. | ||
lastBlockGauge metric.Int64ObservableGauge | ||
// lastFetchedBlockAgeGauge is the gauge for the last block age. | ||
lastFetchedBlockAgeGauge metric.Float64ObservableGauge | ||
// lastBlock is the last block processed by the listener. | ||
lastBlock *uint64 | ||
// lastBlockFetchTime is the time the last block was fetched (used to calculate last block age). | ||
lastBlockFetchTime *time.Time | ||
// chainID is the chain ID for the listener. | ||
chainID int | ||
} | ||
|
||
func newOtelRecorder(meterHandler metrics.Handler, chainID int) (_ iOtelRecorder, err error) { | ||
or := otelRecorder{ | ||
metrics: meterHandler, | ||
meter: meterHandler.Meter(meterName), | ||
lastBlock: nil, | ||
lastBlockFetchTime: nil, | ||
chainID: chainID, | ||
} | ||
|
||
or.lastBlockGauge, err = or.meter.Int64ObservableGauge("last_block") | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create last block gauge") | ||
} | ||
|
||
or.lastFetchedBlockAgeGauge, err = or.meter.Float64ObservableGauge("last_block_age") | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create last block age gauge") | ||
} | ||
|
||
_, err = or.meter.RegisterCallback(or.recordLastBlock, or.lastBlockGauge) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not register callback for last block gauge") | ||
} | ||
|
||
_, err = or.meter.RegisterCallback(or.recordLastFetchedBlockAge, or.lastFetchedBlockAgeGauge) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not register callback for last block age gauge") | ||
} | ||
|
||
return &or, nil | ||
} | ||
|
||
func (o *otelRecorder) recordLastBlock(_ context.Context, observer metric.Observer) (err error) { | ||
if o.metrics == nil || o.lastBlockGauge == nil || o.lastBlock == nil { | ||
return nil | ||
} | ||
|
||
opts := metric.WithAttributes( | ||
attribute.Int(metrics.ChainID, o.chainID), | ||
) | ||
observer.ObserveInt64(o.lastBlockGauge, int64(*o.lastBlock), opts) | ||
|
||
return nil | ||
} | ||
|
||
func (o *otelRecorder) recordLastFetchedBlockAge(_ context.Context, observer metric.Observer) (err error) { | ||
if o.metrics == nil || o.lastFetchedBlockAgeGauge == nil || o.lastBlockFetchTime == nil { | ||
return nil | ||
} | ||
|
||
age := time.Since(*o.lastBlockFetchTime).Seconds() | ||
opts := metric.WithAttributes( | ||
attribute.Int(metrics.ChainID, o.chainID), | ||
) | ||
observer.ObserveFloat64(o.lastFetchedBlockAgeGauge, age, opts) | ||
|
||
return nil | ||
} | ||
|
||
// RecordLastBlock records the last block processed by the listener. | ||
func (o *otelRecorder) RecordLastBlock(lastBlock uint64) { | ||
// verify if the last block has changed | ||
var hasChanged bool | ||
if o.lastBlock == nil { | ||
hasChanged = true | ||
} else { | ||
hasChanged = *o.lastBlock != lastBlock | ||
} | ||
if !hasChanged { | ||
return | ||
} | ||
|
||
// record the block | ||
o.lastBlock = &lastBlock | ||
fetchTime := time.Now() | ||
o.lastBlockFetchTime = &fetchTime | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters