Skip to content

Commit 1fe093a

Browse files
authored
Merge pull request #448 from constructive-io/anmol/fix-function-job
fix function job, add more logs in middleware
2 parents 455f218 + d4c4c62 commit 1fe093a

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

functions/send-email-link/src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,22 @@ const getRequiredEnv = (name: string): string => {
7171
return value;
7272
};
7373

74-
const createGraphQLClient = (url: string): GraphQLClient => {
74+
const createGraphQLClient = (
75+
url: string,
76+
hostHeaderEnvVar?: string
77+
): GraphQLClient => {
7578
const headers: Record<string, string> = {};
7679

7780
if (process.env.GRAPHQL_AUTH_TOKEN) {
7881
headers.Authorization = `Bearer ${process.env.GRAPHQL_AUTH_TOKEN}`;
7982
}
8083

84+
const envName = hostHeaderEnvVar || 'GRAPHQL_HOST_HEADER';
85+
const hostHeader = process.env[envName];
86+
if (hostHeader) {
87+
headers.host = hostHeader;
88+
}
89+
8190
return new GraphQLClient(url, { headers });
8291
};
8392

@@ -275,8 +284,8 @@ app.post('*', async (req: any, res: any, next: any) => {
275284
const graphqlUrl = getRequiredEnv('GRAPHQL_URL');
276285
const metaGraphqlUrl = process.env.META_GRAPHQL_URL || graphqlUrl;
277286

278-
const client = createGraphQLClient(graphqlUrl);
279-
const meta = createGraphQLClient(metaGraphqlUrl);
287+
const client = createGraphQLClient(graphqlUrl, 'GRAPHQL_HOST_HEADER');
288+
const meta = createGraphQLClient(metaGraphqlUrl, 'META_GRAPHQL_HOST_HEADER');
280289

281290
const result = await sendEmailLink(params, {
282291
client,

jobs/knative-job-fn/src/index.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,41 @@ const app: any = express();
1717

1818
app.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.
2156
app.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

Comments
 (0)