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.
Azure Functions can be monitored using Application Insights and Azure Monitor. Though Azure provides such monitoring solutions, users cannot monitor multiple entities with various metrics. Whereas, with Serverless360 monitoring, it is possible to monitor various entities based on metrics at the Application level.
Bindings are optional and a function might have one or multiple input and/or output bindings. Triggers and bindings let you avoid hardcoding access to other services. Your function receives data (for example, the content of a queue message) in function parameters.
In your function app, select Configuration under Settings, and then select Application settings. If you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY , Application Insights integration is enabled for your function app running in Azure.
Use the Application Insights core telemetry API to send custom events and metrics and your own versions of standard telemetry. This API is the same API that the standard Application Insights data collectors use.
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;
// ...
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With