3

I'm trying to enhance RequestTelemetry in AppInsights from HttpTrigger Azure Function v3.

Function is initialized with DI and Startup class.

[assembly: FunctionsStartup(typeof(Startup))]
namespace Hager.Example.FunctionApp.FunctionApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            // No service for repro
        }
    }
}

And my Function

    public class Function1
    {
        private readonly ILogger _logger;

        public Function1(ILogger<Function1> logger)
        {
            _logger = logger;
        }

        [FunctionName("HttpTriggered")]
        public IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req)
        {
            using var loggerScope = _logger.BeginScope("{InScope1}{InScope2}{InScope3}", Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid());

            _logger.LogInformation("Started Execution");
            _logger.LogWarning("With a custom property: {CustomProperty}.", Guid.NewGuid());

            Activity.Current?.AddTag("TagStart", Guid.NewGuid());

            if (Activity.Current == null)
            {
                // Always null
                _logger.LogError("No ActivityCurrent {Activity}.", Activity.Current);
                _logger.LogError("ActivityCurrent Tags {Activity}.", Activity.Current?.Tags);
            }

            // Activity.Current.AddTag("Tag2", Guid.NewGuid());  // <- NullException

            _logger.LogInformation("Finished Execution");

            return new NoContentResult();
        }
    }

My project packages:

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.17.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.17.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
  </ItemGroup>

Every logger and scopes are working as expected, but object ActivityTag is always null that shouldnt in Azure Functions.

Did I miss something?

jootl
  • 699
  • 2
  • 8
  • 16

1 Answers1

1

Update:

Added Op's solution: by using request.HttpContext?.Features.Get<RequestTelemetry>(), it worked fine.


Please uninstall the 2 Application Insights packages: Microsoft.ApplicationInsights and Microsoft.ApplicationInsights.AspNetCore.

By default, Application Insights packages do not collect activity tags. So this should be the reason.

I tested your azure function without installing the above 2 Application Insights packages, it works well. Here is the screenshot of the test result:

enter image description here

Adding my local.settings.json here for your reference, the code is the same as yours:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "xxxx",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "xxx"
  }
}

And if the 2 packages are necessary, maybe you can try add a custom ITelemetryInitializer by following this answer(btw, I didn't test it).

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • Thanks for the answer but it doesnt work for me... i dont understand... maybe a difference in framework version... i ended it up by using `request.HttpContext?.Features.Get()`, it worked fine – jootl Mar 19 '21 at 14:51
  • @JTE, it might be. I added your solution in the answer. And if it's helpful, could you please accept it as answer? Thanks. If you want to figure out the root cause, we can share our codes via github by removing the personal information, then I can continue working on it. – Ivan Glasenberg Mar 22 '21 at 06:28