arolariu.Backend.Common.Telemetry.Metering
arolariu.Backend.Common
arolariu.Backend.Common.Telemetry.Metering Namespace
Classes
MeterGenerators Class
Provides centralized System.Diagnostics.Metrics.Meter instances for custom metrics across application components.
This mirrors ActivityGenerators which provides per-bounded-context ActivitySource instances.
public static class MeterGenerators
Inheritance System.Object 🡒 MeterGenerators
Example
// Creating a counter from the Invoice meter
private static readonly Counter<long> InvoicesCreated =
MeterGenerators.InvoiceMeter.CreateCounter<long>("invoices.created", description: "Total invoices created");
// Recording an observation
InvoicesCreated.Add(1, new KeyValuePair<string, object?>("user.id", userId.ToString()));
Remarks
Each bounded context has its own System.Diagnostics.Metrics.Meter to ensure metric isolation and clear ownership:
- CommonMeter — Infrastructure metrics (config refresh, Key Vault, etc.)
- CoreMeter — Core application metrics (health checks, startup)
- AuthMeter — Authentication/authorization metrics (logins, token operations)
- InvoiceMeter — Invoice domain metrics (CRUD counts, analysis duration, RU cost)
All meters are registered in AddOTelMetering(this WebApplicationBuilder) via AddMeter().
Fields
MeterGenerators.AuthMeter Field
Meter for the Authentication package — auth event metrics.
public static readonly Meter AuthMeter;
Field Value
System.Diagnostics.Metrics.Meter
MeterGenerators.CommonMeter Field
Meter for the Common package — infrastructure and configuration metrics.
public static readonly Meter CommonMeter;
Field Value
System.Diagnostics.Metrics.Meter
MeterGenerators.CoreMeter Field
Meter for the Core package — application lifecycle and health metrics.
public static readonly Meter CoreMeter;
Field Value
System.Diagnostics.Metrics.Meter
MeterGenerators.InvoiceMeter Field
Meter for the Invoices domain package — business operation metrics.
public static readonly Meter InvoiceMeter;
Field Value
System.Diagnostics.Metrics.Meter
MeteringExtensions Class
Provides extension methods for configuring OpenTelemetry metrics collection with Azure Monitor integration. This class sets up performance monitoring and metrics export to Azure Application Insights.
public static class MeteringExtensions
Inheritance System.Object 🡒 MeteringExtensions
Example
// Usage in Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.AddOTelMetering();
Remarks
This extension configures: - ASP.NET Core metrics for HTTP request performance monitoring - HTTP client metrics for dependency call tracking - Azure Monitor exporter for cloud-based metrics aggregation - Console exporter for local development and debugging
Methods
MeteringExtensions.AddOTelMetering(this WebApplicationBuilder) Method
Configures OpenTelemetry metrics collection with automatic instrumentation and Azure Monitor export. This method enables comprehensive performance monitoring for ASP.NET Core applications and HTTP clients.
public static void AddOTelMetering(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder);
Parameters
builder Microsoft.AspNetCore.Builder.WebApplicationBuilder
The Microsoft.AspNetCore.Builder.WebApplicationBuilder to configure with OpenTelemetry metrics.
Exceptions
System.ArgumentNullException
Thrown when builder is null.
Remarks
This method configures metrics collection for: - ASP.NET Core instrumentation: HTTP request duration, response codes, and throughput - HTTP client instrumentation: Outbound request performance and dependency tracking - Console export during debugging for immediate feedback - Azure Monitor export for production monitoring and alerting - Managed Identity authentication for secure cloud integration