Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.94k stars 442 forks source link

Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' #5530

Closed scott-lin closed 4 years ago

scott-lin commented 4 years ago

I am trying to run my Azure Function locally in Visual Studio 2019. I am relying on AppInsights + dependency injection to get an instance of TelemetryConfiguration, so I can construct a TelementryClient for logging.

When I run my function locally, I run into this error,

Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration'

I tried the suggestions in related issues (#4682 and https://github.com/MicrosoftDocs/azure-docs/issues/35181), however, I had no luck. Namely, I tried installing downgraded versions of AppInsights nuget package. See repro steps for full details.

Edit: I tried running in Azure instead of just locally, and it fails there as well.

Investigative information

Repro steps

  1. Create a function app in Visual Studio 2019. I created a TimerTrigger function.
  2. Install Application Insights nuget package if not done so already. Package "Microsoft.ApplicationInsights" and v2.12.0 (I have also tried versions 2.9.1, 2.10.0 and 2.11.0 as suggested by the aforementioned issues)
  3. In your function constructor, add a parameter for TelemetryConfiguration similar to
    public MyFunction( TelemetryConfiguration telemetryConfiguration ) { ... }
  4. Add an entry for APPINSIGHTS_INSTRUMENTATIONKEY environment variable to local.settings.json
  5. Run the function via F5

Here is a complete picture of my function project's nuget dependencies:

    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.12.0" />
    <PackageReference Include="Microsoft.Azure.ServiceBus" Version="4.1.1" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.3.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.5" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.1.3" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.7.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.2" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="RestSharp" Version="106.10.0" />

Expected behavior

After performing step 5, I expect the function to start and my break point in the constructor to be hit.

Actual behavior

Azure Functions Core Tools terminal shows error

"Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' while attempting to activate 'DocfxPipelineFunctions.CategoryHydrationFunction'."

Here is the complete transcript from the terminal in case it helps:

Azure Functions Core Tools (2.7.1948 Commit hash: 29a0626ded3ae99c4111f66763f27bb9fb564103)
Function Runtime Version: 2.0.12888.0
[1/23/2020 10:23:54 PM] Building host: startup suppressed: 'False', configuration suppressed: 'False', startup operation id: '845b6b4e-bed0-4160-af4b-77fa76030f85'
[1/23/2020 10:23:55 PM] Reading host configuration file 'C:\Users\sclin\Repos\PIE-AECore-EngineeringHub\docfxpipeline\DocfxPipelineFunctions\bin\Debug\netcoreapp2.1\host.json'
[1/23/2020 10:23:55 PM] Host configuration file read:
[1/23/2020 10:23:55 PM] {
[1/23/2020 10:23:55 PM]   "version": "2.0"
[1/23/2020 10:23:55 PM] }
[1/23/2020 10:23:55 PM] Reading functions metadata
[1/23/2020 10:23:55 PM] 1 functions found
[1/23/2020 10:23:55 PM] Loading startup extension 'AzureStorage'
[1/23/2020 10:23:55 PM] Loaded extension 'AzureStorage' (3.0.10.0)
[1/23/2020 10:23:57 PM] Initializing Warmup Extension.
[1/23/2020 10:23:57 PM] Initializing Host. OperationId: '845b6b4e-bed0-4160-af4b-77fa76030f85'.
[1/23/2020 10:23:57 PM] Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=845b6b4e-bed0-4160-af4b-77fa76030f85
[1/23/2020 10:23:57 PM] ApplicationInsightsLoggerOptions
[1/23/2020 10:23:57 PM] {
[1/23/2020 10:23:57 PM]   "SamplingSettings": {
[1/23/2020 10:23:57 PM]     "EvaluationInterval": "00:00:15",
[1/23/2020 10:23:57 PM]     "InitialSamplingPercentage": 100.0,
[1/23/2020 10:23:57 PM]     "MaxSamplingPercentage": 100.0,
[1/23/2020 10:23:57 PM]     "MaxTelemetryItemsPerSecond": 20.0,
[1/23/2020 10:23:57 PM]     "MinSamplingPercentage": 0.1,
[1/23/2020 10:23:57 PM]     "MovingAverageRatio": 0.25,
[1/23/2020 10:23:57 PM]     "SamplingPercentageDecreaseTimeout": "00:02:00",
[1/23/2020 10:23:57 PM]     "SamplingPercentageIncreaseTimeout": "00:15:00"
[1/23/2020 10:23:57 PM]   },
[1/23/2020 10:23:57 PM]   "SamplingExcludedTypes": null,
[1/23/2020 10:23:57 PM]   "SamplingIncludedTypes": null,
[1/23/2020 10:23:57 PM]   "SnapshotConfiguration": null,
[1/23/2020 10:23:57 PM]   "EnablePerformanceCountersCollection": true,
[1/23/2020 10:23:57 PM]   "HttpAutoCollectionOptions": {
[1/23/2020 10:23:57 PM]     "EnableHttpTriggerExtendedInfoCollection": true,
[1/23/2020 10:23:57 PM]     "EnableW3CDistributedTracing": true,
[1/23/2020 10:23:57 PM]     "EnableResponseHeaderInjection": true
[1/23/2020 10:23:57 PM]   },
[1/23/2020 10:23:57 PM]   "LiveMetricsInitializationDelay": "00:00:15",
[1/23/2020 10:23:57 PM]   "EnableLiveMetrics": true,
[1/23/2020 10:23:57 PM]   "EnableDependencyTracking": true
[1/23/2020 10:23:57 PM] }
[1/23/2020 10:23:57 PM] LoggerFilterOptions
[1/23/2020 10:23:57 PM] {
[1/23/2020 10:23:57 PM]   "MinLevel": "None",
[1/23/2020 10:23:57 PM]   "Rules": [
[1/23/2020 10:23:57 PM]     {
[1/23/2020 10:23:57 PM]       "ProviderName": null,
[1/23/2020 10:23:57 PM]       "CategoryName": null,
[1/23/2020 10:23:57 PM]       "LogLevel": null,
[1/23/2020 10:23:57 PM]       "Filter": "<AddFilter>b__0"
[1/23/2020 10:23:57 PM]     },
[1/23/2020 10:23:57 PM]     {
[1/23/2020 10:23:57 PM]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[1/23/2020 10:23:57 PM]       "CategoryName": null,
[1/23/2020 10:23:57 PM]       "LogLevel": "None",
[1/23/2020 10:23:57 PM]       "Filter": null
[1/23/2020 10:23:57 PM]     },
[1/23/2020 10:23:57 PM]     {
[1/23/2020 10:23:57 PM]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[1/23/2020 10:23:57 PM]       "CategoryName": null,
[1/23/2020 10:23:57 PM]       "LogLevel": null,
[1/23/2020 10:23:57 PM]       "Filter": "<AddFilter>b__0"
[1/23/2020 10:23:57 PM]     },
[1/23/2020 10:23:57 PM]     {
[1/23/2020 10:23:57 PM]       "ProviderName": "Microsoft.Azure.WebJobs.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider",
[1/23/2020 10:23:57 PM]       "CategoryName": null,
[1/23/2020 10:23:57 PM]       "LogLevel": "Trace",
[1/23/2020 10:23:57 PM]       "Filter": null
[1/23/2020 10:23:57 PM]     }
[1/23/2020 10:23:57 PM]   ]
[1/23/2020 10:23:57 PM] }
[1/23/2020 10:23:57 PM] LoggerFilterOptions
[1/23/2020 10:23:57 PM] {
[1/23/2020 10:23:57 PM]   "MinLevel": "None",
[1/23/2020 10:23:57 PM]   "Rules": [
[1/23/2020 10:23:57 PM]     {
[1/23/2020 10:23:57 PM]       "ProviderName": null,
[1/23/2020 10:23:57 PM]       "CategoryName": null,
[1/23/2020 10:23:57 PM]       "LogLevel": null,
[1/23/2020 10:23:57 PM]       "Filter": "<AddFilter>b__0"
[1/23/2020 10:23:57 PM]     },
[1/23/2020 10:23:57 PM]     {
[1/23/2020 10:23:57 PM]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[1/23/2020 10:23:57 PM]       "CategoryName": null,
[1/23/2020 10:23:57 PM]       "LogLevel": "None",
[1/23/2020 10:23:57 PM]       "Filter": null
[1/23/2020 10:23:57 PM]     },
[1/23/2020 10:23:57 PM]     {
[1/23/2020 10:23:57 PM]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[1/23/2020 10:23:57 PM]       "CategoryName": null,
[1/23/2020 10:23:57 PM]       "LogLevel": null,
[1/23/2020 10:23:57 PM]       "Filter": "<AddFilter>b__0"
[1/23/2020 10:23:57 PM]     },
[1/23/2020 10:23:57 PM]     {
[1/23/2020 10:23:57 PM]       "ProviderName": "Microsoft.Azure.WebJobs.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider",
[1/23/2020 10:23:57 PM]       "CategoryName": null,
[1/23/2020 10:23:57 PM]       "LogLevel": "Trace",
[1/23/2020 10:23:57 PM]       "Filter": null
[1/23/2020 10:23:57 PM]     }
[1/23/2020 10:23:57 PM]   ]
[1/23/2020 10:23:57 PM] }
[1/23/2020 10:23:57 PM] FunctionResultAggregatorOptions
[1/23/2020 10:23:57 PM] {
[1/23/2020 10:23:57 PM]   "BatchSize": 1000,
[1/23/2020 10:23:57 PM]   "FlushTimeout": "00:00:30",
[1/23/2020 10:23:57 PM]   "IsEnabled": true
[1/23/2020 10:23:57 PM] }
[1/23/2020 10:23:57 PM] SingletonOptions
[1/23/2020 10:23:57 PM] {
[1/23/2020 10:23:57 PM]   "LockPeriod": "00:00:15",
[1/23/2020 10:23:57 PM]   "ListenerLockPeriod": "00:00:15",
[1/23/2020 10:23:57 PM]   "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
[1/23/2020 10:23:57 PM]   "LockAcquisitionPollingInterval": "00:00:05",
[1/23/2020 10:23:57 PM]   "ListenerLockRecoveryPollingInterval": "00:01:00"
[1/23/2020 10:23:57 PM] }
[1/23/2020 10:23:57 PM] QueuesOptions
[1/23/2020 10:23:57 PM] {
[1/23/2020 10:23:57 PM]   "BatchSize": 16,
[1/23/2020 10:23:57 PM]   "NewBatchThreshold": 8,
[1/23/2020 10:23:57 PM]   "MaxPollingInterval": "00:00:02",
[1/23/2020 10:23:57 PM]   "MaxDequeueCount": 5,
[1/23/2020 10:23:58 PM]   "VisibilityTimeout": "00:00:00"
[1/23/2020 10:23:58 PM] }
[1/23/2020 10:23:58 PM] BlobsOptions
[1/23/2020 10:23:58 PM] {
[1/23/2020 10:23:58 PM]   "CentralizedPoisonQueue": false
[1/23/2020 10:23:58 PM] }
[1/23/2020 10:23:58 PM] HttpOptions
[1/23/2020 10:23:58 PM] {
[1/23/2020 10:23:58 PM]   "DynamicThrottlesEnabled": false,
[1/23/2020 10:23:58 PM]   "MaxConcurrentRequests": -1,
[1/23/2020 10:23:58 PM]   "MaxOutstandingRequests": -1,
[1/23/2020 10:23:58 PM]   "RoutePrefix": "api"
[1/23/2020 10:23:58 PM] }
[1/23/2020 10:23:58 PM] Starting JobHost
[1/23/2020 10:23:58 PM] Starting Host (HostId=desktopajf7vh8-1642830398, InstanceId=d833cc48-b5c5-4ed7-9279-77221091176a, Version=2.0.12888.0, ProcessId=16928, AppDomainId=1, InDebugMode=False, InDiagnosticMode=False, FunctionsExtensionVersion=(null))
[1/23/2020 10:23:58 PM] Loading functions metadata
[1/23/2020 10:23:58 PM] 1 functions loaded
[1/23/2020 10:23:58 PM] Generating 1 job function(s)
[1/23/2020 10:23:58 PM] Found the following functions:
[1/23/2020 10:23:58 PM] DocfxPipelineFunctions.CategoryHydrationFunction.Run
[1/23/2020 10:23:58 PM]
[1/23/2020 10:23:58 PM] Initializing function HTTP routes
[1/23/2020 10:23:58 PM] No HTTP routes mapped
[1/23/2020 10:23:58 PM]
[1/23/2020 10:23:58 PM] Host initialized (601ms)
[1/23/2020 10:24:00 PM] Executed 'CategoryHydrationFunction' (Failed, Id=386b999e-85bd-4759-85e8-fe092ea50db9)
[1/23/2020 10:24:00 PM] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' while attempting to activate 'DocfxPipelineFunctions.CategoryHydrationFunction'.
[1/23/2020 10:24:00 PM] The next 5 occurrences of the 'CategoryHydrationFunction' schedule (Cron: '0 * * * * *') will be:
[1/23/2020 10:24:00 PM] 01/23/2020 14:25:00-08:00 (01/23/2020 22:25:00Z)
[1/23/2020 10:24:00 PM] 01/23/2020 14:26:00-08:00 (01/23/2020 22:26:00Z)
[1/23/2020 10:24:00 PM] 01/23/2020 14:27:00-08:00 (01/23/2020 22:27:00Z)
[1/23/2020 10:24:00 PM] 01/23/2020 14:28:00-08:00 (01/23/2020 22:28:00Z)
[1/23/2020 10:24:00 PM] 01/23/2020 14:29:00-08:00 (01/23/2020 22:29:00Z)
[1/23/2020 10:24:00 PM]
[1/23/2020 10:24:00 PM] Host started (2155ms)
[1/23/2020 10:24:00 PM] Job host started
[1/23/2020 10:24:00 PM] Executed 'CategoryHydrationFunction' (Failed, Id=fc93494f-bf55-4ed1-9355-4a1d1e8ebab0)
[1/23/2020 10:24:00 PM] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' while attempting to activate 'DocfxPipelineFunctions.CategoryHydrationFunction'.
Hosting environment: Production
Content root path: C:\Users\sclin\Repos\PIE-AECore-EngineeringHub\docfxpipeline\DocfxPipelineFunctions\bin\Debug\netcoreapp2.1
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.

Related information

shraddhaagrawal commented 4 years ago

We were facing same issue for version 2.0.12961.0 and issue got resolved after updating default host.json

Before- { "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[1.*, 2.0.0)" } }

