Question: How to properly intercept response body on Enrich method
See original GitHub issueQuestion
I’ve been digging into a way to intercept the response body using the Enrich method available in the AddAspNetCoreInstrumentation
hook.
The thing is that I haven’t found a way to properly get the actual response body into a string version or even an object, the HttpResponse object is available at the OnStopActivity
event name but, turns out, the body stream is kind not ready to be read at that point.
Would like to know if someone has already been able to accomplish that or if there’s even a way of accomplishing it.
Quick code example:
services.AddOpenTelemetryTracing((builder) =>
{
builder
.AddAspNetCoreInstrumentation((options) => options.Enrich = async (activity, eventName, rawObject) =>
{
if (eventName.Equals("OnStartActivity"))
{
// no issues here, was able to properly get the Request Body
if (rawObject is HttpRequest httpRequest)
{
if (httpRequest?.Body != null && httpRequest.Body.CanRead)
{
using (var sr = new StreamReader(httpRequest.Body))
{
var requestBody = await sr.ReadToEndAsync();
activity.SetTag("http.request.body", requestBody);
}
}
activity.SetTag("http.request.headers", httpRequest?.Headers.ToString());
activity.SetTag("requestProtocol", httpRequest.Protocol);
}
}
else if (eventName.Equals("OnStopActivity"))
{
if (rawObject is HttpResponse httpResponse)
{
// heres the part where I'm kinda struggling to get the Response body, Ive tried a few different attempts, like the request one, but not luck yet
}
}
})
})
Describe your environment.
ASP.NET Core 3.1 Web API project (using the weather example that Visual Studio automatically creates).
What are you trying to achieve? Being able to properly intercept the response body so I can add that into the activity as a tag (I bet this is pretty simple to accomplish and it’s just me being a noob at it 😄 )
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (5 by maintainers)
@DavoBR
You can try the below if you want, it seems to work in my limited testing. But I cannot stress enough this is a really really bad awful idea which will destroy the throughput of your service(s) 💣
edit: Look at the comments below pointed by @CodeBlanch before trying this out. This doubles the memory hit for every request/response.
My original comment: One thing that worked well for me was enriching the HttpClient Instrumentation and using HttpRequestMessage and HttpResponseMessage. This way I could use ReadAsStringAsync() for the request & response. My TraceProviderBuilder ended up like the following:
This way I was able to visualize the http.request_content & http.response_content in the UI of my chosen telemetry tool.