-
Notifications
You must be signed in to change notification settings - Fork 3
use metrics.RPCClientMetrics in multinode #97
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
Open
guandali
wants to merge
3
commits into
main
Choose a base branch
from
lli/integrate-rpc-metric
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−22
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,13 +9,27 @@ import ( | |
|
|
||
| "github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
| "github.com/smartcontractkit/chainlink-common/pkg/services" | ||
| frameworkmetrics "github.com/smartcontractkit/chainlink-framework/metrics" | ||
| ) | ||
|
|
||
| var errInvalidHead = errors.New("invalid head") | ||
|
|
||
| const ( | ||
| rpcCallNameLatestBlock = "latest_block" | ||
| rpcCallNameLatestFinalizedBlock = "latest_finalized_block" | ||
| ) | ||
|
|
||
| type RPCClientBaseConfig interface { | ||
| NewHeadsPollInterval() time.Duration | ||
| FinalizedBlockPollInterval() time.Duration | ||
| } | ||
|
|
||
| type RPCClientBaseMetricsConfig struct { | ||
| RPCClientMetrics frameworkmetrics.RPCClientMetrics | ||
| RPCURL string | ||
| IsSendOnly bool | ||
| } | ||
|
|
||
| // RPCClientBase is used to integrate multinode into chain-specific clients. | ||
| // For new MultiNode integrations, we wrap the RPC client and inherit from the RPCClientBase | ||
| // to get the required RPCClient methods and enable the use of MultiNode. | ||
|
|
@@ -46,14 +60,19 @@ type RPCClientBase[HEAD Head] struct { | |
| highestUserObservations ChainInfo | ||
| // most recent chain info observed during current lifecycle | ||
| latestChainInfo ChainInfo | ||
|
|
||
| rpcMetrics frameworkmetrics.RPCClientMetrics | ||
| rpcURL string | ||
| isSendOnly bool | ||
| } | ||
|
|
||
| func NewRPCClientBase[HEAD Head]( | ||
| cfg RPCClientBaseConfig, ctxTimeout time.Duration, log logger.Logger, | ||
| latestBlock func(ctx context.Context) (HEAD, error), | ||
| latestFinalizedBlock func(ctx context.Context) (HEAD, error), | ||
| rpcMetrics *RPCClientBaseMetricsConfig, | ||
|
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. Here I think it makes more sense to accept an already |
||
| ) *RPCClientBase[HEAD] { | ||
| return &RPCClientBase[HEAD]{ | ||
| base := &RPCClientBase[HEAD]{ | ||
| cfg: cfg, | ||
| log: log, | ||
| ctxTimeout: ctxTimeout, | ||
|
|
@@ -62,6 +81,12 @@ func NewRPCClientBase[HEAD Head]( | |
| subs: make(map[Subscription]struct{}), | ||
| lifeCycleCh: make(chan struct{}), | ||
| } | ||
| if rpcMetrics != nil { | ||
| base.rpcMetrics = rpcMetrics.RPCClientMetrics | ||
| base.rpcURL = rpcMetrics.RPCURL | ||
| base.isSendOnly = rpcMetrics.IsSendOnly | ||
| } | ||
| return base | ||
| } | ||
|
|
||
| func (m *RPCClientBase[HEAD]) lenSubs() int { | ||
|
|
@@ -155,37 +180,53 @@ func (m *RPCClientBase[HEAD]) LatestBlock(ctx context.Context) (HEAD, error) { | |
| // capture lifeCycleCh to ensure we are not updating chainInfo with observations related to previous life cycle | ||
| ctx, cancel, lifeCycleCh := m.AcquireQueryCtx(ctx, m.ctxTimeout) | ||
| defer cancel() | ||
| start := time.Now() | ||
|
|
||
| head, err := m.latestBlock(ctx) | ||
| if err != nil { | ||
| m.recordRPCRequest(ctx, rpcCallNameLatestBlock, start, err) | ||
| return head, err | ||
| } | ||
|
|
||
| if !head.IsValid() { | ||
| return head, errors.New("invalid head") | ||
| m.recordRPCRequest(ctx, rpcCallNameLatestBlock, start, errInvalidHead) | ||
| return head, errInvalidHead | ||
| } | ||
|
|
||
| m.recordRPCRequest(ctx, rpcCallNameLatestBlock, start, nil) | ||
| m.OnNewHead(ctx, lifeCycleCh, head) | ||
| return head, nil | ||
| } | ||
|
|
||
| func (m *RPCClientBase[HEAD]) LatestFinalizedBlock(ctx context.Context) (HEAD, error) { | ||
| ctx, cancel, lifeCycleCh := m.AcquireQueryCtx(ctx, m.ctxTimeout) | ||
| defer cancel() | ||
| start := time.Now() | ||
|
|
||
| head, err := m.latestFinalizedBlock(ctx) | ||
| if err != nil { | ||
| m.recordRPCRequest(ctx, rpcCallNameLatestFinalizedBlock, start, err) | ||
| return head, err | ||
| } | ||
|
|
||
| if !head.IsValid() { | ||
| return head, errors.New("invalid head") | ||
| m.recordRPCRequest(ctx, rpcCallNameLatestFinalizedBlock, start, errInvalidHead) | ||
| return head, errInvalidHead | ||
| } | ||
|
|
||
| m.recordRPCRequest(ctx, rpcCallNameLatestFinalizedBlock, start, nil) | ||
| m.OnNewFinalizedHead(ctx, lifeCycleCh, head) | ||
| return head, nil | ||
| } | ||
|
|
||
| func (m *RPCClientBase[HEAD]) recordRPCRequest(ctx context.Context, callName string, startedAt time.Time, err error) { | ||
| if m.rpcMetrics == nil { | ||
| return | ||
| } | ||
|
|
||
| m.rpcMetrics.RecordRequest(ctx, m.rpcURL, m.isSendOnly, callName, time.Since(startedAt), err) | ||
| } | ||
|
|
||
| func (m *RPCClientBase[HEAD]) OnNewHead(ctx context.Context, requestCh <-chan struct{}, head HEAD) { | ||
| if !head.IsValid() { | ||
| return | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It doesn't make sense IMO to create a config that contains a
RPCClientMetricswhich the caller needs to construct with aRPCClientMetricsConfigthat itself contains the chainFamily and ID.I think it is a worthy change to have the caller call an interface instead of the metric directly so we can take care of things like moving from promauto to beholder and other details, but we should have the rpc_client care about it the least possible amount, and that'd be simply providing only the necessary config once.
So, if the URL and SendOnly are constant throughout the lifetime of the client, they can be part of
RPCClientMetricsConfig, and that's it.