55 formatLog ,
66 getErrorContext ,
77 getRequestId ,
8+ type LogContext ,
89} from "@embedly/logging" ;
910import { Platforms } from "@embedly/platforms" ;
1011import { httpInstrumentationMiddleware } from "@hono/otel" ;
@@ -21,6 +22,21 @@ import { version } from "../package.json";
2122
2223type ScrapeResponse = Awaited < ReturnType < ( typeof Platforms ) [ keyof typeof Platforms ] [ "transform" ] > > ;
2324
25+ interface ApiLogContext extends LogContext {
26+ request_id : string ;
27+ trace_id ?: string ;
28+ span_id ?: string ;
29+ source : string ;
30+ platform : string ;
31+ post_id : string ;
32+ force : boolean ;
33+ cache_status : "skipped" | "miss" | "hit" | "read_error" | "stored" | "write_error" ;
34+ outcome : "success" | "error" ;
35+ status_code : number ;
36+ error_type ?: string ;
37+ duration_ms ?: number ;
38+ }
39+
2440const config : ResolveConfigFn < CloudflareBindings > = ( env ) => {
2541 if ( ! env . OTEL_ENDPOINT ) throw new Error ( "OTEL_ENDPOINT is required." ) ;
2642
@@ -52,7 +68,7 @@ const app = new Hono<{ Bindings: CloudflareBindings }>()
5268 const { id, platform, force } = c . req . valid ( "json" ) ;
5369 const requestId = getRequestId ( c . req . raw ) ;
5470 const spanContext = trace . getActiveSpan ( ) ?. spanContext ( ) ;
55- const logContext : Record < string , unknown > = {
71+ const logContext : ApiLogContext = {
5672 request_id : requestId ,
5773 trace_id : spanContext ?. traceId ,
5874 span_id : spanContext ?. spanId ,
@@ -73,25 +89,20 @@ const app = new Hono<{ Bindings: CloudflareBindings }>()
7389 const cachedItem = await cache . get < ScrapeResponse > ( cacheKey , "json" ) ;
7490 if ( cachedItem ) {
7591 logContext . cache_status = "hit" ;
76- return c . json ( cachedItem as ScrapeResponse , 200 ) ;
92+ return c . json ( cachedItem , 200 ) ;
7793 }
7894 } catch ( cause ) {
79- const problem = createProblem ( EmbedlyErrors . CacheReadFailed , {
80- request_id : requestId ,
81- context : { ...logContext , ...getErrorContext ( cause ) } ,
82- } ) ;
83- Object . assign ( logContext , getErrorContext ( cause ) , {
84- outcome : "error" ,
85- status_code : problem . status ,
86- error_type : problem . type ,
87- } ) ;
88- return c . json ( problem , problem . status ) ;
95+ logContext . cache_status = "read_error" ;
96+ console . warn (
97+ formatLog ( "warn" , EmbedlyErrors . CacheReadFailed , {
98+ ...logContext ,
99+ ...getErrorContext ( cause ) ,
100+ } ) ,
101+ ) ;
89102 }
90103 }
91104
92- // oxlint-disable-next-line import/namespace
93- const p = Platforms [ platform as keyof typeof Platforms ] ;
94- if ( ! p ) {
105+ if ( ! Object . hasOwn ( Platforms , platform ) ) {
95106 const problem = createProblem ( EmbedlyErrors . NoMatchesFound , {
96107 request_id : requestId ,
97108 context : logContext ,
@@ -104,6 +115,8 @@ const app = new Hono<{ Bindings: CloudflareBindings }>()
104115 } ) ;
105116 return c . json ( problem , problem . status ) ;
106117 }
118+ // oxlint-disable-next-line import/namespace -- SAFETY: Object.hasOwn verified this registry key.
119+ const p = Platforms [ platform as keyof typeof Platforms ] ;
107120
108121 let raw : unknown ;
109122 try {
@@ -125,6 +138,7 @@ const app = new Hono<{ Bindings: CloudflareBindings }>()
125138
126139 let data : ScrapeResponse ;
127140 try {
141+ // SAFETY: raw came from this platform's fetch implementation.
128142 data = await p . transform ( raw as any ) ;
129143 } catch ( cause ) {
130144 const problem = createProblem ( EmbedlyErrors . PlatformTransformFailed , {
@@ -145,16 +159,13 @@ const app = new Hono<{ Bindings: CloudflareBindings }>()
145159 } ) ;
146160 logContext . cache_status = "stored" ;
147161 } catch ( cause ) {
148- const problem = createProblem ( EmbedlyErrors . CacheWriteFailed , {
149- request_id : requestId ,
150- context : { ...logContext , ...getErrorContext ( cause ) } ,
151- } ) ;
152- Object . assign ( logContext , getErrorContext ( cause ) , {
153- outcome : "error" ,
154- status_code : problem . status ,
155- error_type : problem . type ,
156- } ) ;
157- return c . json ( problem , problem . status ) ;
162+ logContext . cache_status = "write_error" ;
163+ console . warn (
164+ formatLog ( "warn" , EmbedlyErrors . CacheWriteFailed , {
165+ ...logContext ,
166+ ...getErrorContext ( cause ) ,
167+ } ) ,
168+ ) ;
158169 }
159170
160171 return c . json ( data , 200 ) ;
0 commit comments