0

my startup file looks like this

[assembly: FunctionsStartup(typeof(Startup))]
{ 
 public override void Configure(IFunctionsHostBuilder builder)
{
    var context = builder.GetContext();
    var _config = context.Configuration;

    var telemetryConfig = new TelemetryConfiguration();
    telemetryConfig.ConnectionString = _config.GetConnectionString("ApplicationInsights");

    builder.Services.AddSingleton<TelemetryClient>(x => new TelemetryClient(telemetryConfig));
}
 }

and the settings file looks like this

{
 "IsEncrypted": false,
 "Values": {
   "AzureWebJobsStorage": "UseDevelopmentStorage=true",
   "FUNCTIONS_WORKER_RUNTIME": "dotnet",
   "APPINSIGHTS_INSTRUMENTATIONKEY": "xxxxxx-fbc9-441b-9869-70bcb4afc93a",
   "TimerInterval": "0 */5 * * * *"
 },
 "ConnectionStrings": {
   "ApplicationInsights": "InstrumentationKey=xxxxxx-fbc9-441b-9869- 
70bcb4afc93a;IngestionEndpoint=https://xxx- 
in.applicationinsights.azure.com/;LiveEndpoint=https://xxxxxx.livediagnostics.monitor.azure.c 
 om/"    }
}

problem here is that telemetryConfig is set properly i can see the InstrumentationKey and ConnString

but new TelemetryClient(telemetryConfig) has InstrumentationKey empty and most of the properties set to null.

even if i set InstrumentationKey directly (which is obsolete) the dependency injected value in the code has empty InstrumentationKey

in code i am using it like this

 private readonly TelemetryClient _telemetry;

    public FHIRExtract(ILogger<FHIRExtract> logger, TelemetryClient telemetry, IConfiguration configuration)
    {
        _logger = logger;
        _config = configuration;
        _telemetry = telemetry;
    }

    [FunctionName("FHIRExtract")]
    public async Task Run([TimerTrigger("%TimerInterval%"

_telemetry has InstrumentationKey empty

but when i set this value in the code

//_telemetry.InstrumentationKey = "xxxxxx-fbc9-441b-9869-70bcb4afc93a"; it works and i am able to send telemetry data

I am trying to use non obsolete functions to configure Telemetry client and want to use it by DI. what am i doing wrong ?

Raas Masood
  • 1,475
  • 3
  • 23
  • 61

1 Answers1

0

You shouldn't setup Application Insights this way.

  1. Install the Microsoft.Azure.WebJobs.Logging.ApplicationInsights NuGet package in the azure functions project
  2. In the Configure override add the logging:
    builder.Services.AddLogging();
    
  3. Configure the instrumentatio key by setting APPINSIGHTS_INSTRUMENTATIONKEY in the settings.json file
  4. Inject a TelemetryConfiguration in your function and create an instance of TelemetryClient:
     public FHIRExtract(ILogger<FHIRExtract> logger, TelemetryConfiguration 
                        telemetryConfig, IConfiguration configuration)
     {
         _logger = logger;
         _config = configuration;
         _telemetry = new TelemetryClient(telemetryConfig);
     }
    

For some reason the custom telemetry logging in azure functions breaks when only setting APPLICATIONINSIGHTS_CONNECTION_STRING in the configuration but I am sure that will be fixed in the near future. In my experience the application insights integration always lags behind a bit.

References:

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • same thing is happening here i have telemetryConfig properly populated with Instrumentation key but same property is empty in _telemetry – Raas Masood Feb 09 '23 at 17:01