Search code examples
c#azureazure-functionsazure-application-insights

Add Custom Properties for RequestTelemetry of the Azure Function (v3) binding to BlobTrigger


I want to add Custom Properties to the RequestTelemetry generated for the Azure function(V3) written in C#.

Thanks to this StackOverflow post, I managed to achieve this for the function with HttpTrigger binding.

var requestTelemetry = req.HttpContext?.Features.Get<RequestTelemetry>();
requestTelemetry.Properties.Add("MyProp", "Some value");

However, when I try to do same thing for another function with BlobTrigger binding, it became confusing. The first challenge is:

How to get current RequestTelemetry in a function that is using BlobTrigger binding?

In a function with HttpTrigger binding, the function is injected with HttpRequest parameter

public async Task<IActionResult> Upload(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
            string name,
            ILogger log,
            ExecutionContext context)
        {  
  ...
}

So we can get current RequestTelemetry using HttpRequest. However, what about a function with BlobTrigger:

    [FunctionName("Create-Thumbnail")]
            public async Task CreateThumbnail([BlobTrigger("input/{name}",  Source = BlobTriggerSource.EventGrid, Connection = "AzureWebJobsStorage")] Stream image,
                IDictionary<string,string> metadata,
                string name,
                ExecutionContext context)
            { 
  ...
}

I have tried injecting HttpRequest and using same way as HttpTrigger function. But it didn't work.

After hours extensive research, I couldn't find any documentation or posts that are related to this question.

Can anyone provide some tips for this?


Solution

  • AFAIK there is no http request when using a blob trigger. You can still add custom properties to the telemetry generated during the execution by setting properties like this:

    // The current telemetry item gets a property. 
    // Useful if the function is not triggered by an http request
    Activity.Current.AddTag("setUsingTag", "setUsingTag");
    
    // Subsequent telemetry gets this property attached
    Activity.Current.AddBaggage("setUsingActivityBaggage", "setUsingActivityBaggage"); 
    

    When using Baggage instead of a Tag the custom property is added to all telemetry generated during the execution, like dependency calls etc.

    See also this github thread. It also mentions there might be a bug introduced in a later version of AI that might force you to downgrade AI for this to work.


    Thanks to this GitHub issue, this is finally working after downgrading System.Diagnostics.DiagnosticSource to version 4.6

    <PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.6.0" />