Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom telemetry properties inside Azure Function

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:

  • Use a custom 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).
  • Use an 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.

like image 737
Joost Avatar asked Jul 02 '19 11:07

Joost


People also ask

How do I monitor my Azure function?

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.

Can an azure function have multiple input bindings?

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.

How do I enable application insights in Azure function app?

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.

Which application insights data type should you use to capture the custom order telemetry?

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.


1 Answers

  1. 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.

  2. 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;
      // ...
    }
  }
}
like image 57
Liudmila Molkova Avatar answered Sep 24 '22 21:09

Liudmila Molkova