Using OpenTelemetry to Gather and Export EF Core Performance Metrics

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:

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"):

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 NameInstrument TypeDescription
microsoft.entityframeworkcore.active_dbcontextsObservableUpDownCounterTracks the number of currently active DbContext instances.
microsoft.entityframeworkcore.queriesObservableCounterCumulative count of total database queries executed.
microsoft.entityframeworkcore.savechangesObservableCounterCumulative count of database save operations executed.
microsoft.entityframeworkcore.compiled_query_cache_hitsObservableCounterTracks cache hits for compiled queries.
microsoft.entityframeworkcore.compiled_query_cache_missesObservableCounterTracks 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:

Update your metrics registration in Program.cs:

Best and Cheap Entity Framework Core 10 Hosting

To help you find the best Windows hosting provider that fully support Entity Framework, we will give you our recommendation. After we had reviewed 50+ providers, we found that ASPHostPortal is one of the best. They have expert ASP.NET support who always ready to help your problem, although with 99.99% uptime we think you won’t get any problem.

ASPHostPortal.com is Microsoft No #1 Recommended Windows Hosting Partner ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for the ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, .NET 4.5.2/ASP.NET 4.5.1, ASP.NET MVC 6.0/5.2, Silverlight 5 and Visual Studio Lightswitch.

You may also like...

Popular Posts