-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[rfq] metrics, hotfixes #2854
[rfq] metrics, hotfixes #2854
Changes from 4 commits
bd22f55
a15576b
c1415fd
224190e
365b58e
2543711
4fd7090
a5d46a2
849ea6a
e0e1354
e64943b
a1900b6
b59be93
03d95f9
4afaee1
60b0cf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,117 @@ | ||||
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" | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused constant The constant - const meterName = "github.com/synapsecns/sanguine/ethergo/listener" Committable suggestion
Suggested change
ToolsGitHub Check: Lint (ethergo)
|
||||
|
||||
// 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 | ||||
// lastBlockAgeGauge is the gauge for the last block age. | ||||
lastBlockAgeGauge 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 uint32 | ||||
} | ||||
|
||||
func newOtelRecorder(meterHandler metrics.Handler) (_ iOtelRecorder, err error) { | ||||
or := otelRecorder{ | ||||
metrics: meterHandler, | ||||
meter: meterHandler.Meter(meterName), | ||||
lastBlock: nil, | ||||
lastBlockFetchTime: nil, | ||||
} | ||||
|
||||
or.lastBlockGauge, err = or.meter.Int64ObservableGauge("last_block") | ||||
if err != nil { | ||||
return nil, fmt.Errorf("could not create last block gauge") | ||||
} | ||||
|
||||
or.lastBlockAgeGauge, 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.recordLastBlockAge, or.lastBlockAgeGauge) | ||||
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 | ||||
} | ||||
Comment on lines
+74
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for error handling. The error handling blocks in the Do you want me to generate the unit testing code or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||||
|
||||
opts := metric.WithAttributes( | ||||
attribute.Int(metrics.ChainID, int(o.chainID)), | ||||
) | ||||
observer.ObserveInt64(o.lastBlockGauge, int64(*o.lastBlock), opts) | ||||
|
||||
return nil | ||||
} | ||||
|
||||
func (o *otelRecorder) recordLastBlockAge(_ context.Context, observer metric.Observer) (err error) { | ||||
if o.metrics == nil || o.lastBlockAgeGauge == nil || o.lastBlockFetchTime == nil { | ||||
return nil | ||||
} | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for error handling. The error handling blocks in the Do you want me to generate the unit testing code or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||||
|
||||
age := time.Since(*o.lastBlockFetchTime).Seconds() | ||||
opts := metric.WithAttributes( | ||||
attribute.Int(metrics.ChainID, int(o.chainID)), | ||||
) | ||||
observer.ObserveFloat64(o.lastBlockAgeGauge, age, opts) | ||||
|
||||
return nil | ||||
} | ||||
|
||||
// RecordLastBlock records the last block processed by the listener. | ||||
func (o *otelRecorder) RecordLastBlock(lastBlock uint64) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧠 logic: The logic for checking if the last block has changed seems inverted. It should be |
||||
// 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.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -387,13 +387,7 @@ | |||||||||
} else { | ||||||||||
// process in parallel (new goroutine) | ||||||||||
request := req // capture func literal | ||||||||||
ok := r.semaphore.TryAcquire(1) | ||||||||||
if !ok { | ||||||||||
span.AddEvent("could not acquire semaphore", trace.WithAttributes( | ||||||||||
attribute.String("transaction_id", hexutil.Encode(request.TransactionID[:])), | ||||||||||
)) | ||||||||||
continue | ||||||||||
} | ||||||||||
err = r.semaphore.Acquire(ctx, 1) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding context with timeout for semaphore acquisition. To avoid potential deadlocks, consider using a context with timeout when acquiring the semaphore. - err = r.semaphore.Acquire(ctx, 1)
+ acquireCtx, cancel := context.WithTimeout(ctx, time.Second * 30)
+ defer cancel()
+ err = r.semaphore.Acquire(acquireCtx, 1) Committable suggestion
Suggested change
|
||||||||||
if err != nil { | ||||||||||
return fmt.Errorf("could not acquire semaphore: %w", err) | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tests for recording the last block.
The new code for recording the last block using the
otelRecorder
in thedoPoll
function is not covered by tests. Ensure that this case is tested to improve code reliability.Do you want me to generate the unit testing code or open a GitHub issue to track this task?
Tools
GitHub Check: codecov/patch