-
Notifications
You must be signed in to change notification settings - Fork 8
Enable with manual OpenTelemetry setup
You do not have to use the Azure Monitor OpenTelemetry distro
(UseAzureMonitor()) to run the profiler. If you configure OpenTelemetry
yourself — for example because you switch exporters per environment, register
your own ActivitySources, or customize sampling — you can still enable the
profiler with a single call.
This is the scenario from issue #20.
In addition to the Get Started prerequisites, you only need the profiler package:
dotnet add package Azure.Monitor.OpenTelemetry.Profiler --prereleaseYou do not need Azure.Monitor.OpenTelemetry.AspNetCore. The profiler
depends on Azure.Monitor.OpenTelemetry.Exporter only, so it composes with a
hand-rolled OpenTelemetry pipeline.
Call AddAzureMonitorProfiler() on the builder returned by
AddOpenTelemetry(), after your own tracing configuration:
using Azure.Monitor.OpenTelemetry.Profiler;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService("my-service"))
.WithTracing(tracerProviderBuilder =>
{
tracerProviderBuilder
.AddAspNetCoreInstrumentation()
.AddSource("MyCompany.MyProduct");
if (useAzureMonitor)
{
tracerProviderBuilder.AddAzureMonitorTraceExporter();
}
else
{
// For example, the Aspire Dashboard during local development.
tracerProviderBuilder.AddOtlpExporter();
}
})
.AddAzureMonitorProfiler(); // Enable the profilerAddAzureMonitorProfiler() is a plain registration call, so you can gate it on
the same flag that selects your exporter. Both an IOpenTelemetryBuilder and an
IServiceCollection overload exist:
var otelBuilder = builder.Services.AddOpenTelemetry()
.ConfigureResource(...)
.WithTracing(...);
if (useAzureMonitor)
{
otelBuilder.AddAzureMonitorProfiler();
}
// Equivalent, when you only have the IServiceCollection at hand:
// if (useAzureMonitor) builder.Services.AddAzureMonitorProfiler();Calling it more than once is safe — registration is skipped if the profiler services are already present.
Skipping UseAzureMonitor() does not leave anything unconfigured for the
profiler, but two inputs are worth knowing about.
The profiler always needs an Application Insights connection string. It resolves one in this order, taking the first non-empty value:
- The
ServiceProfiler:ConnectionStringconfiguration section (for example fromappsettings.jsonor environment variables — see the Configuration Guide). -
AzureMonitorExporterOptions.ConnectionString— this is populated for you if you callAddAzureMonitorTraceExporter(), with or without the distro. - The
APPLICATIONINSIGHTS_CONNECTION_STRINGenvironment variable.
If none of these is set, the profiler disables itself and logs an actionable error at startup rather than failing your application.
Note: the Azure Monitor exporter itself also requires a connection string and will throw during startup if one is missing. That failure comes from the exporter, not the profiler.
The profiler reads the cloud role name, role instance, and application version
from the OpenTelemetry Resource (service.name, service.instance.id, and
service.version), so the standard ConfigureResource(r => r.AddService(...))
call — or the OTEL_SERVICE_NAME / OTEL_RESOURCE_ATTRIBUTES environment
variables — is all you need. See
Setup the Role name
for details and fallbacks.
Older guidance (including comments on issue #20) suggested a
TracerProviderBuilder.UseProfiler() extension method. That method no longer
exists. Use AddAzureMonitorProfiler() on IOpenTelemetryBuilder or
IServiceCollection as shown above.