Working- { "version": "2.0" }

scott-lin commented 4 years ago

Thanks for the info. Unfortunately, my host file from the terminal dump above shows I'm using the same host.json content { "version": "2.0" }.

Bpflugrad commented 4 years ago

On 1/22 this began affecting one of my functions running on a Consumption Plan host (V2 functions on .Net Core 2.2). I've been unable to resolve it by upgrading/downgrading Microsoft.ApplicationInsights. Kind of at my wits' end at this point...

Bpflugrad commented 4 years ago

Ultimately I had to work around this by creating a working TelemetryClient in my extension of Microsoft.Azure.Functions.Extensions.DependencyInjection.FunctionsStartup and injecting that.

Update: Works in Azure but not using Azure Functions Core Tools (2.7.1948 Commit hash: 29a0626ded3ae99c4111f66763f27bb9fb564103) Function Runtime Version: 2.0.12888.0

scott-lin commented 4 years ago

@ankitkumarr, are you able to help here?

This is blocking release of our functions because we will not have any telemetry on them, which is a no-go. Running locally and in a Consumption plan in Azure are both not working with the issue called out.

piizei commented 4 years ago

Does not work on function runtime 3.0 either. This is really blocking us.

ankitkumarr commented 4 years ago

I apologize for the delay here, I was away for a bit. I will see if I can repro this on my end. @scott-lin, do you use Startup (FunctionsStartup) to register TelemetryConfiguration? If so, would you mind sharing a full repro with us?

