skip to content

In an Aspire ServiceDefaults project, when does ConfigureOpenTelemetry actually export telemetry, and why might a service deployed without the AppHost export nothing?

level: seniorimportance: should knowfreq 33%

answer

  1. collection is not export
  2. OTEL_EXPORTER_OTLP_ENDPOINT gates UseOtlpExporter
  3. the AppHost injects the OTEL_* variables
  4. always_on comes from the AppHost
  5. AddSource(builder.Environment.ApplicationName)

basics

~20 s

ConfigureOpenTelemetry always sets up logs, metrics and traces, but it registers the OTLP exporter only when OTEL_EXPORTER_OTLP_ENDPOINT has a value. The AppHost injects that variable; a deployment that does not set it collects telemetry and sends it nowhere.

solid answer

~30 s

`ConfigureOpenTelemetry` adds OpenTelemetry logging with formatted messages and scopes, ASP.NET Core, HttpClient and runtime metrics, and tracing for ASP.NET Core, HttpClient and an `ActivitySource` named `builder.Environment.ApplicationName`. Its private `AddOpenTelemetryExporters` then calls `UseOtlpExporter()` only if `builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]` is not empty. Under `aspire run` the AppHost injects that endpoint, the protocol, an API-key header, `OTEL_SERVICE_NAME`, and in Development `OTEL_TRACES_SAMPLER=always_on` plus 1-second export intervals. Deployed elsewhere without those variables, the service silently exports nothing. Two more traps: the template sets no sampler itself, despite the docs page, and spans from an `ActivitySource` with any other name are dropped until you add it with `AddSource`.

code

