How to selectively filter HttpClient Instrumentation? #5524
-
When adding HttpClient Instrumentation, we can use the public sealed class SplunkEventCollectorFilteringProcessor(string _address, string _method, string _statusCode) : BaseProcessor<Activity>
{
public override void OnEnd(Activity activity)
{
bool address = false;
bool method = false;
bool statusCode = false;
foreach (var tag in activity.Tags)
{
switch (tag.Key)
{
case "server.address": address = tag.Value == _address; break;
case "http.request.method": method = tag.Value == _method; break;
case "http.response.status_code": statusCode = tag.Value == _statusCode; break;
}
}
if (address && method && statusCode)
{
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded; // drop the activity
}
}
} However that will run on all activities, so should this work? .AddHttpClientInstrumentation(options =>
{
options.EnrichWithHttpResponseMessage = (activity, response) =>
{
if (!response.IsSuccessStatusCode)
{
var request = response.RequestMessage;
if (request?.RequestUri?.Host == "splunk.host.name" && request.Method == HttpMethod.Post)
{
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded; // drop the activity
}
}
};
}) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Enrich is intended for enrichment, not filtering (though, it can technically be used as your example demonstrated.) |
Beta Was this translation helpful? Give feedback.
https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.Http#filter-httpclient-api
Enrich is intended for enrichment, not filtering (though, it can technically be used as your example demonstrated.)
For you use-case, use the filtering capability provided by the instrumentation itself, as it'll likely be faster than the general purpose
ActivityProcessor
approach!