feat(observability): instrument SWR fetch caches so cache metrics reach ClickHouse - #411
Open
JonasJesus42 wants to merge 2 commits into
Open
feat(observability): instrument SWR fetch caches so cache metrics reach ClickHouse#411JonasJesus42 wants to merge 2 commits into
JonasJesus42 wants to merge 2 commits into
Conversation
…trics reach ClickHouse
Cache/upstream telemetry only reached ClickHouse for the edge + cachedLoader
layers; the apps' in-memory SWR fetch caches (VTEX, and any Magento site) were
dark. Add one shared, instrumented SWR cache and route all commerce apps
through it so hit/miss + upstream latency flow automatically.
- @decocms/blocks: new `sdk/fetchCache` (`createFetchCache`) emitting
`deco.cache.requests{layer="swr",profile=<provider>}` on HIT/STALE-HIT/MISS,
provider-isolated Maps; generalize CacheLayer `vtex-swr` -> `swr`.
- apps-vtex: fetchCache is now a thin binding over the shared util (no behavior
change, now emits metrics).
- apps-magento: `setMagentoFetch`/`createMagentoFetch` + operationRouter +
`magentoCachedFetch` (route egress through instrumented fetch + SWR cache).
- apps-salesforce: `createSalesforceFetch`; `createHttpClient` defaults its
fetcher to it so all loaders are instrumented automatically.
- Convention documented in CLAUDE.md + guardrail test in apps-commerce that
fails if a commerce app lacks an instrumented fetch.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… honest guardrail scope Addresses PR review: the swr cache carried the backend on `deco.cache.profile`, which the edge layer uses for page-type — a `sum by (profile)` panel would blend provider names with page-types. Add a separate `deco.cache.provider` label (recordCacheMetric gains an optional `provider` arg); the swr layer now leaves `profile` unset. Also soften the guardrail docstring + CLAUDE.md to state its real scope (the factory exists; it does NOT verify the site wired setXFetch). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Cache/upstream telemetry only reached ClickHouse for the
edgeandcachedLoaderlayers; the commerce apps' in-memory SWR fetch caches (VTEX today, and every Magento site) were dark, so we had no hit/miss ratio or upstream latency for the layer doing the real dedup/SWR work against provider APIs. This adds one shared, instrumented SWR cache —createFetchCachein@decocms/blocks/sdk/fetchCache, emittingdeco.cache.requests{layer="swr",profile=<provider>}on HIT/STALE-HIT/MISS with provider-isolated Maps — and routes every commerce app through it plus the sharedcreateInstrumentedFetch, so any current or future app is covered automatically (thevtex-swrlayer is renamed to the provider-agnosticswr). VTEX'sfetchCachebecomes a thin binding over the shared util (no behavior change, now emits metrics); apps-magento gainssetMagentoFetch/createMagentoFetch+magentoCachedFetch+ an operation router; apps-salesforce gainscreateSalesforceFetchand defaultscreateHttpClient's fetcher to it so all loaders are instrumented without per-loader wiring. The contract is documented inCLAUDE.mdand enforced by a guardrail test inapps-commercethat fails if a commerce app lacks an instrumented fetch. Verified: typecheck clean across the six affected packages and the full test suites pass (plus newfetchCache, magento instrumentation, and guardrail tests).🤖 Generated with Claude Code
Summary by cubic
Instrumented the shared SWR fetch cache and provider fetches so cache hit/miss and upstream latency metrics reach ClickHouse with clean labels. SWR now emits
deco.cache.requestswithlayer="swr"and the backend indeco.cache.provider; VTEX, Magento, and Salesforce also emithttp.client.request.duration.New Features
createFetchCachein@decocms/blocks/sdk/fetchCacheemittingdeco.cache.requests{layer="swr",provider=<backend>}on HIT/STALE-HIT/MISS.vtex-swrtoswr; SWR leavesdeco.cache.profileunset and carries the backend indeco.cache.provider.fetchWithCache, stats, clear).createMagentoFetch+setMagentoFetch,magentoCachedFetch, and an operation router to instrument all egress via one fetch.createSalesforceFetch;createHttpClientnow defaults itsfetcherto it so all loaders are covered.recordCacheMetricto accept aproviderlabel; tests and docs reflect the new label scheme. Guardrail test ensures each commerce app ships aninstrumentedFetchfactory (factory existence only).Migration
setMagentoFetch(createMagentoFetch())at boot; usemagentoCachedFetchfor cacheable GETs.fetchertocreateHttpClient, wrap it withcreateSalesforceFetch({ baseFetch }).deco.cache.layer="swr"and slice bydeco.cache.provider(wasvtex-swranddeco.cache.profile).Written for commit 8d3f4db. Summary will update on new commits.