Fixed: No Azure Cosmos DB Traces in OpenTelemetry Even with DisableDistributedTracing = false
I turned on tracing in the Azure Cosmos DB .NET SDK the way Microsoft's observability page describes it: DisableDistributedTracing = false on the client, and the Azure.Cosmos.Operation source in the tracer provider. The app saved and read an item without a problem, and the Aspire Dashboard showed No traces found. One line was missing: the Azure SDK's experimental switch.
What I Was Doing
I wanted a span for every Cosmos DB call an app makes, sent with OpenTelemetry to the Aspire Dashboard on my machine. To try it on its own, I wrote a small console app, TodoTrace, with the Cosmos DB SDK 3.64.0 and the OpenTelemetry OTLP exporter 1.21.0. It saves one to-do item with an upsert and reads it back. Its tracing part had the two settings Microsoft's Azure Cosmos DB SDK observability page names:
// Collect the Cosmos DB SDK's spans and send them over OTLP (http://localhost:4317 by default).
using TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
.ConfigureResource(resource => resource.AddService("TodoTrace"))
.AddSource("Azure.Cosmos.Operation")
.AddOtlpExporter()
.Build();
// ...
using CosmosClient client = new(endpoint, new AzureCliCredential(), new CosmosClientOptions
{
UseSystemTextJsonSerializerWithOptions = JsonSerializerOptions.Web,
// A span for every read and write. The SDK's stable releases leave this off.
CosmosClientTelemetryOptions = new CosmosClientTelemetryOptions
{
DisableDistributedTracing = false,
},
});
The page says of the first: For non-preview versions, it's off by default. You can enable tracing by setting DisableDistributedTracing = false in CosmosClientOptions.CosmosClientTelemetryOptions. And of the second: To use OpenTelemetry with the Azure Cosmos DB SDKs, add the Azure.Cosmos.Operation source to your trace provider.
I set the account, the database and the container in three environment variables and ran the app:
- macOS / Linux
- Windows (PowerShell)
export COSMOS_ENDPOINT="https://<your-account>.documents.azure.com:443/"
export COSMOS_DATABASE="<your-database>"
export COSMOS_CONTAINER="<your-container>"
dotnet run
$env:COSMOS_ENDPOINT = "https://<your-account>.documents.azure.com:443/"
$env:COSMOS_DATABASE = "<your-database>"
$env:COSMOS_CONTAINER = "<your-container>"
dotnet run
Read back: Buy milk (done: False)
What I Expected
Two spans in the dashboard, one for the upsert and one for the read.
What I Got
Nothing. The app's output shows that both calls worked, but the dashboard's Traces page was empty, and no app was listed under its telemetry sources:

The app printed no error. The SDK simply created no spans for the tracer provider to collect.
What I Checked
The two settings. Both were in the code, exactly as the observability page names them: DisableDistributedTracing = false in CosmosClientTelemetryOptions, and AddSource("Azure.Cosmos.Operation").
The rest of the observability page. Its prose names those two settings and nothing else. But its OpenTelemetry sample, under Configure OpenTelemetry, has one more line, right before it builds the trace provider:
// Configure OpenTelemetry trace provider
AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);
The page doesn't explain it.
What the switch is. It's the Azure SDK's switch for experimental tracing. The Azure SDK's diagnostics guide explains its history: Prior to November 2023, OpenTelemetry support was experimental for all Azure client libraries, and Most of Azure client libraries released in or after November 2023 have OpenTelemetry support enabled by default. So most Azure libraries trace without the switch today. Most, not all: the guide names the messaging libraries as still experimental, and for any library that still needs it, it says Certain tracing features remain experimental and still need to be enabled explicitly.
Why It Happens
The Cosmos DB SDK, 3.64.0 here, creates its spans only when the Azure SDK's experimental tracing switch is on. DisableDistributedTracing = false lets the client trace, and AddSource subscribes to its source, but without the switch the SDK never starts a span, so there's nothing to subscribe to. Microsoft's own sample turns the switch on, which is why it works.
The Fix
Turn the switch on at the start of the app, before the tracer provider is built and before the first Cosmos DB call:
// The Cosmos DB SDK creates its spans only with this switch on.
AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);

AppContext.SetSwitch sets a named switch for the whole app, and Azure.Experimental.EnableActivitySource is the name the diagnostics guide gives for turning on the Azure SDK's experimental tracing.
Then run the app again, in the same terminal so the three variables are still set:
dotnet run
Read back: Buy milk (done: False)
After the Fix
The output was the same, and the dashboard now had two traces: TodoTrace: upsert_item howto-otel, 6.84 seconds, and TodoTrace: read_item howto-otel, 1.41 seconds. The upsert's span holds everything about the call:

The azure.cosmosdb.* names are cut off in the list. In full, they are azure.cosmosdb.connection.mode (direct), azure.cosmosdb.contacted_regions (East US 2), azure.cosmosdb.request.request_charge (10 request units) and azure.cosmosdb.response.sub_status_code (0). The span's source, under Context, is Azure.Cosmos.Operation, the one the tracer provider listens to.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Setting only DisableDistributedTracing = false and AddSource("Azure.Cosmos.Operation") | No Cosmos DB spans at all | Turn on the switch as well |
| Expecting the switch to do it alone | Without DisableDistributedTracing = false, a stable SDK release doesn't trace | Keep all three: the switch, the client option and the source |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| No traces found, and the app works | The switch is missing | Add AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true) |
| Other spans arrive, Cosmos DB's don't | The same, or the source isn't subscribed | Check the switch and AddSource("Azure.Cosmos.Operation") |
| Nothing arrives from any source | The exporter can't reach the collector | Check the OTLP endpoint, http://localhost:4317 by default |
FAQ
Do other Azure SDK libraries need the switch?
Most don't, according to the diagnostics guide: libraries released in or after November 2023 trace without it, and the messaging libraries, Service Bus and Event Hubs, still need it. I checked one: Azure.Identity 1.21.0 created its spans without the switch. The Cosmos DB SDK 3.64.0 didn't.
Can I turn it on without changing the code?
The diagnostics guide lists two other ways: the AZURE_EXPERIMENTAL_ENABLE_ACTIVITY_SOURCE environment variable set to true, or a RuntimeHostConfigurationOption item with the switch's name in the project file. I used the switch in code.
What does experimental mean for these spans?
The guide: The shape of experimental Activities may change in the future without notice, including attributes attached to telemetry spans. The attribute names on the span above, such as db.collection.name, are already different from the ones in the table on the observability page, so check what your SDK version sends before you build queries or alerts on them.
Conclusion
The Azure Cosmos DB .NET SDK 3.64.0 traces only when three things are in place: DisableDistributedTracing = false on the client, AddSource("Azure.Cosmos.Operation") in the tracer provider, and the Azure SDK's experimental switch, AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true). The observability page names the first two in its text and has the third only in its sample. With all three, every upsert and read became a span with its duration, its request charge and its result.
Additional Resources
- Azure Cosmos DB SDK observability — the client option, the source name, and the sample with the switch.
- Azure SDK diagnostics — which libraries trace by default, and the three ways to turn on experimental tracing.
- AppContext.SetSwitch — setting a runtime switch by name.
