Skip to content

Commit 2b5dfce

Browse files
dromanolclaude
andcommitted
Narrow the warning to the third-party endpoint getter
Downgrading the whole catch also suppressed error telemetry for defects in our own collection path (EndpointsCollection.CollectEndpoints, ReportEndpoints, duck casts), which the logging guidance requires to stay at Log.Error. Wrap only the EndpointDataSource.Endpoints evaluation, the third-party code we cannot control, and restore Log.Error on the outer catch. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 186e74f commit 2b5dfce

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/AspNetCore/EndpointsCollection/KestrelServerImplStartAsyncIntegration.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#if !NETFRAMEWORK
99

1010
using System;
11+
using System.Collections.Generic;
1112
using System.ComponentModel;
1213
using System.Threading;
1314
using Datadog.Trace.AppSec;
@@ -57,12 +58,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TApplication>(TTarget ins
5758
}
5859
catch (Exception ex)
5960
{
60-
// Reading EndpointDataSource.Endpoints runs third-party code that can throw for reasons
61-
// outside our control (e.g. the Azure Functions worker builds its endpoints lazily and
62-
// rejects a malformed route template). Endpoints collection is best-effort, so a failure
63-
// here degrades the feature without affecting the application: warn rather than report
64-
// an error to telemetry.
65-
Log.Warning(ex, "API Security: Failed to collect endpoints.");
61+
Log.Error(ex, "API Security: Failed to collect endpoints.");
6662
}
6763

6864
return CallTargetState.GetDefault();
@@ -89,7 +85,24 @@ private static void GatherEndpoints(IKestrelServer kestrelServer)
8985
return;
9086
}
9187

92-
AppSec.EndpointsCollection.CollectEndpoints(endpointDataSource.Endpoints);
88+
IReadOnlyList<object> endpoints;
89+
90+
try
91+
{
92+
endpoints = endpointDataSource.Endpoints;
93+
}
94+
catch (Exception ex)
95+
{
96+
// Evaluating this property runs third-party code that can throw for reasons outside our
97+
// control. The Azure Functions worker, for instance, builds its endpoints lazily from
98+
// function metadata and rejects malformed route templates, so we are the first caller to
99+
// trigger the failure. That is not a tracer defect, so warn instead of reporting an error
100+
// to telemetry. Anything thrown by our own collection below is a defect and still errors.
101+
Log.Warning(ex, "API Security: Endpoints collection: Failed to evaluate the EndpointDataSource endpoints.");
102+
return;
103+
}
104+
105+
AppSec.EndpointsCollection.CollectEndpoints(endpoints);
93106
}
94107
}
95108

0 commit comments

Comments
 (0)