Skip to content

Commit 92ed81e

Browse files
authored
Plumb through account ID and Worker ID into asset-worker and router-worker (#7844)
1 parent cd31971 commit 92ed81e

File tree

8 files changed

+82
-34
lines changed

8 files changed

+82
-34
lines changed
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+
chore: plumb through account ID and Worker ID into the asset-worker and router-worker for use in analytics and error reporting.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const VERSION = 1;
55

66
// When adding new columns please update the schema
77
type Data = {
8+
// -- Indexes --
9+
accountId?: number;
10+
scriptId?: number;
11+
812
// -- Doubles --
913
// double1 - The time it takes for the whole request to complete in milliseconds
1014
requestTime?: number;
@@ -55,8 +59,8 @@ export class Analytics {
5559

5660
this.readyAnalytics.logEvent({
5761
version: VERSION,
58-
accountId: 0, // TODO: need to plumb through
59-
indexId: this.data.hostname?.substring(0, 96),
62+
accountId: this.data.accountId,
63+
indexId: this.data.scriptId?.toString(),
6064
doubles: [
6165
this.data.requestTime ?? -1, // double1
6266
this.data.coloId ?? -1, // double2

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import { InternalServerErrorResponse } from "./responses";
99
import { getAssetWithMetadataFromKV } from "./utils/kv";
1010
import { mockJaegerBinding } from "./utils/mocks";
1111
import type {
12-
AssetConfig,
12+
AssetWorkerConfig,
13+
ColoMetadata,
1314
JaegerTracing,
1415
UnsafePerformanceTimer,
1516
} from "../../utils/types";
16-
import type { ColoMetadata, Environment, ReadyAnalytics } from "./types";
17+
import type { Environment, ReadyAnalytics } from "./types";
1718
import type { Toucan } from "toucan-js";
1819

1920
export type Env = {
@@ -29,7 +30,7 @@ export type Env = {
2930
*/
3031
ASSETS_KV_NAMESPACE: KVNamespace;
3132

32-
CONFIG: AssetConfig;
33+
CONFIG: AssetWorkerConfig;
3334

3435
SENTRY_DSN: string;
3536
SENTRY_ACCESS_CLIENT_ID: string;
@@ -73,25 +74,29 @@ export default class extends WorkerEntrypoint<Env> {
7374
this.ctx,
7475
this.env.SENTRY_DSN,
7576
this.env.SENTRY_ACCESS_CLIENT_ID,
76-
this.env.SENTRY_ACCESS_CLIENT_SECRET
77+
this.env.SENTRY_ACCESS_CLIENT_SECRET,
78+
this.env.COLO_METADATA,
79+
this.env.CONFIG?.account_id,
80+
this.env.CONFIG?.script_id
7781
);
7882

7983
const config = applyConfigurationDefaults(this.env.CONFIG);
8084
const userAgent = request.headers.get("user-agent") ?? "UA UNKNOWN";
8185

82-
if (sentry) {
83-
const colo = this.env.COLO_METADATA.coloId;
84-
sentry.setTag("colo", this.env.COLO_METADATA.coloId);
85-
sentry.setTag("metal", this.env.COLO_METADATA.metalId);
86-
sentry.setUser({ userAgent: userAgent, colo: colo });
87-
}
88-
8986
const url = new URL(request.url);
90-
if (this.env.COLO_METADATA && this.env.VERSION_METADATA) {
87+
if (
88+
this.env.COLO_METADATA &&
89+
this.env.VERSION_METADATA &&
90+
this.env.CONFIG
91+
) {
9192
analytics.setData({
93+
accountId: this.env.CONFIG.account_id,
94+
scriptId: this.env.CONFIG.script_id,
95+
9296
coloId: this.env.COLO_METADATA.coloId,
9397
metalId: this.env.COLO_METADATA.metalId,
9498
coloTier: this.env.COLO_METADATA.coloTier,
99+
95100
coloRegion: this.env.COLO_METADATA.coloRegion,
96101
version: this.env.VERSION_METADATA.id,
97102
hostname: url.hostname,

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ export interface ReadyAnalytics {
44
logEvent: (e: ReadyAnalyticsEvent) => void;
55
}
66

7-
export interface ColoMetadata {
8-
metalId: number;
9-
coloId: number;
10-
coloRegion: string;
11-
coloTier: number;
12-
}
13-
147
export interface ReadyAnalyticsEvent {
158
accountId?: number;
169
indexId?: string;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export enum DISPATCH_TYPE {
1010

1111
// When adding new columns please update the schema
1212
type Data = {
13+
// -- Indexes --
14+
accountId?: number;
15+
scriptId?: number;
16+
1317
// -- Doubles --
1418
// double1 - The time it takes for the whole request to complete in milliseconds
1519
requestTime?: number;
@@ -58,8 +62,8 @@ export class Analytics {
5862

5963
this.readyAnalytics.logEvent({
6064
version: VERSION,
61-
accountId: 0, // TODO: need to plumb through
62-
indexId: this.data.hostname?.substring(0, 96),
65+
accountId: this.data.accountId,
66+
indexId: this.data.scriptId?.toString(),
6367
doubles: [
6468
this.data.requestTime ?? -1, // double1
6569
this.data.coloId ?? -1, // double2

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,23 @@ export default {
3434
ctx,
3535
env.SENTRY_DSN,
3636
env.SENTRY_ACCESS_CLIENT_ID,
37-
env.SENTRY_ACCESS_CLIENT_SECRET
37+
env.SENTRY_ACCESS_CLIENT_SECRET,
38+
env.COLO_METADATA,
39+
env.CONFIG?.account_id,
40+
env.CONFIG?.script_id
3841
);
3942

4043
const url = new URL(request.url);
41-
if (sentry) {
42-
sentry.setUser({ username: url.hostname });
43-
sentry.setTag("colo", env.COLO_METADATA.coloId);
44-
sentry.setTag("metal", env.COLO_METADATA.metalId);
45-
}
4644

4745
if (env.COLO_METADATA && env.VERSION_METADATA && env.CONFIG) {
4846
analytics.setData({
47+
accountId: env.CONFIG.account_id,
48+
scriptId: env.CONFIG.script_id,
49+
4950
coloId: env.COLO_METADATA.coloId,
5051
metalId: env.COLO_METADATA.metalId,
5152
coloTier: env.COLO_METADATA.coloTier,
53+
5254
coloRegion: env.COLO_METADATA.coloRegion,
5355
hostname: url.hostname,
5456
version: env.VERSION_METADATA.id,

packages/workers-shared/utils/sentry.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { Toucan } from "toucan-js";
2+
import type { ColoMetadata } from "./types";
23

34
export function setupSentry(
45
request: Request,
56
context: ExecutionContext | undefined,
67
dsn: string,
78
clientId: string,
8-
clientSecret: string
9+
clientSecret: string,
10+
coloMetadata?: ColoMetadata,
11+
accountId?: number,
12+
scriptId?: number
913
): Toucan | undefined {
1014
// Are we running locally without access to Sentry secrets? If so, don't initialise Sentry
1115
if (!(dsn && clientId && clientSecret)) {
@@ -37,10 +41,18 @@ export function setupSentry(
3741
},
3842
},
3943
});
40-
const colo = request.cf?.colo ?? "UNKNOWN";
41-
sentry.setTag("colo", colo as string);
4244

43-
const userAgent = request.headers.get("user-agent") ?? "UA UNKNOWN";
44-
sentry.setUser({ userAgent: userAgent, colo: colo });
45+
if (coloMetadata) {
46+
sentry.setTag("colo", coloMetadata.coloId);
47+
sentry.setTag("metal", coloMetadata.metalId);
48+
}
49+
50+
if (accountId && scriptId) {
51+
sentry.setTag("accountId", accountId);
52+
sentry.setTag("scriptId", scriptId);
53+
}
54+
55+
sentry.setUser({ id: accountId?.toString() });
56+
4557
return sentry;
4658
}

packages/workers-shared/utils/types.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { z } from "zod";
33
export const RoutingConfigSchema = z.object({
44
has_user_worker: z.boolean().optional(),
55
invoke_user_worker_ahead_of_assets: z.boolean().optional(),
6+
7+
// Used for analytics and reporting
8+
account_id: z.number().optional(),
9+
script_id: z.number().optional(),
610
});
711

812
export const AssetConfigSchema = z.object({
@@ -20,8 +24,20 @@ export const AssetConfigSchema = z.object({
2024
serve_directly: z.boolean().optional(),
2125
});
2226

27+
export const InternalConfigSchema = z.object({
28+
// Used for analytics and reporting
29+
account_id: z.number().optional(),
30+
script_id: z.number().optional(),
31+
});
32+
33+
export const AssetWorkerConfigShema = z.object({
34+
...AssetConfigSchema.shape,
35+
...InternalConfigSchema.shape,
36+
});
37+
2338
export type RoutingConfig = z.infer<typeof RoutingConfigSchema>;
2439
export type AssetConfig = z.infer<typeof AssetConfigSchema>;
40+
export type AssetWorkerConfig = z.infer<typeof AssetWorkerConfigShema>;
2541

2642
export interface UnsafePerformanceTimer {
2743
readonly timeOrigin: number;
@@ -64,3 +80,10 @@ export interface SpanContext {
6480

6581
export type JaegerValue = string | number | boolean;
6682
export type JaegerRecord = Record<string, JaegerValue>;
83+
84+
export interface ColoMetadata {
85+
metalId: number;
86+
coloId: number;
87+
coloRegion: string;
88+
coloTier: number;
89+
}

0 commit comments

Comments
 (0)