[P2] `QueryClient` distributed-tracing observability in `BaseDataService`

Offen
#8,552 0 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@MajorLift arbeitet bereits daran.

Seit 21.9.2026.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
50/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Ruhig
Tech-Stack
typescript
Bereich
observability

Rechercherichtung

Beginne mit packages/base-data-service/src/BaseDataService.ts und verfolge, wie der interne QueryClient erstellt und verwendet wird. Bewerte die Optionen für einen Getter und einen Konstruktor-Hook und verifiziere anschließend, dass die nachgelagerte queryCache.subscribe-Verdrahtung den Cache- und Request-Lebenszyklus beobachten kann, ohne die UI-Oberfläche, das OmitKeyof-Verhalten, den Messenger-Vertrag oder die Semantik von proxy queryFn zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

for-migration mig-epic-42-child

Parent Epic: None (standalone)
File: packages/base-data-service/src/BaseDataService.ts
Size: S | Hours: 2–4


Problem

Option A's proxy model (TanStack Query QueryClient in the background process, proxy queryFn in the UI) intentionally hides the cache, dedupe, retry, fetch, and sync chain from UI-perceived latency tracing. The distributed-tracing wrappers in metamask-extension#39891 give us the call boundaries (useQuery → submitRequestToBackground → messenger → BaseDataService action → response). Inside the opaque region, every interesting decision is invisible:

  • Cache hit vs. miss. Most data service calls don't make a network request. Auto-instrumented fetch spans see zero. The query looks "slow" with no attribution.
  • In-flight dedupe. N concurrent observers collapse to one fetch. The fetch span doesn't know N.
  • Cockatiel retry sequences. A single logical "fetch this" can produce 0, 1, or many HTTP requests. Auto-spans see them as unrelated. Backoff intervals are invisible.
  • Circuit breaker state. When open, no fetch happens. The query fails or returns cached data with no auto-span at all.
  • Cache sync to UI. setData → cacheUpdated event → Port.postMessage → UI hydrate is the post-fetch synchronization cost. None of it surfaces as a span.
  • Trigger attribution. Poll interval, user invalidation, observer subscribe, and WebSocket invalidation are all identical from the network's view.

Three Option A tradeoffs documented in decisions#131 currently depend on observability that doesn't exist:

  • Per-screen staleTime. Validating that the service-side workarounds hold in production requires distinguishing fetches from cache serves.
  • refetchOnMount / refetchOnWindowFocus collapse. Measuring whether concurrent queries amplify messenger volume or are absorbed by background staleTime.
  • Split-process staleness indirection. Correlating UI proxy calls with background fetches.

Without this visibility, the tradeoffs are theoretical. With it, they're verifiable.


Solution

Add a sanctioned extension point to BaseDataService that exposes the internal QueryClient for read-only observability subscriptions. Two variants; preference is maintainer's call:

  1. Public getter. get queryClient(): QueryClient
  2. Constructor callback. onCreate?: (qc: QueryClient) => void

Either is ~3 lines in core. Downstream clients attach their own instrumentation externally (e.g. qc.getQueryCache().subscribe(emitSpan) plus Cockatiel onRetry / onBreak handlers in extension's Sentry init, ~50 lines). The full span chain becomes stitchable end-to-end:

useQuery → submitRequestToBackground (span) → messenger (span) →
  [BaseDataService.fetchQuery — cache miss, observerAdded → fetching →
   Cockatiel attempt 1 → fetch (auto, child of rpc.handler) → 503 →
   backoff 200ms → fetch (auto) → 200 → setData → success] →
cacheUpdated → port → UI hydrate

Acceptance Criteria

  • BaseDataService exposes either a public queryClient getter or an onCreate constructor hook (maintainer's choice).
  • Downstream queryCache.subscribe(...) wiring composes cleanly from a client-side Sentry init file. A prototype in the extension repo demonstrates the full stack trace from useQuery through retry through success.
  • No new UI-facing surface.
  • No change to OmitKeyof type erasure.
  • No change to messenger contract.
  • No change to proxy queryFn semantics.

Labels

area-data-service, area-observability


Dependencies

Related: #8530, #8531 (orthogonal — same epic scope but different surface; not blocking).
Depends on: none.
Dependents: none.

Vorherrschende Sprache
TypeScript
Sterne
424
Forks
311
Ø Merge
1 T. 8 Std.
Gemergte PRs (30 T.)
446

Entwicklungsumgebung

  • Kein Dockerfile und keine Docker-Compose-Datei
  • Hat eine Pull-Request-Vorlage
  • Kein Beitragsleitfaden

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus MetaMask/core

Alle Issues in MetaMask/core

Ähnliche Issues

Weitere Issues zu TypeScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.