[x-telemetry] Skip runtime resolution when telemetry is disabled#22078
Conversation
Bundle size
Deploy previewhttps://deploy-preview-22078--material-ui-x.netlify.app/ Check out the code infra dashboard for more information about this PR. |
LukasTy
left a comment
There was a problem hiding this comment.
LGTM. 👌
The 2nd and 3rd Claude observations could be worth checking.
Claude Opus 4.6 review
Summary
The fix is correct and well-motivated. The core change in sender.ts reorders the logic so the eligibility check happens before the expensive getTelemetryContext() call (which triggers fetch('/package.json')).
Correctness Analysis
The key question is: does shouldSendTelemetry() work correctly with the base context (before runtime resolution)?
Looking at what shouldSendTelemetry actually checks:
typeof window === 'undefined'— no context neededgetTelemetryEnvConfigValue('IS_COLLECTING')— no context needed (this is wheredisableTelemetry()sets the flag via settings.ts)telemetryContext.traits.isCI— set at postinstall time byci-info, never modified bygetTelemetryContext()
And getTelemetryContext() only adds: sessionId, anonymousId, runtimePackageNameHash, projectId, fingerprint — none of which are used by shouldSendTelemetry.
Additionally, since telemetryContext from ../context is a singleton (the same object getTelemetryContext() mutates and returns), baseTelemetryContext and the resolved context are literally the same reference. So the check is guaranteed to produce the same result either way.
Things that look good
- The payload still uses the fully resolved context (with
runtimePackageNameHash,fingerprint, etc.) — only the eligibility check was moved earlier - The new
beforeEachresets forruntimePackageNameHashandruntimePackageNameHashResolvedprevent test pollution from the new tests that set these tonull/false - Both disabling paths are tested (programmatic
disableTelemetry()and CI environment)
Minor observations (non-blocking)
-
Dynamic vs static import — The new
await import('../context')follows the existing pattern of dynamic imports insendMuiXTelemetryEvent. However,../contextis an extremely lightweight module (just a plain object, no side effects), and other lightweight modules likegetTelemetryEnvConfigValueandfetchWithRetryare already imported statically at the top of the file. A staticimport telemetryContext from '../context'would be simpler and avoid the async overhead on every call. Not critical since it's consistent with theget-contextimport pattern, but worth considering. -
Type import inconsistency —
TelemetryContextTypeis still imported from'./get-context'(line 1), while the value is now imported from'../context'. Both resolve to the same type (sinceget-contextre-exports it from../context), so no bug, but importing both from'../context'would be cleaner. -
Pre-existing test cleanup gap — The existing test "should not send telemetry when opted out via settings" (line 89) calls
disableTelemetry()but never cleans up. It only works because the next test happens to callenableTelemetry(). The PR's new test correctly callsenableTelemetry()at the end, which is good. Worth notingafterEachdoesn't reset the telemetry settings — fragile test ordering is a pre-existing issue, not introduced here.
Verdict
Approve — the fix is correct, the tests cover the important cases, and the change is minimal and focused. The minor observations above are non-blocking style preferences.
… github.com:aemartos/mui-x into aemartos/sto-113-telemetry-skip-fetch-when-disabled
|
@LukasTy suggestions addressed! |
Fixes #22070
Summary
shouldSendTelemetrybefore callinggetTelemetryContext()insender.ts, so disabled telemetry doesn't triggerfetch('/package.json')Context
When telemetry was disabled via
muiXTelemetrySettings.disableTelemetry(), env var, or CI environment, afetch('/package.json')request was still being made on every telemetry event. This caused issues in test environments that monitor unhandled requests.The root cause:
sender.tscalledgetTelemetryContext()(which resolvesruntimePackageNameHashviafetch('/package.json')) before checking whether telemetry should be sent.Fix
Reorder the checks in
sendMuiXTelemetryEvent:shouldSendTelemetrywith the base context, early exit if disabledgetTelemetryContext()to resolve runtime valuesThe base context already has everything
shouldSendTelemetryneeds (isCIis set at postinstall time), so no additional work is required for the eligibility check.