-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WC-2695 Add better visibility for router-worker (#6941)
This commit instruments the router worker with analytics such as request time, colo metadata, error, etc, in order for us to have better visibility into the router worker. These changes were tested using gradual rollouts with a 0% version and a Cloudflare-Workers-Version-Overrides header.
- Loading branch information
1 parent
51aedd4
commit fd43068
Showing
6 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@cloudflare/workers-shared": minor | ||
--- | ||
|
||
feat: Add observability to router-worker |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { Environment, ReadyAnalytics } from "./types"; | ||
|
||
// This will allow us to make breaking changes to the analytic schema | ||
const VERSION = 1; | ||
|
||
export enum DISPATCH_TYPE { | ||
ASSETS = "asset", | ||
WORKER = "worker", | ||
} | ||
|
||
// When adding new columns please update the schema | ||
type Data = { | ||
// -- Doubles -- | ||
// double1 - The time it takes for the whole request to complete in milliseconds | ||
requestTime?: number; | ||
// double2 - Colo ID | ||
coloId?: number; | ||
// double3 - Metal ID | ||
metalId?: number; | ||
// double4 - Colo tier (e.g. tier 1, tier 2, tier 3) | ||
coloTier?: number; | ||
|
||
// -- Blobs -- | ||
// blob1 - Hostname of the request | ||
hostname?: string; | ||
// blob2 - Dispatch type - what kind of thing did we dispatch | ||
dispatchtype?: DISPATCH_TYPE; | ||
// blob3 - Error message | ||
error?: string; | ||
// blob4 - The current version UUID of router-server | ||
version?: string; | ||
// blob5 - Region of the colo (e.g. WEUR) | ||
coloRegion?: string; | ||
}; | ||
|
||
export class Analytics { | ||
private data: Data = {}; | ||
|
||
setData(newData: Partial<Data>) { | ||
this.data = { ...this.data, ...newData }; | ||
} | ||
|
||
getData(key: keyof Data) { | ||
return this.data[key]; | ||
} | ||
|
||
write(env: Environment, readyAnalytics?: ReadyAnalytics, hostname?: string) { | ||
if (!readyAnalytics) { | ||
return; | ||
} | ||
|
||
readyAnalytics.logEvent({ | ||
version: VERSION, | ||
accountId: 0, // TODO: need to plumb through | ||
indexId: hostname, | ||
doubles: [ | ||
this.data.requestTime ?? -1, // double1 | ||
this.data.coloId ?? -1, // double2 | ||
this.data.metalId ?? -1, // double3 | ||
this.data.coloTier ?? -1, // double4 | ||
], | ||
blobs: [ | ||
this.data.hostname?.substring(0, 256), // blob1 - trim to 256 bytes | ||
this.data.dispatchtype, // blob2 | ||
this.data.error?.substring(0, 256), // blob3 - trim to 256 bytes | ||
this.data.version, // blob4 | ||
this.data.coloRegion, // blob5 | ||
], | ||
}); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { UnsafePerformanceTimer } from "./types"; | ||
|
||
export class PerformanceTimer { | ||
private performanceTimer; | ||
|
||
constructor(performanceTimer?: UnsafePerformanceTimer) { | ||
this.performanceTimer = performanceTimer; | ||
} | ||
|
||
now() { | ||
if (this.performanceTimer) { | ||
return this.performanceTimer.timeOrigin + this.performanceTimer.now(); | ||
} | ||
return Date.now(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export type Environment = "production" | "staging"; | ||
|
||
export interface ReadyAnalytics { | ||
logEvent: (e: ReadyAnalyticsEvent) => void; | ||
} | ||
|
||
export interface ColoMetadata { | ||
metalId: number; | ||
coloId: number; | ||
coloRegion: string; | ||
coloTier: number; | ||
} | ||
|
||
export interface UnsafePerformanceTimer { | ||
readonly timeOrigin: number; | ||
now: () => number; | ||
} | ||
|
||
export interface ReadyAnalyticsEvent { | ||
accountId?: number; | ||
indexId?: string; | ||
version?: number; | ||
doubles?: (number | undefined)[]; | ||
blobs?: (string | undefined)[]; | ||
} |
This file contains 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