Configurable OpenTelemetry setup for ASP.NET Core via appsettings.json — tracing, metrics and logging with a single AddTelemetry() call.
- Tracing: ASP.NET Core, HttpClient and SqlClient instrumentation out of the box.
- Metrics: ASP.NET Core, HttpClient and .NET runtime metrics.
- Logging: Structured log export via OpenTelemetry Protocol (OTLP).
- Flexible configuration: Bind from
appsettings.jsonor configure inline in code. - Extensible: Register custom instrumentation via
ConfigureTracing,ConfigureMetricsandConfigureLoggingcallbacks. - Protocol support: HTTP/protobuf (port 4318) and gRPC (port 4317).
Install via NuGet:
dotnet add package OpenTelemetryExtension.Configurationbuilder.Services.AddTelemetry(builder.Configuration);{
"Telemetry": {
"Endpoint": "http://localhost:4318",
"ServiceName": "my-api",
"EnvironmentName": "production"
}
}builder.Services.AddTelemetry(o =>
{
o.Endpoint = new Uri("http://localhost:4318");
o.ServiceName = "my-api";
o.EnvironmentName = "production";
});All options can be set via appsettings.json under the Telemetry key.
| Property | Type | Default | Description |
|---|---|---|---|
Enabled |
bool |
true |
Disables all telemetry when false. |
Endpoint |
Uri |
— (required) | OTLP collector endpoint, e.g. http://localhost:4318. |
Headers |
string |
"" |
Exporter headers, e.g. Authorization=Basic .... Format: key1=value1,key2=value2. |
Protocol |
string |
Grpc |
OTLP protocol. Valid values: HttpProtobuf, Grpc. |
ServiceName |
string? |
null |
Service name reported to the backend. |
EnvironmentName |
string? |
null |
Reported as deployment.environment resource attribute. |
EnableTracing |
bool |
true |
Enables distributed tracing. |
EnableMetrics |
bool |
true |
Enables metrics collection. |
EnableLogging |
bool |
true |
Enables log export. |
EnableAspNetCoreInstrumentation |
bool |
true |
Instruments incoming HTTP requests. |
EnableHttpClientInstrumentation |
bool |
true |
Instruments outgoing HttpClient requests. |
EnableSqlClientInstrumentation |
bool |
true |
Instruments SQL database calls. |
EnableRuntimeInstrumentation |
bool |
true |
Collects GC, memory and thread pool metrics. |
RecordExceptions |
bool |
true |
Records exceptions with stack traces on spans. |
ExcludeHealthChecks |
bool |
true |
Excludes /health endpoints from tracing. |
Note:
ConfigureTracing,ConfigureMetricsandConfigureLoggingcallbacks are only available when configuring inline in code — they cannot be set viaappsettings.json.
Register additional instrumentation libraries via the callbacks:
builder.Services.AddTelemetry(o =>
{
o.Endpoint = new Uri("http://localhost:4318");
// e.g. MySQL, Redis, MongoDB, ...
o.ConfigureTracing = tracing => tracing.AddSource("MyApp");
o.ConfigureMetrics = metrics => metrics.AddMeter("MyApp");
o.ConfigureLogging = logging => logging.AddConsole();
});Run the helm charts or use docker deploy with the cmd scripts.
See the deploy folder for all configuration files and startup scripts,
and the sample project for the corresponding appsettings configurations.
{
"Telemetry": {
"Endpoint": "http://localhost:18888",
"Protocol": "HttpProtobuf"
}
}{
"Telemetry": {
"Endpoint": "http://localhost:4318",
"Protocol": "HttpProtobuf"
}
}{
"Telemetry": {
"Endpoint": "http://localhost:50709"
}
}{
"Telemetry": {
"Endpoint": "http://localhost:30318",
"Protocol": "HttpProtobuf"
}
}Loki supports OTLP for logs only. Tracing and metrics must be disabled.
{
"Telemetry": {
"Endpoint": "http://localhost:3100/otlp",
"Protocol": "HttpProtobuf",
"EnableTracing": false,
"EnableMetrics": false
}
}{
"Telemetry": {
"Endpoint": "http://localhost:30117/api/default",
"Protocol": "HttpProtobuf",
"Headers": "Authorization=Basic <base64>,stream-name=default"
}
}{
"Telemetry": {
"Endpoint": "http://localhost:30118",
"Protocol": "Grpc",
"Headers": "Authorization=Basic <base64>,organization=default,stream-name=default"
}
}Contributions are welcome! If you'd like to improve the project, please:
- Check out our contributing guidelines.
- Ideally, open an issue before starting work.
- Submit a pull request with your changes.
Thank you for helping make OpenTelemetryExtension.Configuration better!
If you encounter any issues or bugs, please report them here.
For additional licensing and attribution details, see NOTICE.md and THIRD_PARTY_LICENSES.md.