Skip to content

Commit 7744f1a

Browse files
authored
Add Jaeger tracing to unstable asset-worker methods (#9043)
1 parent a4c52e6 commit 7744f1a

File tree

4 files changed

+86
-44
lines changed

4 files changed

+86
-44
lines changed

.changeset/blue-suns-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/workers-shared": patch
3+
---
4+
5+
Adds tracing into asset-worker unstable methods so we can better measure/debug these.

packages/workers-shared/asset-worker/src/index.ts

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ export default class extends WorkerEntrypoint<Env> {
140140
}
141141
}
142142

143-
// TODO: Add observability to these methods
144143
async unstable_canFetch(request: Request): Promise<boolean> {
145144
// TODO: Mock this with Miniflare
146145
this.env.JAEGER ??= mockJaegerBinding();
@@ -162,28 +161,45 @@ export default class extends WorkerEntrypoint<Env> {
162161
cacheStatus: "HIT" | "MISS";
163162
}> {
164163
const performance = new PerformanceTimer(this.env.UNSAFE_PERFORMANCE);
165-
const startTime = performance.now();
166-
const asset = await getAssetWithMetadataFromKV(
167-
this.env.ASSETS_KV_NAMESPACE,
168-
eTag
169-
);
170-
const endTime = performance.now();
171-
const assetFetchTime = endTime - startTime;
172-
173-
if (!asset || !asset.value) {
174-
throw new Error(
175-
`Requested asset ${eTag} exists in the asset manifest but not in the KV namespace.`
164+
const jaeger = this.env.JAEGER ?? mockJaegerBinding();
165+
return jaeger.enterSpan("unstable_getByETag", async (span) => {
166+
const startTime = performance.now();
167+
const asset = await getAssetWithMetadataFromKV(
168+
this.env.ASSETS_KV_NAMESPACE,
169+
eTag
176170
);
177-
}
171+
const endTime = performance.now();
172+
const assetFetchTime = endTime - startTime;
173+
174+
if (!asset || !asset.value) {
175+
span.setTags({
176+
error: true,
177+
});
178+
span.addLogs({
179+
error: `Requested asset ${eTag} exists in the asset manifest but not in the KV namespace.`,
180+
});
181+
throw new Error(
182+
`Requested asset ${eTag} exists in the asset manifest but not in the KV namespace.`
183+
);
184+
}
178185

179-
return {
180-
readableStream: asset.value,
181-
contentType: asset.metadata?.contentType,
182186
// KV does not yet provide a way to check if a value was fetched from cache
183187
// so we assume that if the fetch time is less than 100ms, it was a cache hit.
184188
// This is a reasonable assumption given the data we have and how KV works.
185-
cacheStatus: assetFetchTime <= 100 ? "HIT" : "MISS",
186-
};
189+
const cacheStatus = assetFetchTime <= 100 ? "HIT" : "MISS";
190+
191+
span.setTags({
192+
etag: eTag,
193+
contentType: asset.metadata?.contentType ?? "unknown",
194+
cacheStatus,
195+
});
196+
197+
return {
198+
readableStream: asset.value,
199+
contentType: asset.metadata?.contentType,
200+
cacheStatus,
201+
};
202+
});
187203
}
188204

189205
async unstable_getByPathname(
@@ -194,12 +210,21 @@ export default class extends WorkerEntrypoint<Env> {
194210
contentType: string | undefined;
195211
cacheStatus: "HIT" | "MISS";
196212
} | null> {
197-
const eTag = await this.unstable_exists(pathname, request);
198-
if (!eTag) {
199-
return null;
200-
}
213+
const jaeger = this.env.JAEGER ?? mockJaegerBinding();
214+
return jaeger.enterSpan("unstable_getByPathname", async (span) => {
215+
const eTag = await this.unstable_exists(pathname, request);
216+
217+
span.setTags({
218+
path: pathname,
219+
found: eTag !== null,
220+
});
201221

202-
return this.unstable_getByETag(eTag, request);
222+
if (!eTag) {
223+
return null;
224+
}
225+
226+
return this.unstable_getByETag(eTag, request);
227+
});
203228
}
204229

205230
async unstable_exists(
@@ -208,25 +233,37 @@ export default class extends WorkerEntrypoint<Env> {
208233
): Promise<string | null> {
209234
const analytics = new ExperimentAnalytics(this.env.EXPERIMENT_ANALYTICS);
210235
const performance = new PerformanceTimer(this.env.UNSAFE_PERFORMANCE);
236+
const jaeger = this.env.JAEGER ?? mockJaegerBinding();
237+
return jaeger.enterSpan("unstable_exists", async (span) => {
238+
if (
239+
this.env.COLO_METADATA &&
240+
this.env.VERSION_METADATA &&
241+
this.env.CONFIG
242+
) {
243+
analytics.setData({
244+
accountId: this.env.CONFIG.account_id,
245+
experimentName: "manifest-read-timing",
246+
});
247+
}
211248

212-
if (
213-
this.env.COLO_METADATA &&
214-
this.env.VERSION_METADATA &&
215-
this.env.CONFIG
216-
) {
217-
analytics.setData({
218-
accountId: this.env.CONFIG.account_id,
219-
experimentName: "manifest-read-timing",
220-
});
221-
}
249+
const startTimeMs = performance.now();
250+
try {
251+
const assetsManifest = new AssetsManifest(this.env.ASSETS_MANIFEST);
252+
const eTag = await assetsManifest.get(pathname);
222253

223-
const startTimeMs = performance.now();
224-
try {
225-
const assetsManifest = new AssetsManifest(this.env.ASSETS_MANIFEST);
226-
return await assetsManifest.get(pathname);
227-
} finally {
228-
analytics.setData({ manifestReadTime: performance.now() - startTimeMs });
229-
analytics.write();
230-
}
254+
span.setTags({
255+
path: pathname,
256+
found: eTag !== null,
257+
etag: eTag ?? "",
258+
});
259+
260+
return eTag;
261+
} finally {
262+
analytics.setData({
263+
manifestReadTime: performance.now() - startTimeMs,
264+
});
265+
analytics.write();
266+
}
267+
});
231268
}
232269
}

packages/workers-shared/asset-worker/wrangler.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ name = "ASSETS_KV_NAMESPACE"
7373
type = "internal_assets"
7474

7575
[[env.staging.unsafe.bindings]]
76-
name = "workers-asset-worker"
76+
name = "workers-asset-worker-staging"
7777
type = "internal_capability_grants"

packages/workers-shared/router-worker/wrangler.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ stable_id = "cloudflare/cf_router_worker"
6565
networks = ["cf","jdc"]
6666

6767
[[env.staging.unsafe.bindings]]
68-
name = "workers-router-worker"
69-
type = "internal_capability_grants"
68+
name = "workers-router-worker-staging"
69+
type = "internal_capability_grants"

0 commit comments

Comments
 (0)