Skip to content

Commit 07bc173

Browse files
committed
Better logging around end-of-challenge calculations
1 parent 19ff166 commit 07bc173

File tree

2 files changed

+294
-57
lines changed

2 files changed

+294
-57
lines changed

src/autopilot/services/review-summation-api.service.ts

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ export class ReviewSummationApiService {
4141
return `${normalizedBase}${normalizedPath}`;
4242
}
4343

44+
private sanitizeHeaders(
45+
headers: Record<string, unknown> | undefined,
46+
): Record<string, unknown> | undefined {
47+
if (!headers) {
48+
return undefined;
49+
}
50+
51+
return Object.entries(headers).reduce<Record<string, unknown>>(
52+
(sanitized, [key, value]) => {
53+
const lowerKey = key.toLowerCase();
54+
if (lowerKey === 'authorization' || lowerKey === 'set-cookie') {
55+
sanitized[key] = '[redacted]';
56+
} else {
57+
sanitized[key] = value;
58+
}
59+
60+
return sanitized;
61+
},
62+
{},
63+
);
64+
}
65+
4466
async finalizeSummations(challengeId: string): Promise<boolean> {
4567
const url = this.buildUrl(
4668
`/reviewSummations/challenges/${challengeId}/final`,
@@ -59,27 +81,62 @@ export class ReviewSummationApiService {
5981
}
6082

6183
let token: string | undefined;
84+
const requestLog: {
85+
method: string;
86+
url: string;
87+
body: null;
88+
headers: Record<string, unknown>;
89+
timeoutMs: number;
90+
} = {
91+
method: 'POST',
92+
url,
93+
body: null,
94+
headers: {
95+
Authorization: '[not available]',
96+
'Content-Type': 'application/json',
97+
},
98+
timeoutMs: this.timeoutMs,
99+
};
62100

63101
try {
64102
token = await this.auth0Service.getAccessToken();
103+
if (token) {
104+
requestLog.headers.Authorization = 'Bearer [redacted]';
105+
}
106+
107+
const axiosHeaders: Record<string, string> = {
108+
'Content-Type': 'application/json',
109+
};
110+
if (token) {
111+
axiosHeaders.Authorization = `Bearer ${token}`;
112+
}
113+
114+
const axiosConfig = {
115+
headers: axiosHeaders,
116+
timeout: this.timeoutMs,
117+
};
118+
65119
const response = await firstValueFrom(
66-
this.httpService.post(url, undefined, {
67-
headers: {
68-
Authorization: `Bearer ${token}`,
69-
'Content-Type': 'application/json',
70-
},
71-
timeout: this.timeoutMs,
72-
}),
120+
this.httpService.post(url, undefined, axiosConfig),
73121
);
74122

75123
const status = response.status;
124+
const sanitizedResponseHeaders = this.sanitizeHeaders(
125+
response.headers as Record<string, unknown> | undefined,
126+
);
76127
await this.dbLogger.logAction('reviewSummation.finalize', {
77128
challengeId,
78129
status: 'SUCCESS',
79130
source: ReviewSummationApiService.name,
80131
details: {
81132
url,
82133
status,
134+
request: requestLog,
135+
response: {
136+
status,
137+
data: response.data ?? null,
138+
headers: sanitizedResponseHeaders,
139+
},
83140
},
84141
});
85142

@@ -92,6 +149,9 @@ export class ReviewSummationApiService {
92149
const message = err?.message || 'Unknown error';
93150
const status = err?.response?.status;
94151
const data = err?.response?.data;
152+
const sanitizedResponseHeaders = this.sanitizeHeaders(
153+
err?.response?.headers,
154+
);
95155

96156
this.logger.error(
97157
`Failed to finalize review summations for challenge ${challengeId}: ${message}`,
@@ -104,9 +164,11 @@ export class ReviewSummationApiService {
104164
source: ReviewSummationApiService.name,
105165
details: {
106166
url,
167+
request: requestLog,
107168
error: message,
108169
status,
109170
response: data,
171+
responseHeaders: sanitizedResponseHeaders,
110172
},
111173
});
112174

0 commit comments

Comments
 (0)