Resource Detectors in OpenTelemetry .NET
Resource detectors automatically detect and collect information about the environment in which your application is running. This information is attached to telemetry data (traces, metrics, and logs) to provide additional context for monitoring and debugging.
OpenTelemetry .NET provides several built-in resource detectors, and you can create custom detectors to gather application-specific information.
By default, OpenTelemetry .NET uses basic resource detection, but you can configure additional detectors:
using OpenTelemetry;
using OpenTelemetry.Resources;
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddEnvironmentVariableDetector()
.AddTelemetrySdk()
.AddService("my-service", "1.0.0")
)
.AddSource("MyApp.*")
.AddOtlpExporter()
.Build();
Built-in detectors
Environment Variable Detector
Detects resource attributes from OTEL_RESOURCE_ATTRIBUTES
and OTEL_SERVICE_NAME
environment variables.
.AddEnvironmentVariableDetector()
Environment variables:
export OTEL_SERVICE_NAME="user-api"
export OTEL_RESOURCE_ATTRIBUTES="service.version=1.2.0,deployment.environment=production"
Telemetry SDK Detector
Adds OpenTelemetry SDK information (name, language, version).
.AddTelemetrySdk()
Service Detector
Manually specify service information.
.AddService(
serviceName: "user-api",
serviceVersion: "1.2.0",
serviceInstanceId: Environment.MachineName
)
ASP.NET Core integration
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddEnvironmentVariableDetector()
.AddTelemetrySdk()
.AddService(
serviceName: "user-api",
serviceVersion: "1.2.0"
)
)
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter()
);
Cloud detectors
Azure
For Azure environments, install the Azure detector package:
dotnet add package OpenTelemetry.ResourceDetectors.Azure
using OpenTelemetry.ResourceDetectors.Azure;
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddAzureVMDetector()
.AddEnvironmentVariableDetector()
.AddTelemetrySdk()
)
AWS
For AWS environments, install the AWS detector package:
dotnet add package OpenTelemetry.ResourceDetectors.AWS
using OpenTelemetry.ResourceDetectors.AWS;
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddAWSEC2Detector()
.AddEnvironmentVariableDetector()
.AddTelemetrySdk()
)
Custom resource detector
Create custom detectors by implementing IResourceDetector
:
using OpenTelemetry.Resources;
public class CustomDetector : IResourceDetector
{
public Resource Detect()
{
return Resource.CreateFromAttributes(new[]
{
new KeyValuePair<string, object>("deployment.datacenter", GetDataCenter()),
new KeyValuePair<string, object>("application.team", "platform")
});
}
private string GetDataCenter()
{
return Environment.GetEnvironmentVariable("DATACENTER") ?? "unknown";
}
}
// Usage
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddDetector(new CustomDetector())
.AddEnvironmentVariableDetector()
.AddTelemetrySdk()
)
Manual attributes
Add custom attributes directly:
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddEnvironmentVariableDetector()
.AddTelemetrySdk()
.AddAttributes(new[]
{
new KeyValuePair<string, object>("host.name", Environment.MachineName),
new KeyValuePair<string, object>("process.pid", Environment.ProcessId),
new KeyValuePair<string, object>("deployment.environment", "production")
})
)
OpenTelemetry APM
Uptrace is a OpenTelemetry backend that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.
Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.
Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.
In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.