The TaskHub.Shared.Observability.OpenTelemetry module is the central nervous system for monitoring and diagnostics in TaskHub. It provides a standardized, pre-configured setup for the three pillars of observability: Traces, Metrics, and Logs.
When you call AddAppOpenTelemetry, the system initializes a comprehensive pipeline:
service.name, service.version, and deployment.environment based on its assembly metadata and configuration.AspNetCoreInstrumentation: Captures incoming HTTP requests.HttpClientInstrumentation: Captures outgoing HTTP calls to other services.EntityFrameworkCoreInstrumentation: Captures SQL queries and database operations.SqlClientInstrumentation: Low-level SQL tracking for non-EF providers.RedisInstrumentation: Tracks performance of distributed cache operations.The module ensures that TraceId and SpanId are correctly propagated across the entire microservice ecosystem using the W3C traceparent header. This allows a single user request to be visualized as a continuous flow from the API Gateway down to the database.
AddAppOpenTelemetry(Action<OpenTelemetryOptions> configure)| Parameter | Description |
| :— | :— |
| ServiceName | Unique identifier for the service (e.g., TaskHub.Identity). |
| OtlpEndpoint | The URL of the OpenTelemetry Collector (e.g., http://tempo:4317). |
| SamplingRatio | Value between 0.0 and 1.0. 1.0 means capture everything. |
Program.cs)builder.Services.AddAppOpenTelemetry(options =>
{
options.ServiceName = "TaskHub.UserService";
options.OtlpEndpoint = "http://otel-collector:4317";
options.SamplingRatio = 1.0; // Sample all requests in development
});
public class MyBusinessLogic
{
private static readonly ActivitySource MyActivitySource = new("TaskHub.CustomLogic");
public async Task ProcessDataAsync()
{
// Start a manual span
using var activity = MyActivitySource.StartActivity("DataCrunching");
activity?.SetTag("item.count", 100);
activity?.SetTag("logic.version", "2.1");
try
{
// ... work ...
activity?.SetStatus(ActivityStatusCode.Ok);
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
activity?.AddException(ex);
throw;
}
}
}
appsettings.json)"OpenTelemetry": {
"OtlpEndpoint": "http://otel-collector:4317",
"SamplingRatio": 0.5,
"Exporters": {
"IsOtlpEnabled": true,
"IsPrometheusEnabled": true,
"IsZipkinEnabled": false
},
"Instrumentation": {
"IsSqlEnabled": true,
"IsRedisEnabled": true,
"IsHttpEnabled": true
}
}
Every trace in TaskHub includes these by default:
service.name: The source service.host.name: The machine or pod name.http.method: e.g., GET, POST.http.status_code: e.g., 200, 404.db.system: e.g., postgresql.command.name: Present in spans created by the CommandsBus.result.code: The TaskHub standard result code.user.id: If the user is authenticated, their ID is tagged.SamplingRatio to reduce volume in high-traffic production environments.CancellationToken as it often carries internal tracing context in .NET.