Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="3.1.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.*" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.51.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.*" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.*" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.*" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.52.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask.AzureManaged" Version="1.12.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.1.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.1.0" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.10.0" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.14.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.OpenTelemetry" Version="1.2.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.20.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class Functions
{
[Function("StartOrchestration")]
public static async Task<HttpResponseData> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req,
[DurableClient] DurableTaskClient client,
FunctionContext executionContext)
{
Expand Down
29 changes: 25 additions & 4 deletions samples/durable-functions/dotnet/DistributedTracing/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
using Microsoft.Azure.Functions.Worker;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.Azure.Functions.Worker.OpenTelemetry;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry;
using OpenTelemetry.Trace;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
.ConfigureServices((context, services) =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
var connectionString = context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
if (!string.IsNullOrWhiteSpace(connectionString))
{
services.AddAzureClientsCore();
services.AddOptions<AzureMonitorExporterOptions>()
.Configure<AzureComponentFactory>((options, factory) =>
{
options.ConnectionString = connectionString;
var credentialSettings = context.Configuration.GetSection("AzureMonitorExporter");
if (!string.IsNullOrWhiteSpace(context.Configuration["AzureMonitorExporter:credential"]))
{
options.Credential = factory.CreateTokenCredential(credentialSettings);
}
});
services.AddOpenTelemetry()
.UseFunctionsWorkerDefaults()
.WithTracing(tracing => tracing.AddSource("Microsoft.DurableTask"))
.UseAzureMonitorExporter();
}
})
.Build();

Expand Down
27 changes: 26 additions & 1 deletion samples/durable-functions/dotnet/DistributedTracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ This sample demonstrates how to enable distributed tracing in Durable Functions

## Prerequisites

