I'm using Azure Functions with Automatic Dependency Tracking disabled and manually tracking my dependencies.
I also use the SDK provided ILogger
for traces and metrics, and also heavily use the ILogger.BeginScope(...)
to track details about operations.
Is it possible to get the current scope properties so I can add them to my dependency tracking?
Example would be
using(var scope = log.BeginScope("{document}{user}", documentId, userId)){
// do HTTP Call using HttpClient
}
HTTP Client Config
.AddHttpMessageHandler(serviceProvider => new
DependencyLoggingMessageHandler(serviceProvider.GetRequiredService<TelemetryClient>()))
And the Message Handler
public class DependencyLoggingMessageHandler : DelegatingHandler
{
private readonly TelemetryClient client;
public DependencyLoggingMessageHandler(TelemetryClient client)
{
this.client = client;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
try
{
response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
return response;
}
finally
{
client.TrackDependency(new DependencyTelemetry(..){
Data = ...,
Properties = /*how to get scope props? */
});
}
}
}
In the AppInsights dependency created for this, I would like to have document
and user
as properties like the traces and metrics created by the SDK.
Update:
Currently I have 2 (questionable) solutions
- If you want a no frills, easy implementation, use
LogMetric("HTTP_REQUEST", duration, properties)
which will have the scope properties as usual and add your things to its properties.
Sure it won't show up in app insights dependencies, but you can have the duration of the HTTP request as its value, have the url, host .etc. as custom properties and get most things done. My main requirement is to track duration and urls, which works in this solution so I have settled for this solution for now.
- Maintain the scope properties yourself
I think this can be done with essentially duplicating what the Functions Runtime does. i.e Using a custom version of it in a AsyncLocal
and iterating from current scope to the root scope and adding them to your telemetry like the runtime does. Looks like a lot more work than its worth to me.