11import { setupSentry } from "../../utils/sentry" ;
2+ import { Analytics , DISPATCH_TYPE } from "./analytics" ;
3+ import { PerformanceTimer } from "./performance" ;
24import type AssetWorker from "../../asset-worker/src/index" ;
35import type { RoutingConfig } from "../../utils/types" ;
6+ import type {
7+ ColoMetadata ,
8+ Environment ,
9+ ReadyAnalytics ,
10+ UnsafePerformanceTimer ,
11+ } from "./types" ;
412
513interface Env {
614 ASSET_WORKER : Service < AssetWorker > ;
715 USER_WORKER : Fetcher ;
816 CONFIG : RoutingConfig ;
917
1018 SENTRY_DSN : string ;
19+ ENVIRONMENT : Environment ;
20+ ANALYTICS : ReadyAnalytics ;
21+ COLO_METADATA : ColoMetadata ;
22+ UNSAFE_PERFORMANCE : UnsafePerformanceTimer ;
23+ VERSION_METADATA : WorkerVersionMetadata ;
1124
1225 SENTRY_ACCESS_CLIENT_ID : string ;
1326 SENTRY_ACCESS_CLIENT_SECRET : string ;
@@ -16,7 +29,9 @@ interface Env {
1629export default {
1730 async fetch ( request : Request , env : Env , ctx : ExecutionContext ) {
1831 let sentry : ReturnType < typeof setupSentry > | undefined ;
19- const maybeSecondRequest = request . clone ( ) ;
32+ const analytics = new Analytics ( ) ;
33+ const performance = new PerformanceTimer ( env . UNSAFE_PERFORMANCE ) ;
34+ const startTimeMs = performance . now ( ) ;
2035
2136 try {
2237 sentry = setupSentry (
@@ -27,21 +42,50 @@ export default {
2742 env . SENTRY_ACCESS_CLIENT_SECRET
2843 ) ;
2944
45+ const url = new URL ( request . url ) ;
46+ if ( sentry ) {
47+ sentry . setUser ( { username : url . hostname } ) ;
48+ sentry . setTag ( "colo" , env . COLO_METADATA . coloId ) ;
49+ sentry . setTag ( "metal" , env . COLO_METADATA . metalId ) ;
50+ }
51+
52+ if ( env . COLO_METADATA && env . VERSION_METADATA ) {
53+ analytics . setData ( {
54+ coloId : env . COLO_METADATA . coloId ,
55+ metalId : env . COLO_METADATA . metalId ,
56+ coloTier : env . COLO_METADATA . coloTier ,
57+ coloRegion : env . COLO_METADATA . coloRegion ,
58+ hostname : url . hostname ,
59+ version : env . VERSION_METADATA . id ,
60+ } ) ;
61+ }
62+
63+ const maybeSecondRequest = request . clone ( ) ;
3064 if ( env . CONFIG . has_user_worker ) {
3165 if ( await env . ASSET_WORKER . unstable_canFetch ( request ) ) {
66+ analytics . setData ( { dispatchtype : DISPATCH_TYPE . ASSETS } ) ;
3267 return await env . ASSET_WORKER . fetch ( maybeSecondRequest ) ;
3368 } else {
69+ analytics . setData ( { dispatchtype : DISPATCH_TYPE . WORKER } ) ;
3470 return env . USER_WORKER . fetch ( maybeSecondRequest ) ;
3571 }
3672 }
3773
74+ analytics . setData ( { dispatchtype : DISPATCH_TYPE . ASSETS } ) ;
3875 return await env . ASSET_WORKER . fetch ( request ) ;
3976 } catch ( err ) {
77+ if ( err instanceof Error ) {
78+ analytics . setData ( { error : err . message } ) ;
79+ }
80+
4081 // Log to Sentry if we can
4182 if ( sentry ) {
4283 sentry . captureException ( err ) ;
4384 }
4485 throw err ;
86+ } finally {
87+ analytics . setData ( { requestTime : performance . now ( ) - startTimeMs } ) ;
88+ analytics . write ( env . ENVIRONMENT , env . ANALYTICS ) ;
4589 }
4690 } ,
4791} ;
0 commit comments