Additionally, is this a new issue you are seeing in recent releases, or has this never worked?

cc: @brettsam, are you the right person to help with this (Application Insights + DI)?

scott-lin commented 4 years ago

I do not use a Startup to register TelemetryConfiguration. My original repro steps should be complete. This has never worked for me.

I'm led to believe a custom Startup isn't necessary because of these docs.microsoft.com articles:

Specifically, in the latter article, the "Warning" in the section I linked to says,

Do not register your own TelemetryConfiguration or TelemetryClient if you are using the built-in Application Insights functionality.

These docs lead me to believe creating a function with the following aspects will result in a TelemetryConfiguration instance to be dependency injected into my function's constructor:

  1. Install nuget Microsoft.ApplicationInsights
  2. Specify a constructor like, public MyFunction( TelemetryConfiguration telemetryConfiguration ) { ... }
  3. Ensure environment variable APPINSIGHTS_INSTRUMENTATIONKEY is present at runtime
brettsam commented 4 years ago

I believe there's two issues you're running into but we should be able to get you running:

  1. Add <SkipFunctionsDepsCopy>true</SkipFunctionsDepsCopy> to your project file. This will prevent a deps.json file from being deployed and redirecting the App Insights reference.
  2. Reference 2.0.11 (or earlier) of the ApplicationInsights package.

Make sure to rebuild or fully clean as the deps.json file may hang around if you don't.

The good news is that both of these restrictions will go away soon. This PR has fixed the issue (I just did some testing again to maks sure): https://github.com/Azure/azure-functions-host/pull/5551. You can see that for some other issues that are also impacted by this.

ankitkumarr commented 4 years ago

Thanks @brettsam, I was able to repro the behavior @scott-lin was experiencing. With your suggestions, I was able to fix the issue for now.

@scott-lin, please let us know if that works for you in the meanwhile. Sounds like we'll have a proper fix rolling out soon.

scott-lin commented 4 years ago

I confirmed Brett's workaround works.

brettsam commented 4 years ago

Thanks for confirming. Will close this issue as the PR above fixes it and it'll be in our next release.