11import { WorkerEntrypoint } from "cloudflare:workers" ;
2+ import { PerformanceTimer } from "../../utils/performance" ;
23import { setupSentry } from "../../utils/sentry" ;
4+ import { Analytics } from "./analytics" ;
35import { AssetsManifest } from "./assets-manifest" ;
46import { applyConfigurationDefaults } from "./configuration" ;
57import { decodePath , getIntent , handleRequest } from "./handler" ;
@@ -8,7 +10,8 @@ import {
810 MethodNotAllowedResponse ,
911} from "./responses" ;
1012import { getAssetWithMetadataFromKV } from "./utils/kv" ;
11- import type { AssetConfig } from "../../utils/types" ;
13+ import type { AssetConfig , UnsafePerformanceTimer } from "../../utils/types" ;
14+ import type { ColoMetadata , Environment , ReadyAnalytics } from "./types" ;
1215
1316type Env = {
1417 /*
@@ -29,6 +32,12 @@ type Env = {
2932
3033 SENTRY_ACCESS_CLIENT_ID : string ;
3134 SENTRY_ACCESS_CLIENT_SECRET : string ;
35+
36+ ENVIRONMENT : Environment ;
37+ ANALYTICS : ReadyAnalytics ;
38+ COLO_METADATA : ColoMetadata ;
39+ UNSAFE_PERFORMANCE : UnsafePerformanceTimer ;
40+ VERSION_METADATA : WorkerVersionMetadata ;
3241} ;
3342
3443/*
@@ -45,6 +54,10 @@ type Env = {
4554export default class extends WorkerEntrypoint < Env > {
4655 async fetch ( request : Request ) : Promise < Response > {
4756 let sentry : ReturnType < typeof setupSentry > | undefined ;
57+ const analytics = new Analytics ( this . env . ANALYTICS ) ;
58+ const performance = new PerformanceTimer ( this . env . UNSAFE_PERFORMANCE ) ;
59+ const startTimeMs = performance . now ( ) ;
60+
4861 try {
4962 sentry = setupSentry (
5063 request ,
@@ -54,9 +67,34 @@ export default class extends WorkerEntrypoint<Env> {
5467 this . env . SENTRY_ACCESS_CLIENT_SECRET
5568 ) ;
5669
70+ const config = applyConfigurationDefaults ( this . env . CONFIG ) ;
71+ const userAgent = request . headers . get ( "user-agent" ) ?? "UA UNKNOWN" ;
72+
73+ if ( sentry ) {
74+ const colo = this . env . COLO_METADATA . coloId ;
75+ sentry . setTag ( "colo" , this . env . COLO_METADATA . coloId ) ;
76+ sentry . setTag ( "metal" , this . env . COLO_METADATA . metalId ) ;
77+ sentry . setUser ( { userAgent : userAgent , colo : colo } ) ;
78+ }
79+
80+ if ( this . env . COLO_METADATA && this . env . VERSION_METADATA ) {
81+ const url = new URL ( request . url ) ;
82+ analytics . setData ( {
83+ coloId : this . env . COLO_METADATA . coloId ,
84+ metalId : this . env . COLO_METADATA . metalId ,
85+ coloTier : this . env . COLO_METADATA . coloTier ,
86+ coloRegion : this . env . COLO_METADATA . coloRegion ,
87+ version : this . env . VERSION_METADATA . id ,
88+ hostname : url . hostname ,
89+ htmlHandling : config . html_handling ,
90+ notFoundHandling : config . not_found_handling ,
91+ userAgent : userAgent ,
92+ } ) ;
93+ }
94+
5795 return handleRequest (
5896 request ,
59- applyConfigurationDefaults ( this . env . CONFIG ) ,
97+ config ,
6098 this . unstable_exists . bind ( this ) ,
6199 this . unstable_getByETag . bind ( this )
62100 ) ;
@@ -68,7 +106,14 @@ export default class extends WorkerEntrypoint<Env> {
68106 sentry . captureException ( err ) ;
69107 }
70108
109+ if ( err instanceof Error ) {
110+ analytics . setData ( { error : err . message } ) ;
111+ }
112+
71113 return response ;
114+ } finally {
115+ analytics . setData ( { requestTime : performance . now ( ) - startTimeMs } ) ;
116+ analytics . write ( ) ;
72117 }
73118 }
74119
0 commit comments