Reputation: 394
I have an Azure Function (v2) where data is passed in as JSON via the HTTP body. I want to log some of this JSON data in Application Insights, using the standard Trace and Request events.
What I've tried so far:
ITelemetryInitializer
which parses the body and adds properties to ISupportProperties.Properties
. This has 2 disadvantages though: the body is read and parsed multiple times for each request (once in my Function, and multiple times in the telemetry initializer), and sometimes accessing the body throws an exception because it has been disposed (probably it goes out of scope at the end of the Function call).TelemetryClient
inside my Function. But this client doesn't seem to have an appropriate property to set:
TelemetryClient.Context.GlobalProperties
is meant for global properties, and not for request-scoped properties;TelemetryClient.Context.Properties
is obsolete, and I don't see how I can use the recommended replacement ISupportProperties.Properties
there.Ideally I want to use the data which is parsed inside my Function, and use that data to initialize the telemetry data.
Upvotes: 5
Views: 3365
Reputation: 351
You can update request telemetry properties by adding tags on Activity.Current
like Activity.Current?.AddTag("my-prop", ExtractPropFromRequest());
Without any additional changes, these tags will appear on requests. Unfortunately, you won't get them stamped on traces.
You can also parse your request body once in the function and store it in AsyncLocal. Then access this AsyncLocal in the TelemetryInitializer
public class AsyncLocalPropertyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is ISupportProperties propTelemetry &&
Function1.AdditionalContext.Value != null) // you may find a better way to make it work with DI
{
propTelemetry.Properties["my-prop"] = Function1.AdditionalContext.Value;
}
}
}
public static class Function
{
internal static readonly AsyncLocal<string> AdditionalContext = new AsyncLocal<string>();
[FunctionName("Function1")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
AdditionalContext.Value = "something important"; // read the body here
log.LogInformation("C# HTTP trigger function processed a request.")
AdditionalContext.Value = null;
// ...
}
}
}
Upvotes: 6