Azure / Azure/azure-functions-host

Assemblies with types added to DI container are not marked as runtime assemblies, making them potentially inaccessible

Open
#4,952 14 comments 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

When `APPINSIGHTS_INSTRUMENTATIONKEY` is set, the WebJobs SDK adds AppInsights types to the dependency injection container. However, `Microsoft.ApplicationInsights` is not listed in the function host's `runtimeassemblies.json`, so `FunctionAssemblyLoadContext` does not ensure the runtime's version is always the one that's used.

The right thing happens in the default case, but in the event that the function app is including a `function.deps.json` in its bundle (which my app is for other reasons :smile:), then `function.deps.json` takes priority and the runtime's `Microsoft.ApplicationInsights` assembly is ignored. This effectively blocks AppInsights types from being retrieved via DI (making https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring#log-custom-telemetry-in-c-functions a hard doc to follow :innocent:).

I wrote a function to look at all ServiceTypes added to the DI container that are from a non-runtime assembly, and found the following list of types:
- Assembly: Microsoft.ApplicationInsights
- Type: Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer
- Type: Microsoft.ApplicationInsights.Extensibility.ITelemetryModule
- Type: Microsoft.ApplicationInsights.Extensibility.IApplicationIdProvider
- Type: Microsoft.ApplicationInsights.Channel.ITelemetryChannel
- Type: Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
- Type: Microsoft.ApplicationInsights.TelemetryClient
- Assembly: Microsoft.AI.PerfCounterCollector
- Type: Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule
- Assembly: Microsoft.Azure.WebJobs.Host.Storage
- Type: Microsoft.Azure.WebJobs.DistributedLockManagerContainerProvider

I think that the three assemblies above need to be added to `runtimeassemblies.json`, in order to unblock scenarios

#### Investigative information

Template fields didn't apply, but I did put together a minimal function app repro here:
https://github.com/AtOMiCNebula/FunctionHostAssemblyWoes (make sure you add an `APPINSIGHTS_INSTRUMENTATIONKEY` value to your `local.settings.json`/etc!)

The `BrokenConsumer` function shows the issue with `TelemetryConfiguration`.

The `DIChecker` function enumerates all service types added to the `IServiceCollection` and lists out types that are in assemblies not listed in `runtimeassemblies.json`.

#### Repro steps

Provide the steps required to reproduce the problem:

1. Create new HTTP triggered function in empty project.
2. Add `APPINSIGHTS_INSTRUMENTATIONKEY` to relevant places.
3. Follow https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring#log-custom-telemetry-in-c-functions to pull a `TelemetryConfiguration` from DI.
4. Update function project to include a `function.deps.json`.

#### Expected behavior

Provide a description of the expected behavior.

Function should run successfully.

#### Actual behavior

Provide a description of the actual behavior observed.

Through step 2, the function app does not have trouble acquiring a `TelemetryConfiguration` from the DI container. After step 3, function launch fails.

#### Known workarounds

* Remove usage of `function.deps.json`. This causes other problems though (my app relies on this file being present to successfully use `System.ServiceModel.*` packages), and so I can't leverage it.
* Craft your own `TelemetryConfiguration`. This isn't ideal either, since I have to go build the configuration myself which can drift from whatever the runtime sets up for everything else.

Contributor guide

Open the contributing guide

Research direction

Start by inspecting runtimeassemblies.json and the FunctionAssemblyLoadContext behavior described in the issue. Use the linked FunctionHostAssemblyWoes reproduction, especially the BrokenConsumer and DIChecker functions, with APPINSIGHTS_INSTRUMENTATIONKEY and function.deps.json enabled. Done means the function can retrieve TelemetryConfiguration from DI and the identified assemblies are treated as runtime assemblies.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.