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

未關閉
#8,552 0 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

維護者通常 1 天內回覆

@MajorLift 已經在處理了。

開始於 2026年9月21日。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
50/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
冷清
技術堆疊
typescript
領域
observability

研究方向

從 packages/base-data-service/src/BaseDataService.ts 開始,追蹤其內部 QueryClient 的建立與使用方式。評估 getter 和 constructor-hook 選項,接著驗證下游的 queryCache.subscribe wiring 能夠觀察 cache 與 request 的生命週期,同時不改變 UI surface、OmitKeyof 行為、messenger contract 或 proxy queryFn 語義。

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
TypeScript
星號
424
分支
311
平均合併
1 天 8 小時
30 天內合併 PR
446

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 有 Pull Request 範本
  • 沒有貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

MetaMask/core 的其他 Issue

查看 MetaMask/core 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。