OpenTelemetry.Instrumentation.EntityFrameworkCore 1.13.0-beta.1
EntityFrameworkCore Instrumentation for OpenTelemetry .NET
Status | |
---|---|
Stability | Beta |
Code Owners | @martincostello, @matt-hensley |
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 |
.NET Standard 2.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- OpenTelemetry.Api.ProviderBuilderExtensions (>= 1.13.1 && < 2.0.0)
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 |