Skip to content

Commit

Permalink
Plumb through account ID and Worker ID into asset-worker and router-w…
Browse files Browse the repository at this point in the history
…orker (#7844)
  • Loading branch information
WalshyDev authored Jan 22, 2025
1 parent cd31971 commit 92ed81e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-penguins-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/workers-shared": patch
---

chore: plumb through account ID and Worker ID into the asset-worker and router-worker for use in analytics and error reporting.
8 changes: 6 additions & 2 deletions packages/workers-shared/asset-worker/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const VERSION = 1;

// When adding new columns please update the schema
type Data = {
// -- Indexes --
accountId?: number;
scriptId?: number;

// -- Doubles --
// double1 - The time it takes for the whole request to complete in milliseconds
requestTime?: number;
Expand Down Expand Up @@ -55,8 +59,8 @@ export class Analytics {

this.readyAnalytics.logEvent({
version: VERSION,
accountId: 0, // TODO: need to plumb through
indexId: this.data.hostname?.substring(0, 96),
accountId: this.data.accountId,
indexId: this.data.scriptId?.toString(),
doubles: [
this.data.requestTime ?? -1, // double1
this.data.coloId ?? -1, // double2
Expand Down
29 changes: 17 additions & 12 deletions packages/workers-shared/asset-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { InternalServerErrorResponse } from "./responses";
import { getAssetWithMetadataFromKV } from "./utils/kv";
import { mockJaegerBinding } from "./utils/mocks";
import type {
AssetConfig,
AssetWorkerConfig,
ColoMetadata,
JaegerTracing,
UnsafePerformanceTimer,
} from "../../utils/types";
import type { ColoMetadata, Environment, ReadyAnalytics } from "./types";
import type { Environment, ReadyAnalytics } from "./types";
import type { Toucan } from "toucan-js";

export type Env = {
Expand All @@ -29,7 +30,7 @@ export type Env = {
*/
ASSETS_KV_NAMESPACE: KVNamespace;

CONFIG: AssetConfig;
CONFIG: AssetWorkerConfig;

SENTRY_DSN: string;
SENTRY_ACCESS_CLIENT_ID: string;
Expand Down Expand Up @@ -73,25 +74,29 @@ export default class extends WorkerEntrypoint<Env> {
this.ctx,
this.env.SENTRY_DSN,
this.env.SENTRY_ACCESS_CLIENT_ID,
this.env.SENTRY_ACCESS_CLIENT_SECRET
this.env.SENTRY_ACCESS_CLIENT_SECRET,
this.env.COLO_METADATA,
this.env.CONFIG?.account_id,
this.env.CONFIG?.script_id
);

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

if (sentry) {
const colo = this.env.COLO_METADATA.coloId;
sentry.setTag("colo", this.env.COLO_METADATA.coloId);
sentry.setTag("metal", this.env.COLO_METADATA.metalId);
sentry.setUser({ userAgent: userAgent, colo: colo });
}

const url = new URL(request.url);
if (this.env.COLO_METADATA && this.env.VERSION_METADATA) {
if (
this.env.COLO_METADATA &&
this.env.VERSION_METADATA &&
this.env.CONFIG
) {
analytics.setData({
accountId: this.env.CONFIG.account_id,
scriptId: this.env.CONFIG.script_id,

coloId: this.env.COLO_METADATA.coloId,
metalId: this.env.COLO_METADATA.metalId,
coloTier: this.env.COLO_METADATA.coloTier,

coloRegion: this.env.COLO_METADATA.coloRegion,
version: this.env.VERSION_METADATA.id,
hostname: url.hostname,
Expand Down
7 changes: 0 additions & 7 deletions packages/workers-shared/asset-worker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ export interface ReadyAnalytics {
logEvent: (e: ReadyAnalyticsEvent) => void;
}

export interface ColoMetadata {
metalId: number;
coloId: number;
coloRegion: string;
coloTier: number;
}

export interface ReadyAnalyticsEvent {
accountId?: number;
indexId?: string;
Expand Down
8 changes: 6 additions & 2 deletions packages/workers-shared/router-worker/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export enum DISPATCH_TYPE {

// When adding new columns please update the schema
type Data = {
// -- Indexes --
accountId?: number;
scriptId?: number;

// -- Doubles --
// double1 - The time it takes for the whole request to complete in milliseconds
requestTime?: number;
Expand Down Expand Up @@ -58,8 +62,8 @@ export class Analytics {

this.readyAnalytics.logEvent({
version: VERSION,
accountId: 0, // TODO: need to plumb through
indexId: this.data.hostname?.substring(0, 96),
accountId: this.data.accountId,
indexId: this.data.scriptId?.toString(),
doubles: [
this.data.requestTime ?? -1, // double1
this.data.coloId ?? -1, // double2
Expand Down
14 changes: 8 additions & 6 deletions packages/workers-shared/router-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ export default {
ctx,
env.SENTRY_DSN,
env.SENTRY_ACCESS_CLIENT_ID,
env.SENTRY_ACCESS_CLIENT_SECRET
env.SENTRY_ACCESS_CLIENT_SECRET,
env.COLO_METADATA,
env.CONFIG?.account_id,
env.CONFIG?.script_id
);

const url = new URL(request.url);
if (sentry) {
sentry.setUser({ username: url.hostname });
sentry.setTag("colo", env.COLO_METADATA.coloId);
sentry.setTag("metal", env.COLO_METADATA.metalId);
}

if (env.COLO_METADATA && env.VERSION_METADATA && env.CONFIG) {
analytics.setData({
accountId: env.CONFIG.account_id,
scriptId: env.CONFIG.script_id,

coloId: env.COLO_METADATA.coloId,
metalId: env.COLO_METADATA.metalId,
coloTier: env.COLO_METADATA.coloTier,

coloRegion: env.COLO_METADATA.coloRegion,
hostname: url.hostname,
version: env.VERSION_METADATA.id,
Expand Down
22 changes: 17 additions & 5 deletions packages/workers-shared/utils/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Toucan } from "toucan-js";
import type { ColoMetadata } from "./types";

export function setupSentry(
request: Request,
context: ExecutionContext | undefined,
dsn: string,
clientId: string,
clientSecret: string
clientSecret: string,
coloMetadata?: ColoMetadata,
accountId?: number,
scriptId?: number
): Toucan | undefined {
// Are we running locally without access to Sentry secrets? If so, don't initialise Sentry
if (!(dsn && clientId && clientSecret)) {
Expand Down Expand Up @@ -37,10 +41,18 @@ export function setupSentry(
},
},
});
const colo = request.cf?.colo ?? "UNKNOWN";
sentry.setTag("colo", colo as string);

const userAgent = request.headers.get("user-agent") ?? "UA UNKNOWN";
sentry.setUser({ userAgent: userAgent, colo: colo });
if (coloMetadata) {
sentry.setTag("colo", coloMetadata.coloId);
sentry.setTag("metal", coloMetadata.metalId);
}

if (accountId && scriptId) {
sentry.setTag("accountId", accountId);
sentry.setTag("scriptId", scriptId);
}

sentry.setUser({ id: accountId?.toString() });

return sentry;
}
23 changes: 23 additions & 0 deletions packages/workers-shared/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { z } from "zod";
export const RoutingConfigSchema = z.object({
has_user_worker: z.boolean().optional(),
invoke_user_worker_ahead_of_assets: z.boolean().optional(),

// Used for analytics and reporting
account_id: z.number().optional(),
script_id: z.number().optional(),
});

export const AssetConfigSchema = z.object({
Expand All @@ -20,8 +24,20 @@ export const AssetConfigSchema = z.object({
serve_directly: z.boolean().optional(),
});

export const InternalConfigSchema = z.object({
// Used for analytics and reporting
account_id: z.number().optional(),
script_id: z.number().optional(),
});

export const AssetWorkerConfigShema = z.object({
...AssetConfigSchema.shape,
...InternalConfigSchema.shape,
});

export type RoutingConfig = z.infer<typeof RoutingConfigSchema>;
export type AssetConfig = z.infer<typeof AssetConfigSchema>;
export type AssetWorkerConfig = z.infer<typeof AssetWorkerConfigShema>;

export interface UnsafePerformanceTimer {
readonly timeOrigin: number;
Expand Down Expand Up @@ -64,3 +80,10 @@ export interface SpanContext {

export type JaegerValue = string | number | boolean;
export type JaegerRecord = Record<string, JaegerValue>;

export interface ColoMetadata {
metalId: number;
coloId: number;
coloRegion: string;
coloTier: number;
}

0 comments on commit 92ed81e

Please sign in to comment.