Monitoring application performance is essential for catching database bottlenecks before they impact users. Entity Framework Core (EF Core) exposes performance data natively through the standard .NET System.Diagnostics.Metrics API under the meter name Microsoft.EntityFrameworkCore.
By integrating this meter with OpenTelemetry, you can automatically track active database connections, query volumes, and compiled query cache performance across your application.
Step-by-Step Implementation
1.Install Required Packages:Terminal command.
Run the following command to add the OpenTelemetry hosting and console exporter packages to your project:
Bash
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.Console
Verification: Check your .csproj file to confirm that OpenTelemetry.Extensions.Hosting and the exporter packages are successfully listed under <ItemGroup>.
2.Configure OpenTelemetry Metrics in Program.cs:Code setup.
Register OpenTelemetry metrics during application startup and subscribe to the EF Core meter using .AddMeter("Microsoft.EntityFrameworkCore"):
C#
using OpenTelemetry.Metrics;
var builder = WebApplication.CreateBuilder(args);
// Configure OpenTelemetry Metrics
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation() // Captures HTTP performance metrics
.AddRuntimeInstrumentation() // Captures .NET runtime metrics (memory, CPU)
.AddMeter("Microsoft.EntityFrameworkCore") // Captures EF Core built-in metrics
.AddConsoleExporter()); // Exports metrics to the console for local verification
var app = builder.Build();
// ... configure middleware and endpoints
app.Run();
Verification: Run dotnet build to verify that your project compiles cleanly without missing namespace references.
3.Verify Metric Collection:Runtime check.
Start your application and trigger a few API endpoints that execute database operations.
Verification: Inspect your application console output to confirm that performance counters like query counts and active contexts are actively streaming.
Key Metrics Tracked by EF Core
Once configured, EF Core automatically feeds the following performance measurements into your OpenTelemetry pipeline:
Metric Name |
Instrument Type |
Description |
|
ObservableUpDownCounter |
Tracks the number of currently active |
|
ObservableCounter |
Cumulative count of total database queries executed. |
|
ObservableCounter |
Cumulative count of database save operations executed. |
|
ObservableCounter |
Tracks cache hits for compiled queries. |
|
ObservableCounter |
Tracks cache misses for compiled queries. |
Production Setup: Exporting to Prometheus
In production environments, you typically replace the console exporter with a Prometheus exporter to visualize your metrics in Grafana:
Bash
dotnet add package OpenTelemetry.Exporter.Prometheus.AspNetCore
Update your metrics registration in Program.cs:
C#
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics => metrics
.AddMeter("Microsoft.EntityFrameworkCore")
.AddPrometheusExporter()); // Exposes a /metrics scraping endpoint
// Map the scraping endpoint for Prometheus
app.MapPrometheusScrapingEndpoint();
Join the conversation! Your thoughts help the community grow.