-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c794935
commit 2e91066
Showing
9 changed files
with
165 additions
and
16 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 | ||
--- | ||
|
||
Adds analytics to asset-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,77 @@ | ||
import type { ReadyAnalytics } from "./types"; | ||
|
||
// This will allow us to make breaking changes to the analytic schema | ||
const VERSION = 1; | ||
|
||
// 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 - User agent making the request | ||
userAgent?: string; | ||
// blob3 - Html handling option | ||
htmlHandling?: string; | ||
// blob4 - Not found handling option | ||
notFoundHandling?: string; | ||
// blob5 - Error message | ||
error?: string; | ||
// blob6 - The current version UUID of asset-worker | ||
version?: string; | ||
// blob7 - Region of the colo (e.g. WEUR) | ||
coloRegion?: string; | ||
}; | ||
|
||
export class Analytics { | ||
private data: Data = {}; | ||
private readyAnalytics?: ReadyAnalytics; | ||
|
||
constructor(readyAnalytics?: ReadyAnalytics) { | ||
this.readyAnalytics = readyAnalytics; | ||
} | ||
|
||
setData(newData: Partial<Data>) { | ||
this.data = { ...this.data, ...newData }; | ||
} | ||
|
||
getData(key: keyof Data) { | ||
return this.data[key]; | ||
} | ||
|
||
write(hostname?: string) { | ||
if (!this.readyAnalytics) { | ||
return; | ||
} | ||
|
||
this.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.userAgent?.substring(0, 256), // blob2 - trim to 256 bytes | ||
this.data.htmlHandling, // blob3 | ||
this.data.notFoundHandling, // blob4 | ||
this.data.error?.substring(0, 256), // blob5 - trim to 256 bytes | ||
this.data.version, // blob6 | ||
this.data.coloRegion, // blob7 | ||
], | ||
}); | ||
} | ||
} |
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,20 @@ | ||
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 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
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
File renamed without changes.
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