OpenTelemetry.Instrumentation.EntityFrameworkCore 1.13.0-beta.1

EntityFrameworkCore Instrumentation for OpenTelemetry .NET

Status
Stability Beta
Code Owners @martincostello, @matt-hensley

NuGet version badge NuGet download count badge codecov.io

This is an Instrumentation Library, which instruments Microsoft.EntityFrameworkCore and collects traces about outgoing requests.

Note: This component is based on the OpenTelemetry semantic conventions for metrics and traces. These conventions are Experimental, and hence, this package is a pre-release. Until a stable version is released, there can be breaking changes.

Steps to enable OpenTelemetry.Instrumentation.EntityFrameworkCore

Step 1: Install Package

Add a reference to the OpenTelemetry.Instrumentation.EntityFrameworkCore package. Also, add any other instrumentations & exporters you will need.

dotnet add package --prerelease OpenTelemetry.Instrumentation.EntityFrameworkCore

Step 2: Enable EntityFrameworkCore Instrumentation at application startup

EntityFrameworkCore instrumentation must be enabled at application startup.

The following example demonstrates adding EntityFrameworkCore instrumentation to a console application. This example also sets up the OpenTelemetry Console exporter, which requires adding the package OpenTelemetry.Exporter.Console to the application.

using OpenTelemetry;
using OpenTelemetry.Trace;

public class Program
{
    public static void Main(string[] args)
    {
        using var tracerProvider = Sdk.CreateTracerProviderBuilder()
            .AddEntityFrameworkCoreInstrumentation()
            .AddConsoleExporter()
            .Build();
    }
}

For an ASP.NET Core application, adding instrumentation is typically done in the ConfigureServices of your Startup class. Refer to documentation for OpenTelemetry.Instrumentation.AspNetCore.

For an ASP.NET application, adding instrumentation is typically done in the Global.asax.cs. Refer to the documentation for OpenTelemetry.Instrumentation.AspNet.

Advanced configuration

This instrumentation can be configured to change the default behavior by using EntityFrameworkInstrumentationOptions.

services.AddOpenTelemetry()
    .WithTracing(builder => builder
        .AddEntityFrameworkCoreInstrumentation(options =>
        {
            options.EnrichWithIDbCommand = (activity, command) =>
            {
                var stateDisplayName = $"{command.CommandType} main";
                activity.DisplayName = stateDisplayName;
                activity.SetTag("db.name", stateDisplayName);
            };
        })
        .AddConsoleExporter());

When used with OpenTelemetry.Extensions.Hosting, all configurations to EntityFrameworkInstrumentationOptions can be done in the ConfigureServices method of you applications Startup class as shown below.

// Configure
services.Configure<EntityFrameworkInstrumentationOptions>(options =>
{
    options.EnrichWithIDbCommand = (activity, command) =>
    {
        var stateDisplayName = $"{command.CommandType} main";
        activity.DisplayName = stateDisplayName;
        activity.SetTag("db.name", stateDisplayName);
    };
});

services.AddOpenTelemetry()
    .WithTracing(builder => builder
        .AddEntityFrameworkCoreInstrumentation()
        .AddConsoleExporter());

Filter

This option can be used to filter out activities based on the provider name and the properties of the db command object being instrumented using a Func<string, IDbCommand, bool>. The function receives a provider name and an instance of the db command and should return true if the telemetry is to be collected, and false if it should not.

The following code snippet shows how to use Filter to collect traces for stored procedures only.

services.AddOpenTelemetry()
    .WithTracing(builder => builder
        .AddEntityFrameworkCoreInstrumentation(options =>
        {
            options.Filter = (providerName, command) =>
            {
                return command.CommandType == CommandType.StoredProcedure;
            };
        })
        .AddConsoleExporter());

Experimental features

[!NOTE] Experimental features are not enabled by default and can only be activated with environment variables. They are subject to change or removal in future releases.

DB query parameters

The OTEL_DOTNET_EXPERIMENTAL_EFCORE_ENABLE_TRACE_DB_QUERY_PARAMETERS environment variable controls whether db.query.parameter.<key> attributes are emitted.

Query parameters may contain sensitive data, so only enable this experimental feature if your queries and/or environment are appropriate for enabling this option.

OTEL_DOTNET_EXPERIMENTAL_EFCORE_ENABLE_TRACE_DB_QUERY_PARAMETERS is implicitly false by default. When set to true, the instrumentation will set db.query.parameter.<key> attributes for each of the query parameters associated with a database command.

References

Showing the top 20 packages that depend on OpenTelemetry.Instrumentation.EntityFrameworkCore.

Packages Downloads
Quantum.Framework.Core.NetCore
Package Description
8
Quantum.Framework.Core.NetCore
Package Description
10
Quantum.Framework.Core.NetCore
Package Description
14
Quantum.Framework.Core.NetCore
Package Description
20
Quantum.Framework.Core.NetCore
Package Description
31

For detailed changes see: https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/c821c693ce317e27b222bcea29e12111635bff9e/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md.

Version Downloads Last updated
1.13.0-beta.1 1 10/23/2025
1.12.0-beta.2 4 07/24/2025
1.12.0-beta.1 2 08/22/2025
1.11.0-beta.2 23 08/22/2025
1.11.0-beta.1 2 08/22/2025
1.10.0-beta.1 2 08/22/2025
1.0.0-beta.12 2 08/22/2025
1.0.0-beta.11 2 08/22/2025
1.0.0-beta.10 2 08/22/2025
1.0.0-beta.9 2 08/22/2025
1.0.0-beta.8 2 08/22/2025
1.0.0-beta.7 2 08/22/2025
1.0.0-beta.6 3 08/22/2025
1.0.0-beta.5 2 08/22/2025
1.0.0-beta.4 2 08/22/2025
1.0.0-beta.3 2 08/22/2025