@@ -17,6 +17,41 @@ const app: any = express();
1717
1818app . use ( bodyParser . json ( ) ) ;
1919
20+ // Basic request logging for all incoming job invocations.
21+ app . use ( ( req : any , res : any , next : any ) => {
22+ try {
23+ // Log only the headers we care about plus a shallow body snapshot
24+ const headers = {
25+ 'x-worker-id' : req . get ( 'X-Worker-Id' ) ,
26+ 'x-job-id' : req . get ( 'X-Job-Id' ) ,
27+ 'x-database-id' : req . get ( 'X-Database-Id' ) ,
28+ 'x-callback-url' : req . get ( 'X-Callback-Url' )
29+ } ;
30+
31+ let body : any ;
32+ if ( req . body && typeof req . body === 'object' ) {
33+ // Only log top-level keys to avoid exposing sensitive body contents.
34+ body = { keys : Object . keys ( req . body ) } ;
35+ } else if ( typeof req . body === 'string' ) {
36+ // For string bodies, log only the length.
37+ body = { length : req . body . length } ;
38+ } else {
39+ body = undefined ;
40+ }
41+
42+ // eslint-disable-next-line no-console
43+ console . log ( '[knative-job-fn] Incoming job request' , {
44+ method : req . method ,
45+ path : req . originalUrl || req . url ,
46+ headers,
47+ body
48+ } ) ;
49+ } catch {
50+ // best-effort logging; never block the request
51+ }
52+ next ( ) ;
53+ } ) ;
54+
2055// Echo job headers back on responses for debugging/traceability.
2156app . use ( ( req : any , res : any , next : any ) => {
2257 res . set ( {
@@ -151,6 +186,13 @@ app.use((req: any, res: any, next: any) => {
151186 // If an error handler already sent a callback, skip.
152187 if ( res . locals . jobCallbackSent ) return ;
153188 res . locals . jobCallbackSent = true ;
189+ // eslint-disable-next-line no-console
190+ console . log ( '[knative-job-fn] Function completed' , {
191+ workerId : ctx . workerId ,
192+ jobId : ctx . jobId ,
193+ databaseId : ctx . databaseId ,
194+ statusCode : res . statusCode
195+ } ) ;
154196 void sendJobCallback ( ctx , 'success' ) ;
155197 } ) ;
156198 }
@@ -185,6 +227,41 @@ export default {
185227 console . error ( '[knative-job-fn] Failed to send error callback' , err ) ;
186228 }
187229
230+ // Log the full error context for debugging.
231+ try {
232+ const headers = {
233+ 'x-worker-id' : req . get ( 'X-Worker-Id' ) ,
234+ 'x-job-id' : req . get ( 'X-Job-Id' ) ,
235+ 'x-database-id' : req . get ( 'X-Database-Id' ) ,
236+ 'x-callback-url' : req . get ( 'X-Callback-Url' )
237+ } ;
238+
239+ // Some error types (e.g. GraphQL ClientError) expose response info.
240+ const errorDetails : any = {
241+ message : error ?. message ,
242+ name : error ?. name ,
243+ stack : error ?. stack
244+ } ;
245+
246+ if ( error ?. response ) {
247+ errorDetails . response = {
248+ status : error . response . status ,
249+ statusText : error . response . statusText ,
250+ errors : error . response . errors ,
251+ data : error . response . data
252+ } ;
253+ }
254+
255+ // eslint-disable-next-line no-console
256+ console . error ( '[knative-job-fn] Function error' , {
257+ headers,
258+ path : req . originalUrl || req . url ,
259+ error : errorDetails
260+ } ) ;
261+ } catch {
262+ // never throw from the error logger
263+ }
264+
188265 res . status ( 200 ) . json ( { message : error . message } ) ;
189266 } ) ;
190267 app . listen ( port , cb ) ;
0 commit comments