codigube
codigube

Reputation: 1256

Activity.Current is null in Azure function with BlobTrigger

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?

Upvotes: 4

Views: 2299

Answers (2)

codigube
codigube

Reputation: 1256

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" />

Upvotes: 0

Delliganesh Sevanesan
Delliganesh Sevanesan

Reputation: 4776

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.

Upvotes: 3

Related Questions