3

I have an Azure function(V3) that uses BlobTrigger binding and written in C#.

In order to add custom properties in Application Insights RequestTelemetry for it using

        Activity.Current?.AddTag("TraceId", traceId);

I need to access the Activity.Current based on the suggestion from this Stackoverflow answer. However, it didn't work due to Activity.Current is NULL.

My package configuration looks like as follow:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Storage.Blobs" Version="12.9.0" />
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0-beta.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.14" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.8" />
    <PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
    <PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

And my function looks like as follow:

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

I have been doing research for whole day but failed to find any solution. Does anyone know what could be the issue?

codigube
  • 1,186
  • 1
  • 12
  • 31
  • 2
    Can you try creating a new Activity (you can also initialize it with a proper traceId - it should be in context)? Also, if you'd like it to appear on your Request telemetry then I think you also will need to add a TelemetryInitializer which will copy over tags from Activity. – ZakiMa Aug 23 '21 at 07:01
  • 1
    Hi, I have created new Activity but it doesn't seem to work. Nothing has changed after this addition. – codigube Aug 23 '21 at 21:12
  • 1
    Have you created and registered TelemetryInitializer? Please refer to this answer: https://stackoverflow.com/a/59404790/3646920 – ZakiMa Aug 24 '21 at 03:31
  • 1
    Yes. I have done that and then tried both creating new Activity or using Activity.Current. And it didn't add anything new in the Application insights – codigube Aug 24 '21 at 09:54
  • 1
    Can you please update your question with these steps? – ZakiMa Aug 24 '21 at 17:12

2 Answers2

3

Currently it can be working in HTTP Trigger Functions that were ending up in the Custom Properties of Requests in application insights are no longer. Refer here

The same Activity.Current value is null issue available in github Azure Function host & Application Insights

The Application Insights .NET SDK uses DiagnosticSource (DiagnosticSourceUsersGuide) and Activity (ActivityUserGuide) steps to collect and correlate telemetry.

Please open an issue in the Functions repo link here.

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15
  • 2
    Hi, a little bit confused by your answer. Do you mean it is not possible to use Activity.Current to access current RequestTelemetry right now in a function with non-HTTP trigger? – codigube Aug 23 '21 at 12:35
  • 3
    Hi @codigube, Yes it is not possible in non-http trigger. Only possible in Http-trigger but achieving this we need to downgrade Microsoft.ApplicationInsights to 3.0.14. The bug(Activity.Current is null) still available in GitHub. – Delliganesh Sevanesan Aug 24 '21 at 03:37
  • 1
    Thanks for clarification. Do you know if there is anyway to access the RequestTelemetry in non-http trigger since I want to add custom property to RequestTelemetry for the function. – codigube Aug 24 '21 at 11:00
  • 2
    Currently not possible please open an issue in Function Repo https://github.com/Azure/azure-webjobs-sdk – Delliganesh Sevanesan Aug 24 '21 at 11:04
0

Thanks to this GitHub issue, I manage to get it work after downgrading System.Diagnostics.DiagnosticSource to to version 4.6

<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.6.0" />
codigube
  • 1,186
  • 1
  • 12
  • 31