Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3abc109
Add Entra auth to Durable tracing
YunchuWang Aug 6, 2026
c4fd90a
Merge branch 'dev' into yunchuwang-fix-3497-entra-tracing
YunchuWang Aug 6, 2026
5405752
Fix Entra credential reuse in Functions host
YunchuWang Aug 6, 2026
5fa4182
Harden host credential reuse validation
YunchuWang Aug 6, 2026
8b1b987
Test Durable credential selection branches
YunchuWang Aug 6, 2026
cddec93
Warn when host telemetry credential cannot be read
YunchuWang Aug 10, 2026
3d58aa6
Address review: upgrade logging package and cover the DI credential path
YunchuWang Aug 13, 2026
b170f32
Align remaining System.Drawing.Common pins with 6.0.0
YunchuWang Aug 13, 2026
47c0f5f
Raise WorkerExtensions System.Drawing.Common pin to 6.0.0
YunchuWang Aug 13, 2026
7a43548
Reuse the host telemetry channel instead of reading its credential
YunchuWang Aug 13, 2026
e5d5bc3
Fix telemetry shutdown flush lifecycle
YunchuWang Aug 13, 2026
77ff790
Update src/WebJobs.Extensions.DurableTask/Correlation/TelemetryActiva…
YunchuWang Aug 17, 2026
7de20d9
Merge branch 'dev' into yunchuwang-fix-3497-entra-tracing
YunchuWang Aug 17, 2026
fffc980
Clarify invalid authentication warning
YunchuWang Aug 17, 2026
bcbad7a
Merge branch 'dev' into yunchuwang-fix-3497-entra-tracing
YunchuWang Aug 18, 2026
c046003
Consolidate tracing warning logging
YunchuWang Aug 18, 2026
579419d
Merge branch 'dev' into yunchuwang-fix-3497-entra-tracing
YunchuWang Aug 18, 2026
30718c0
Merge branch 'dev' into yunchuwang-fix-3497-entra-tracing
YunchuWang Aug 19, 2026
1a11607
Merge branch 'dev' into yunchuwang-fix-3497-entra-tracing
YunchuWang Aug 20, 2026
2c27ccb
Merge branch 'dev' into yunchuwang-fix-3497-entra-tracing
YunchuWang Aug 21, 2026
516cff3
Merge branch 'dev' into yunchuwang-fix-3497-entra-tracing
YunchuWang Aug 21, 2026
fa8fc03
Align WorkerExtensions pin comments with the 6.0.0 override
YunchuWang Aug 25, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageVersion Include="Microsoft.Azure.WebJobs" Version="3.0.45" />
<PackageVersion Include="Microsoft.Azure.WebJobs.Extensions.DurableTask.Analyzers" Version="0.5.0" />
<PackageVersion Include="Microsoft.Azure.WebJobs.Extensions.Rpc" Version="3.0.39" />
<PackageVersion Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.45" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.2" />
<PackageVersion Include="Microsoft.Bcl.Memory" Version="10.0.5" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
Expand Down
2 changes: 1 addition & 1 deletion samples/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PackageVersion Update="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="4.0.1" />
<PackageVersion Update="Microsoft.Bcl.AsyncInterfaces" Version="10.0.2" />
<PackageVersion Update="Microsoft.Extensions.Logging.Debug" Version="10.0.0" />
<PackageVersion Update="System.Drawing.Common" Version="4.7.3" />
<PackageVersion Update="System.Drawing.Common" Version="6.0.0" />
<PackageVersion Update="System.Text.Json" Version="10.0.2" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ You will also need to specify an Application Insights resource in the environmen

Next, you'll need to copy the connection string or instrumentation key for that resource to add in the environment variables. We recommend adding the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable, but you can also add `APPINSIGHTS_INSTRUMENTATIONKEY`. If you are running your app locally, then add this environment variable in `local.settings.json`. If you are running the app in Azure, then add it under `Configuration` as an application setting.

If the Application Insights resource has local authentication disabled, also configure `APPLICATIONINSIGHTS_AUTHENTICATION_STRING`. Use `Authorization=AAD` for the function app's system-assigned managed identity, or `Authorization=AAD;ClientId=<USER_ASSIGNED_CLIENT_ID>` for a user-assigned managed identity. The selected identity needs the `Monitoring Metrics Publisher` role on the Application Insights resource. Microsoft Entra authentication for Application Insights isn't supported by the Functions host during local development.

