Skip to content

Commit 705ffa7

Browse files
authored
fix: exclude 4xx client errors from Application Insights failures (#2865)
* fix: exclude 4xx client errors from Application Insights failures Add TelemetryProcessor to mark 4xx responses as success=true. Only 5xx server errors should appear in the Failures dashboard. This reduces noise from expected client errors (400 Bad Request, 401 Unauthorized, 404 Not Found) in failure metrics and alerts. * fix: use correct type for responseCode (string, not number) Application Insights SDK types define responseCode as string. Use parseInt() for proper type conversion before comparison.
1 parent 485c191 commit 705ffa7

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/main.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ async function bootstrap() {
2626
if (process.env.APPINSIGHTS_INSTRUMENTATIONKEY) {
2727
AppInsights.setup().setAutoDependencyCorrelation(true).setAutoCollectConsole(true, true);
2828
AppInsights.defaultClient.context.tags[AppInsights.defaultClient.context.keys.cloudRole] = 'dfx-api';
29+
30+
// Don't mark 4xx client errors as failures - only 5xx are real server errors
31+
AppInsights.defaultClient.addTelemetryProcessor((envelope) => {
32+
const data = envelope.data as { baseType?: string; baseData?: { responseCode?: string; success?: boolean } };
33+
if (data.baseType === 'RequestData' && data.baseData?.responseCode) {
34+
const responseCode = parseInt(data.baseData.responseCode, 10);
35+
if (responseCode >= 400 && responseCode < 500) {
36+
data.baseData.success = true;
37+
}
38+
}
39+
return true;
40+
});
41+
2942
AppInsights.start();
3043
}
3144

0 commit comments

Comments
 (0)