1. [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) or later
1. [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.0) or later
2. [Docker](https://www.docker.com/products/docker-desktop/)
3. [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local) v4+

## Quick Run (Local with Jaeger)

1. Start the infrastructure:

```bash
docker compose up -d
```

2. Run the function app:

```bash
func start
```

3. Trigger an orchestration:

```bash
curl -X POST http://localhost:7071/api/StartOrchestration
```
Expand All @@ -33,8 +36,28 @@ This sample demonstrates how to enable distributed tracing in Durable Functions
- **Jaeger UI:** http://localhost:16686
- **DTS Dashboard:** http://localhost:8082

The compose file starts Jaeger but does not configure the Functions host or worker to export telemetry to it. An OTLP exporter and endpoint configuration are also needed; starting Jaeger alone does not send it traces.

## Key Configuration

### HTTP authentication

`StartOrchestration` requires a function or host key in Azure. Core Tools skips key enforcement locally by default; use `func start --enableAuth` to exercise it locally.

For a deployed app, retrieve a non-admin host key into a shell variable and send it in a header:

```bash
FUNCTION_KEY=$(az functionapp keys list \
--resource-group "$AZURE_RESOURCE_GROUP" \
--name "$AZURE_FUNCTION_NAME" --query functionKeys.default --output tsv)
curl -X POST -H "x-functions-key: ${FUNCTION_KEY}" \
"https://${AZURE_FUNCTION_NAME}.azurewebsites.net/api/StartOrchestration"
```

Do not use the master key or expose keys in source control, logs, or browser code. Keys do not provide per-user or per-instance authorization; managed identity for backend services is unchanged. The returned Durable management URLs contain a system key: keep those URLs secret and poll them as returned.

### Durable tracing

Distributed tracing is enabled in `host.json`:

```json
Expand All @@ -54,6 +77,8 @@ Distributed tracing is enabled in `host.json`:

When deployed to Azure with Application Insights configured:

The Functions host uses OpenTelemetry and the Durable Task distributed tracing configuration above. When `APPLICATIONINSIGHTS_CONNECTION_STRING` is set, the worker exports telemetry to Azure Monitor and captures the `Microsoft.DurableTask` activity source to connect the starter to the orchestration trace. For user-assigned managed identity authentication, set `AzureMonitorExporter__credential` to `managedidentity` and `AzureMonitorExporter__clientId` to the identity's client ID, and grant that identity the Monitoring Metrics Publisher role on Application Insights. If `AzureMonitorExporter__credential` is not set, the exporter uses connection-string authentication.

1. Navigate to your Application Insights resource
2. Go to **Transaction Search**
3. Filter for events with Durable Functions prefixes (`orchestration:`, `activity:`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{
"version": "2.0",
"telemetryMode": "OpenTelemetry",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"DurableTask.Core": "Warning"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -9,13 +9,15 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.52.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.19.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.1.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.1.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="3.1.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.51.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask.AzureManaged" Version="1.10.2" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.10.0" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.14.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.OpenTelemetry" Version="1.2.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.20.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask.AzureManaged" Version="1.12.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class LargePayloadOrchestration

[Function("StartLargePayload")]
public static async Task<HttpResponseData> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[DurableClient] DurableTaskClient client,
FunctionContext executionContext)
{
Expand All @@ -41,8 +41,10 @@ public static async Task<LargePayloadSummary> LargePayloadRoundTrip(
LargePayloadRequest request = context.GetInput<LargePayloadRequest>()
?? throw new InvalidOperationException("The orchestration input payload was not provided.");

ValidatePayload(request.Payload, request.RequestedPayloadBytes);
string echoedPayload = await context.CallActivityAsync<string>(nameof(EchoLargePayload), request.Payload)
?? throw new InvalidOperationException("The activity did not return a payload.");
ValidatePayload(echoedPayload, request.RequestedPayloadBytes);
int orchestrationInputBytes = GetUtf8ByteCount(request.Payload);
int activityOutputBytes = GetUtf8ByteCount(echoedPayload);

Expand Down Expand Up @@ -76,7 +78,7 @@ public static string EchoLargePayload(

[Function("Hello")]
public static async Task<HttpResponseData> Hello(
[HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
[HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req)
{
HttpResponseData response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync("LargePayload sample is running. POST /api/StartLargePayload to start a >1 MB orchestration.");
Expand All @@ -99,6 +101,25 @@ private static string CreatePayload(int payloadSizeBytes)
});
}

private static void ValidatePayload(string payload, int expectedSizeBytes)
{
if (payload.Length != expectedSizeBytes)
{
throw new InvalidOperationException("The payload size did not match the requested size.");
}

const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
uint state = 0x00C0FFEEu;
for (int i = 0; i < payload.Length; i++)
{
state = (state * 1664525) + 1013904223;
if (payload[i] != Alphabet[(int)(state >> 26)])
{
throw new InvalidOperationException($"Invalid payload content at offset {i}.");
}
}
}

private static int GetPositiveIntSetting(string key, int defaultValue)
{
string? rawValue = Environment.GetEnvironmentVariable(key);
Expand Down
26 changes: 22 additions & 4 deletions samples/durable-functions/dotnet/LargePayload/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
using Microsoft.Azure.Functions.Worker;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.Azure.Functions.Worker.OpenTelemetry;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
.ConfigureServices((context, services) =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
string? connectionString = context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
if (!string.IsNullOrWhiteSpace(connectionString))
{
services.AddAzureClientsCore();
services.AddOptions<AzureMonitorExporterOptions>()
.Configure<AzureComponentFactory>((options, factory) =>
{
options.ConnectionString = connectionString;
var credentialSettings = context.Configuration.GetSection("AzureMonitorExporter");
if (!string.IsNullOrWhiteSpace(context.Configuration["AzureMonitorExporter:credential"]))
{
options.Credential = factory.CreateTokenCredential(credentialSettings);
}
});
services.AddOpenTelemetry()
.UseFunctionsWorkerDefaults()
.UseAzureMonitorExporter();
}
})
.Build();

Expand Down
Loading
Loading