feat(network-controller): emit RPC service analytics via AnalyticsController#9270
Open
cryptodev-2s wants to merge 3 commits into
Open
feat(network-controller): emit RPC service analytics via AnalyticsController#9270cryptodev-2s wants to merge 3 commits into
cryptodev-2s wants to merge 3 commits into
Conversation
…troller NetworkController now emits "RPC Service Unavailable" and "RPC Service Degraded" analytics events itself through the AnalyticsController:trackEvent action, instead of relying on each client app to translate its rpcEndpointUnavailable/rpcEndpointDegraded events into MetaMetrics. The feature is opt in via a new optional `analytics` constructor option. When omitted, no analytics are emitted and AnalyticsController is never called. The two client specific pieces stay injected because core cannot know them: `isRpcEndpointUrlPublic` (depends on the client network lists) and `rpcServiceEventsSampleRate` (depends on the build environment). The generic property building, event naming, connection error skipping and delivery now live in core (src/rpc-service-events.ts), so both clients can drop their duplicated handlers in follow up PRs.
The build tsconfig flagged passing the `unknown` catch variable to `captureException`, which expects an `Error`. Cast it, matching the other captureException call sites. Also regenerate the root README dependency graph to include the new analytics-controller edge.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Explanation
Today
NetworkControlleremits no analytics. It publishesrpcEndpointUnavailableandrpcEndpointDegradedevents, and each client app independently subscribes to them and turns them into theRPC Service Unavailable/RPC Service DegradedSegment events. The extension still routes those throughMetaMetricsController; mobile already routes them throughAnalyticsController. The handlers are near identical duplication across both repos.This moves that translation into
NetworkControllerso it is written once and delivered through theAnalyticsController:trackEventaction over the messenger (which is the whole reasonAnalyticsControllerexists). Clients can then delete their duplicated handlers and just wire the new option.The feature is fully opt in via a new optional
analyticsconstructor option. When omitted, no analytics are emitted andAnalyticsControlleris never called, so existing consumers are unaffected. The two genuinely client specific pieces are injected because core cannot know them:isRpcEndpointUrlPublic(url): whether an endpoint URL is safe to report verbatim (depends on the client network lists, Quicknode URLs, Infura key)rpcServiceEventsSampleRate: the proportion of events to emit (depends on the build environment, typically 1% in production and 100% in dev)Core reads
analyticsIdviaAnalyticsController:getState, applies the sample rate withgenerateDeterministicRandomNumber, skips local connection errors, builds the properties, and delivers the event.AnalyticsControllerkeeps owning consent gating.What changed
src/rpc-service-events.ts: pure helpers (sanitizeRpcEndpointUrl,buildRpcServiceEventProperties,toAnalyticsTrackingEvent) plus theNetworkControllerAnalyticsOptionsandRpcServiceEventNametypessrc/NetworkController.ts: new optionalanalyticsoption, widenedAllowedActionswithAnalyticsController:getState+AnalyticsController:trackEvent, two guarded self subscriptions, and a private#trackRpcServiceEvent@metamask/analytics-controllerdependency and tsconfig references (no dependency cycle)index.ts,tests/helpers.ts,CHANGELOG.md, and a newtests/NetworkController.analytics.test.tsOpen question
AnalyticsTrackingEventhas nocategoryfield, so the oldcategory: 'Network'is not carried for now. Confirming with the analytics owners how they want category mapped before relying on it downstream.Follow ups (separate repos)
messenger-action-handlers.ts+utils.ts, pass theanalyticsoption, delegate the two AnalyticsController actions. This is where the extension stops usingMetaMetricsControllerfor these events.References
N/A
Checklist
Note
Medium Risk
Opt-in analytics on RPC failure paths with URL redaction and sampling, but new cross-controller messenger coupling and consumers must delegate AnalyticsController actions when enabling the feature.
Overview
NetworkController can now turn existing
rpcEndpointUnavailable/rpcEndpointDegradedsignals into RPC Service Unavailable and RPC Service Degraded analytics in one place, via optional constructoranalyticsandAnalyticsController:trackEvent(opt-in; default behavior unchanged).When
analyticsis set, the controller subscribes to those RPC events and runs#trackRpcServiceEvent: skip local connection errors, requireanalyticsIdfromAnalyticsController:getState, applyrpcServiceEventsSampleRatewithgenerateDeterministicRandomNumber, then track. Clients injectisRpcEndpointUrlPublicand the sample rate; messenger must delegateAnalyticsController:getStateandAnalyticsController:trackEvent.New
rpc-service-eventshelpers sanitize endpoint URLs (host vs'custom'), build event properties (CAIP chain id, optional degraded fields,http_status), and wrapAnalyticsTrackingEvent.@metamask/analytics-controlleris added as a dependency; types are exported from the package index. Tests cover helpers and controller analytics wiring (including no-op paths andcaptureExceptionon failures).Reviewed by Cursor Bugbot for commit 8697e1f. Bugbot is set up for automated code reviews on this repo. Configure here.