Skip to content

Commit a60e9da

Browse files
WC-3725 Add time-to-dispatch analytics to the router worker (#9884)
Measures the time the Router Worker takes until we dispatch to either the user or Asset Worker, effectively the cost of routing.
1 parent e10c3e2 commit a60e9da

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

.changeset/little-lands-read.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 metrics for time-to-dispatch to Router Worker

packages/workers-shared/router-worker/src/analytics.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type Data = {
3737
abuseMitigationBlocked?: boolean;
3838
// double8 - User worker invocation denied due to free tier limiting
3939
userWorkerFreeTierLimiting?: boolean;
40+
// double9 - The time it takes for the request to be handed off the Asset Worker or user Worker in milliseconds
41+
timeToDispatch?: number;
4042

4143
// -- Blobs --
4244
// blob1 - Hostname of the request
@@ -96,6 +98,7 @@ export class Analytics {
9698
this.data.staticRoutingDecision ?? STATIC_ROUTING_DECISION.NOT_PROVIDED, // double6
9799
this.data.abuseMitigationBlocked ? 1 : 0, // double7
98100
this.data.userWorkerFreeTierLimiting ? 1 : 0, // double8
101+
this.data.timeToDispatch ?? -1, // double9
99102
],
100103
blobs: [
101104
this.data.hostname?.substring(0, 256), // blob1 - trim to 256 bytes

packages/workers-shared/router-worker/src/worker.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ export default {
130130
}
131131
}
132132

133+
analytics.setData({
134+
timeToDispatch: performance.now() - startTimeMs,
135+
});
136+
133137
if (shouldBlockNonImageResponse) {
134138
const resp = await env.USER_WORKER.fetch(maybeSecondRequest);
135139
const isImage = resp.headers
@@ -160,6 +164,9 @@ export default {
160164
dispatchType: DISPATCH_TYPE.ASSETS,
161165
});
162166

167+
analytics.setData({
168+
timeToDispatch: performance.now() - startTimeMs,
169+
});
163170
return env.ASSET_WORKER.fetch(maybeSecondRequest);
164171
});
165172
};

0 commit comments

Comments
 (0)