#### Run the sample
The sample shows how Distributed Tracing V2 improves the observability of some flagship Durable Functions patterns.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;

namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Correlation
{
/// <summary>
/// A telemetry channel that forwards telemetry to a channel owned by the Functions host.
/// </summary>
/// <remarks>
/// <para>
/// Durable distributed tracing keeps its own <see cref="TelemetryConfiguration"/> so that its
/// telemetry initializers, processors and modules stay isolated from the host pipeline. That
/// private configuration has no Microsoft Entra credential of its own, so when the Application
/// Insights component has local authentication disabled its telemetry is rejected at ingestion.
/// </para>
/// <para>
/// The host's channel already carries the credential and the Entra ingestion endpoint, so
/// forwarding to it reuses the host's authentication without Durable ever creating, reading or
/// referencing an <c>Azure.Core.TokenCredential</c>. That matters because the Application
/// Insights SDK resolves that type in the host load context, and a credential created in the
/// function app load context is rejected.
/// </para>
/// <para>
/// This channel deliberately does not own the wrapped channel. It never disposes it, and it
/// keeps <see cref="EndpointAddress"/> and <see cref="DeveloperMode"/> local so that
/// configuring the Durable configuration cannot mutate host state.
/// </para>
/// </remarks>
internal sealed class HostForwardingTelemetryChannel : ITelemetryChannel, IAsyncFlushable
{
private readonly ITelemetryChannel hostChannel;

/// <summary>
/// Initializes a new instance of the <see cref="HostForwardingTelemetryChannel"/> class.
/// </summary>
/// <param name="hostChannel">The host-owned channel to forward telemetry to.</param>
public HostForwardingTelemetryChannel(ITelemetryChannel hostChannel)
{
this.hostChannel = hostChannel ?? throw new ArgumentNullException(nameof(hostChannel));
}

/// <summary>
/// Gets or sets a value indicating whether this channel is in developer mode.
/// </summary>
/// <remarks>
/// Kept local. Forwarding this would let the Durable configuration change how the host
/// batches its own telemetry.
/// </remarks>
public bool? DeveloperMode { get; set; }

/// <summary>
/// Gets or sets the channel's endpoint.
/// </summary>
/// <remarks>
/// Kept local. The host channel already points at the Entra ingestion endpoint, and the
/// Durable configuration has no credential of its own, so propagating this value would
/// downgrade the host to the non-Entra endpoint.
/// </remarks>
public string EndpointAddress { get; set; }

/// <summary>
/// Gets the host-owned channel that telemetry is forwarded to.
/// </summary>
internal ITelemetryChannel HostChannel => this.hostChannel;

/// <summary>
/// Forwards a telemetry item to the host channel, which transmits it using the host's
/// credential and endpoint.
/// </summary>
/// <param name="item">The telemetry item to send.</param>
public void Send(ITelemetry item) => this.hostChannel.Send(item);

/// <summary>
/// Flushes the host channel.
/// </summary>
public void Flush() => this.hostChannel.Flush();

/// <summary>
/// Asynchronously flushes the host channel when supported.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The host channel's asynchronous flush result, or false after a synchronous flush.</returns>
public Task<bool> FlushAsync(CancellationToken cancellationToken)
{
if (this.hostChannel is IAsyncFlushable asyncFlushable)
{
return asyncFlushable.FlushAsync(cancellationToken);
}

this.hostChannel.Flush();
return Task.FromResult(false);
}

/// <summary>
/// Does nothing. The host owns the wrapped channel's lifetime.
/// </summary>
public void Dispose()
{
// Intentionally empty: disposing the host channel here would stop host telemetry.
}
}
}
132 changes: 126 additions & 6 deletions src/WebJobs.Extensions.DurableTask/Correlation/TelemetryActivator.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Security.Authentication;
using System.Threading.Tasks;
using Azure.Identity;
using DurableTask.ApplicationInsights;
using DurableTask.Core;
using DurableTask.Core.Settings;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs.Logging.ApplicationInsights;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ApplicationInsightsTokenCredentialOptions = Microsoft.Azure.WebJobs.Logging.ApplicationInsights.TokenCredentialOptions;

namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Correlation
{
Expand All @@ -21,6 +25,7 @@ public class TelemetryActivator : ITelemetryActivator, IAsyncDisposable, IDispos
{
private readonly DurableTaskOptions options;
private readonly INameResolver nameResolver;
private readonly TelemetryConfiguration hostTelemetryConfiguration;
private EndToEndTraceHelper endToEndTraceHelper;
private TelemetryClient telemetryClient;

Expand All @@ -30,9 +35,24 @@ public class TelemetryActivator : ITelemetryActivator, IAsyncDisposable, IDispos
/// <param name="options">DurableTask options.</param>
/// <param name="nameResolver">Name resolver used for environment variables.</param>
public TelemetryActivator(IOptions<DurableTaskOptions> options, INameResolver nameResolver)
: this(options, nameResolver, hostTelemetryConfiguration: null)
{
}

/// <summary>
/// Constructor for initializing Distributed Tracing with the host telemetry configuration.
/// </summary>
/// <param name="options">DurableTask options.</param>
/// <param name="nameResolver">Name resolver used for environment variables.</param>
/// <param name="hostTelemetryConfiguration">Application Insights configuration owned by the host.</param>
public TelemetryActivator(
IOptions<DurableTaskOptions> options,
INameResolver nameResolver,
TelemetryConfiguration hostTelemetryConfiguration)
{
this.options = options.Value;
this.nameResolver = nameResolver;
this.hostTelemetryConfiguration = hostTelemetryConfiguration;
}

/// <summary>
Expand All @@ -45,20 +65,24 @@ public TelemetryActivator(IOptions<DurableTaskOptions> options, INameResolver na

internal IAsyncDisposable WebJobsTelemetryModule { get; set; }

/// <summary>
/// Gets the private configuration Durable builds for its own telemetry. Exposed so tests can
/// assert which credential was applied without reaching into Application Insights internals.
/// </summary>
internal TelemetryConfiguration TelemetryConfiguration { get; private set; }

/// <inheritdoc/>
public ValueTask DisposeAsync()
public async ValueTask DisposeAsync()
{
if (this.TelemetryModule != null)
{
this.TelemetryModule.DisposeAsync();
await this.TelemetryModule.DisposeAsync();
}

if (this.WebJobsTelemetryModule != null)
{
this.WebJobsTelemetryModule.DisposeAsync();
await this.WebJobsTelemetryModule.DisposeAsync();
}

return default;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -90,6 +114,7 @@ public void Initialize(ILogger logger)
}

TelemetryConfiguration telemetryConfiguration = this.SetupTelemetryConfiguration();
this.TelemetryConfiguration = telemetryConfiguration;

if (this.options.Tracing.Version == Options.DurableDistributedTracingVersion.V2)
{
Expand Down Expand Up @@ -190,6 +215,7 @@ private TelemetryConfiguration SetupTelemetryConfiguration()

string resolvedInstrumentationKey = this.nameResolver.Resolve("APPINSIGHTS_INSTRUMENTATIONKEY");
string resolvedConnectionString = this.nameResolver.Resolve("APPLICATIONINSIGHTS_CONNECTION_STRING");
string resolvedAuthenticationString = this.nameResolver.Resolve("APPLICATIONINSIGHTS_AUTHENTICATION_STRING");

bool instrumentationKeyProvided = !string.IsNullOrEmpty(resolvedInstrumentationKey);
bool connectionStringProvided = !string.IsNullOrEmpty(resolvedConnectionString);
Expand Down Expand Up @@ -238,7 +264,101 @@ private TelemetryConfiguration SetupTelemetryConfiguration()
config.ConnectionString = resolvedConnectionString;
}

if (!string.IsNullOrWhiteSpace(resolvedAuthenticationString))
{
try
{
ApplicationInsightsTokenCredentialOptions tokenCredentialOptions =
ApplicationInsightsTokenCredentialOptions.ParseAuthenticationString(resolvedAuthenticationString);
bool userAssignedIdentity = tokenCredentialOptions.ClientId != null;
bool reusedHostChannel = ApplyEntraAuthentication(
config,
this.hostTelemetryConfiguration,
tokenCredentialOptions,
preserveExistingChannel: this.OnSend != null);
if (this.hostTelemetryConfiguration != null && !reusedHostChannel && this.OnSend == null)
{
this.LogTracingWarning(
"The Application Insights telemetry channel owned by the Functions host could not be read, so Durable distributed tracing created its own managed identity credential. If Durable spans are missing, the host and the function app are likely loading different Azure.Core versions.");
}

this.endToEndTraceHelper.ExtensionInformationalEvent(
hubName: this.options.HubName,
functionName: string.Empty,
instanceId: string.Empty,
message:
"Microsoft Entra authentication enabled for Durable distributed tracing using the "
+ $"{(userAssignedIdentity ? "user-assigned" : "system-assigned")} managed identity.",
writeToUserLogs: true);
}
catch (AuthenticationException)
{
this.LogTracingWarning(
"APPLICATIONINSIGHTS_AUTHENTICATION_STRING is invalid and will not be used for Durable Functions distributed tracing.");
}
catch (FormatException)
{
this.LogTracingWarning(
"APPLICATIONINSIGHTS_AUTHENTICATION_STRING is invalid and will not be used for Durable Functions distributed tracing.");
}
catch (ArgumentException)
{
this.LogTracingWarning(
"APPLICATIONINSIGHTS_AUTHENTICATION_STRING could not be applied and will not be used for Durable Functions distributed tracing.");
}
}

return config;
}

/// <summary>
/// Enables Microsoft Entra authenticated ingestion for the private Durable telemetry
/// configuration.
/// </summary>
/// <remarks>
/// When the Functions host exposes its telemetry configuration, Durable forwards telemetry
/// to the host's channel, which already holds the host's credential and Entra ingestion
/// endpoint. Durable therefore never creates an <c>Azure.Core.TokenCredential</c>, which is
/// important because the Application Insights SDK resolves that type in the host load
/// context and rejects a credential created in the function app load context.
/// Outside the Functions host there is no channel to reuse, so Durable falls back to
/// creating its own managed identity credential.
/// </remarks>
/// <param name="durableConfiguration">The private Durable telemetry configuration.</param>
/// <param name="hostConfiguration">The host telemetry configuration, if available.</param>
/// <param name="tokenCredentialOptions">The parsed authentication string.</param>
/// <param name="preserveExistingChannel">
/// True when the configuration already has a channel that must not be replaced, such as the
/// test channel installed by <see cref="OnSend"/>.
/// </param>
/// <returns>True when the host channel was reused.</returns>
internal static bool ApplyEntraAuthentication(
TelemetryConfiguration durableConfiguration,
TelemetryConfiguration hostConfiguration,
ApplicationInsightsTokenCredentialOptions tokenCredentialOptions,
bool preserveExistingChannel = false)
{
ITelemetryChannel hostChannel = hostConfiguration?.TelemetryChannel;
if (hostChannel != null && !preserveExistingChannel)
{
durableConfiguration.TelemetryChannel = new HostForwardingTelemetryChannel(hostChannel);
return true;
}

ManagedIdentityId managedIdentityId = tokenCredentialOptions.ClientId != null
? ManagedIdentityId.FromUserAssignedClientId(tokenCredentialOptions.ClientId)
: ManagedIdentityId.SystemAssigned;
durableConfiguration.SetAzureTokenCredential(new ManagedIdentityCredential(managedIdentityId));
return false;
}

private void LogTracingWarning(string message)
{
this.endToEndTraceHelper.ExtensionWarningEvent(
hubName: this.options.HubName,
functionName: string.Empty,
instanceId: string.Empty,
message: message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<ItemGroup>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Azure.WebJobs" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Rpc" />
<PackageReference Include="Microsoft.Azure.DurableTask.Core" />
<PackageReference Include="Microsoft.Azure.DurableTask.AzureStorage" />
Expand Down
3 changes: 1 addition & 2 deletions test/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="FluentAssertions" Version="4.19.4" />
<PackageVersion Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.41" />
<PackageVersion Include="Microsoft.Build" Version="17.8.43" />
<PackageVersion Include="Microsoft.Build.Framework" Version="17.8.43" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.4.1" />
Expand All @@ -33,7 +32,7 @@
<PackageVersion Update="Azure.Identity" Version="1.17.1" />
<PackageVersion Update="Microsoft.Bcl.AsyncInterfaces" Version="10.0.2" />
<PackageVersion Update="Microsoft.Extensions.Azure" Version="1.13.1" />
<PackageVersion Update="System.Drawing.Common" Version="4.7.3" />
<PackageVersion Update="System.Drawing.Common" Version="6.0.0" />
<PackageVersion Update="System.Formats.Asn1" Version="8.0.1" />
<PackageVersion Update="System.Text.Json" Version="10.0.2" />
</ItemGroup>
Expand Down
Loading