csharp · 13 lines
csharp
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder)
    where TBuilder : IHostApplicationBuilder
{
    var useOtlpExporter = !string.IsNullOrWhiteSpace(
        builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

    if (useOtlpExporter)
    {
        builder.Services.AddOpenTelemetry().UseOtlpExporter();
    }

    return builder;
}

go deeper

for a junior

Recall that ServiceDefaults turns on logs, metrics and traces, and that the dashboard receives them because the AppHost sets an OTLP endpoint.

for a middle

Explain the OTEL_EXPORTER_OTLP_ENDPOINT gate, which variables the AppHost injects, and why Development runs sample everything.

for a senior

Show you would catch a silently non-exporting deployment, set endpoint, protocol and service name per environment, and register custom sources and meters.

for a principal

Decide sampling and export policy across services and environments, and where the shared ServiceDefaults project should stop and a collector should start.

## Collection and export are separate steps In the Aspire 13.6 template, `ConfigureOpenTelemetry<TBuilder>()` does two different jobs. It **sets up collection** unconditionally: - `builder.Logging.AddOpenTelemetry(...)` with `IncludeFormattedMessage` and `IncludeScopes` set to `true`; - metrics from `AddAspNetCoreInstrumentation()`, `AddHttpClientInstrumentation()` and `AddRuntimeInstrumentation()`; - tracing from `AddSource(builder.Environment.ApplicationName)`, `AddAspNetCoreInstrumentation(...)` with a filter that drops `/health` and `/alive` requests, and `AddHttpClientInstrumentation()`. Then it **decides on export** in a private method, `AddOpenTelemetryExporters`: 1. Read `OTEL_EXPORTER_OTLP_ENDPOINT` from configuration. 2. If it is not null or whitespace, call `builder.Services.AddOpenTelemetry().UseOtlpExporter()`, which sends logs, metrics and traces over OTLP. 3. Otherwise register no exporter at all. A commented block shows where a vendor exporter would go. Nothing else exports. ## What the AppHost injects Projects added with `AddProject` get the OTLP settings automatically. When the AppHost starts the quotes API it sets, among others: | Variable | Value under `aspire run` | |---|---| | `OTEL_EXPORTER_OTLP_ENDPOINT` | the dashboard's OTLP endpoint | | `OTEL_EXPORTER_OTLP_PROTOCOL` | the protocol that endpoint speaks | | `OTEL_EXPORTER_OTLP_HEADERS` | `x-otlp-api-key=...` when the dashboard requires a key | | `OTEL_SERVICE_NAME`, `OTEL_RESOURCE_ATTRIBUTES` | the resource name and instance id | | `OTEL_TRACES_SAMPLER` | `always_on`, in Development only | | `OTEL_BSP_SCHEDULE_DELAY`, `OTEL_BLRP_SCHEDULE_DELAY`, `OTEL_METRIC_EXPORT_INTERVAL` | `1000` ms, in Development only | The OpenTelemetry .NET SDK reads these standard variables, which is why the dashboard fills within a second during local runs. ## Why a deployed service goes quiet Run the rating engine on a platform that does not set `OTEL_EXPORTER_OTLP_ENDPOINT` and the gate stays closed: instrumentation still records data, no exporter ships it, and nothing fails or logs a warning. Console logging still works, which hides the problem until someone opens the tracing backend. The fix is configuration, not code: - set `OTEL_EXPORTER_OTLP_ENDPOINT` (and the protocol and headers your collector needs) in each environment; - set `OTEL_SERVICE_NAME` so the five services do not show up under generic names; - remember that `always_on` sampling and the short export intervals were Development settings from the AppHost, so production sampling is whatever you configure. ## Docs versus template The aspire.dev ServiceDefaults page says the method uses an `AlwaysOnSampler` in development and instruments gRPC. The 13.6 template sets **no sampler** and leaves `AddGrpcClientInstrumentation()` commented out. The always-on behaviour observed under `aspire run` comes from the `OTEL_TRACES_SAMPLER` variable the AppHost injects. A team that relies on the docs will expect gRPC client spans the template never produces. ## Custom spans Tracing listens to one custom source: the one named after the application. Code in the rating engine that creates `new ActivitySource("Quotes.Pricing")` produces activities that no listener samples, so they never reach the dashboard. Either name the source after the application or add `tracing.AddSource("Quotes.Pricing")` in `Extensions.cs`, which is exactly the kind of edit the shared project exists for. Custom meters work the same way and need `AddMeter`. ## A deployment checklist For each of the five quoting services and each environment, confirm: 1. `OTEL_EXPORTER_OTLP_ENDPOINT` points at a collector the service can reach. 2. `OTEL_EXPORTER_OTLP_PROTOCOL` matches what that endpoint accepts. 3. `OTEL_SERVICE_NAME` is set to a stable, distinct name. 4. A sampling policy is configured on purpose, since the Development `always_on` value is gone. 5. Every custom `ActivitySource` and `Meter` name is registered in `Extensions.cs`. ## Why the gate is designed this way Making export conditional keeps a service runnable anywhere: unit tests, a plain `dotnet run` or a container without a collector start cleanly instead of logging connection errors to a missing endpoint. The cost is that the absence of telemetry is not an error. Interviewers like this question because it separates candidates who have only seen the dashboard light up under `aspire run` from those who have shipped a service and gone looking for its traces. ## Logs travel the same road `builder.Logging.AddOpenTelemetry(...)` routes every `ILogger` record into the OpenTelemetry pipeline. With the gate open, those records leave as OTLP logs carrying the current trace and span ids, which is how the dashboard links a log line to its trace. `IncludeFormattedMessage = true` adds the rendered message next to the template, and `IncludeScopes = true` attaches logging scopes as attributes. With the gate closed, the same records still reach the console provider, so a service looks healthy in its console output while its central log store stays empty.

  • Why does the template filter /health and /alive out of tracing?
    Platform probes call those paths every few seconds. Without the `Filter` on `AddAspNetCoreInstrumentation`, each probe would become a server span, drowning real requests in the trace view and costing export volume. The filter compares `Request.Path` against the same constants `MapDefaultEndpoints` uses, so renaming a path means updating both.
  • How would you give the rating engine's custom ActivitySource spans a place in the dashboard?
    Add the source name to tracing in `Extensions.cs`, for example `tracing.AddSource("Quotes.Pricing")`, or create the source with `builder.Environment.ApplicationName` as its name. Because every service uses the same shared project, adding a naming convention there covers all five services at once.

  • ServiceDefaults exports OTLP even when no endpoint is configured.
  • The template itself sets an AlwaysOnSampler for Development.
  • Spans from any ActivitySource in the service are exported automatically.
  • The ServiceDefaults template traces outgoing gRPC calls out of the box.
  • Health-probe requests appear as a trace on every probe.

Assumes the Aspire 13.6 ServiceDefaults template and OpenTelemetry .NET 1.19's UseOtlpExporter; the aspire.dev page's AlwaysOnSampler and gRPC claims do not match this template.