![]() |
VOOZH | about |
dotnet add package OpenTelemetry.Instrumentation.SqlClient --version 1.15.2
NuGet\Install-Package OpenTelemetry.Instrumentation.SqlClient -Version 1.15.2
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.15.2" />
<PackageVersion Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.15.2" />Directory.Packages.props
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" />Project file
paket add OpenTelemetry.Instrumentation.SqlClient --version 1.15.2
#r "nuget: OpenTelemetry.Instrumentation.SqlClient, 1.15.2"
#:package OpenTelemetry.Instrumentation.SqlClient@1.15.2
#addin nuget:?package=OpenTelemetry.Instrumentation.SqlClient&version=1.15.2Install as a Cake Addin
#tool nuget:?package=OpenTelemetry.Instrumentation.SqlClient&version=1.15.2Install as a Cake Tool
| Status | |
|---|---|
| Stability | Stable |
| Code Owners | @open-telemetry/dotnet-contrib-maintainers |
This is an Instrumentation Library, which instruments Microsoft.Data.SqlClient and System.Data.SqlClient and collects traces about database operations.
This component is based on v1.33 of database semantic conventions. For details on the default set of attributes that are added, check out the Traces and Metrics sections below.
Instrumentation is not working with Microsoft.Data.SqlClient v3.* due to
the issue. It was fixed in 4.0
and later.
Add a reference to the
OpenTelemetry.Instrumentation.SqlClient
package. Also, add any other instrumentations & exporters you will need.
dotnet add package OpenTelemetry.Instrumentation.SqlClient
SqlClient instrumentation must be enabled at application startup.
The following example demonstrates adding SqlClient traces 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.Trace;
public class Program
{
public static void Main(string[] args)
{
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSqlClientInstrumentation()
.AddConsoleExporter()
.Build();
}
}
The instrumentation adheres to the semantic conventions for database client spans. An activity emitted by the instrumentation will include the following list of attributes:
error.typedb.namespacedb.operation.namedb.query.summarydb.query.textdb.response.status_codedb.stored_procedure.namedb.system.nameserver.addressserver.portThe following example demonstrates adding SqlClient metrics 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.Metrics;
public class Program
{
public static void Main(string[] args)
{
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSqlClientInstrumentation()
.AddConsoleExporter()
.Build();
}
}
The instrumentation adheres to the semantic conventions for database client metrics.
Currently, the instrumentation supports the following metric and attributes.
| Name | Instrument Type | Unit | Description |
|---|---|---|---|
db.client.operation.duration |
Histogram | s |
Duration of database client operations. |
error.typedb.namespacedb.operation.namedb.query.summarydb.response.status_codedb.stored_procedure.namedb.system.nameserver.addressserver.portFor 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.
This instrumentation can be configured to change the default behavior by using
SqlClientTraceInstrumentationOptions.
EnrichWithSqlCommand is available on .NET runtimes only.
This option can be used to enrich the activity with additional information from
the raw SqlCommand object. The EnrichWithSqlCommand action is called only
when activity.IsAllDataRequested is true. It contains the activity itself
(which can be enriched), the name of the event, and the actual raw object.
Currently there is only one event name reported, "OnCustom". The actual object
is Microsoft.Data.SqlClient.SqlCommand for Microsoft.Data.SqlClient and
System.Data.SqlClient.SqlCommand for System.Data.SqlClient.
The following code snippet shows how to add additional tags using
EnrichWithSqlCommand.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSqlClientInstrumentation(opt => opt.EnrichWithSqlCommand
= (activity, obj) =>
{
if (obj is SqlCommand cmd)
{
activity.SetTag("db.commandTimeout", cmd.CommandTimeout);
}
})
.Build();
Processor,
is the general extensibility point to add additional properties to any activity.
The EnrichWithSqlCommand option is specific to this instrumentation, and is
provided to get access to SqlCommand object.
RecordException is available on .NET runtimes only.
This option can be set to instruct the instrumentation to record SqlExceptions as Activity events.
The default value is false and can be changed by the code like below.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSqlClientInstrumentation(
options => options.RecordException = true)
.AddConsoleExporter()
.Build();
Filter is available on .NET runtimes only.
This option can be used to filter out activities based on the properties of the
SqlCommand object being instrumented using a Func<object, bool>. The
function receives an instance of the raw SqlCommand and should return true
if the telemetry is to be collected, and false if it should not. The parameter
of the Func delegate is of type object and needs to be cast to the appropriate
type of SqlCommand, either Microsoft.Data.SqlClient.SqlCommand or
System.Data.SqlClient.SqlCommand. The example below filters out all commands
that are not stored procedures.
using var traceProvider = Sdk.CreateTracerProviderBuilder()
.AddSqlClientInstrumentation(
opt =>
{
opt.Filter = cmd =>
{
if (cmd is SqlCommand command)
{
return command.CommandType == CommandType.StoredProcedure;
}
return false;
};
})
.AddConsoleExporter()
.Build();
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.
This feature is available on .NET runtimes only.
The OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_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_SQLCLIENT_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.
Only CommandType.Text commands are supported for trace context propagation.
Only .NET runtimes are supported.
Database trace context propagation can be enabled by setting
OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_CONTEXT_PROPAGATION
environment variable to true.
This uses the SET CONTEXT_INFO
command to set traceparent
information for the current connection, which results in
an additional round-trip to the database.
Activity.Duration represents the time the underlying connection takes to
execute the command/query. Completing the operation includes the time up to
determining that the request was successful. It doesn't include the time spent
reading the results from a query set (for example enumerating all the rows
returned by a data reader).
This is illustrated by the code snippet below:
using var connection = new SqlConnection("...");
connection.Open();
using var command = connection.CreateCommand();
command.CommandText = "select top 100000 * from Users";
// Activity duration starts
using var reader = command.ExecuteReader();
// Activity duration ends
// Not included in the Activity duration
while (reader.Read())
{
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on OpenTelemetry.Instrumentation.SqlClient:
| Package | Downloads |
|---|---|
|
Microsoft.ApplicationInsights.AspNetCore
Application Insights for ASP.NET Core web applications. See https://azure.microsoft.com/documentation/articles/app-insights-asp-net-five/ for more information. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156 |
|
|
Microsoft.ApplicationInsights.WorkerService
Application Insights for .NET Core Worker Service (messaging, background tasks, and any non-HTTP workloads) applications. See https://docs.microsoft.com/azure/azure-monitor/app/worker-service for more information. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156 |
|
|
Microsoft.ApplicationInsights.Web
Application Insights for .NET web applications. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156 |
|
|
OpenTelemetry.AutoInstrumentation.Runtime.Managed
Managed components used by the OpenTelemetry.AutoInstrumentation project. |
|
|
Aspire.Microsoft.EntityFrameworkCore.SqlServer
A Microsoft SQL Server provider for Entity Framework Core that integrates with Aspire, including connection pooling, health check, logging, and telemetry. |
Showing the top 11 popular GitHub repositories that depend on OpenTelemetry.Instrumentation.SqlClient:
| Repository | Stars |
|---|---|
|
microsoft/aspire
Aspire is the tool for code-first, extensible, observable dev and deploy.
|
|
|
DuendeSoftware/products
The most flexible and standards-compliant OpenID Connect and OAuth 2.x framework for ASP.NET Core
|
|
|
thangchung/clean-architecture-dotnet
🕸 Yet Another .NET Clean Architecture, but for Microservices project. It uses Minimal Clean Architecture with DDD-lite, CQRS-lite, and just enough Cloud-native patterns apply on the simple eCommerce sample and run on Tye with Dapr extension 🍻
|
|
|
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
|
|
|
Aguafrommars/TheIdServer
OpenID/Connect, OAuth2, WS-Federation and SAML 2.0 server based on Duende IdentityServer and ITFoxtec Identity SAML 2.0 with its admin UI
|
|
|
microsoft/ApplicationInsights-dotnet
ApplicationInsights-dotnet
|
|
|
damikun/trouble-training
FullStack DDD/CQRS with GraphQL workshop including distributed tracing and monitoring. This shows the configuration from React frontend to .Net backend.
|
|
|
TanvirArjel/CleanArchitecture
This repository contains the implementation of domain-driven design and clear architecture in ASP.NET Core.
|
|
| dotnet/systemweb-adapters | |
|
Azure/modern-web-app-pattern-dotnet
The Modern Web App Pattern is a set of objectives to help you apply an iterative change to modernize a cloud deployed monolith. This content builds on the Reliable Web App. This repo contains a reference implementation of a Modern Web App for .NET.
|
|
|
marinasundstrom/YourBrand
Prototype enterprise system for e-commerce and consulting services
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.15.2 | 4,635,572 | 4/21/2026 |
| 1.15.1 | 2,068,923 | 3/4/2026 |
| 1.15.0 | 5,623,257 | 1/28/2026 |
| 1.15.0-rc.1 | 158,944 | 1/21/2026 |
| 1.14.0-rc.1 | 192,553 | 1/13/2026 |
| 1.14.0-beta.1 | 1,949,510 | 11/13/2025 |
| 1.13.0-beta.2 | 233,895 | 11/3/2025 |
| 1.13.0-beta.1 | 245,644 | 10/22/2025 |
| 1.12.0-beta.3 | 1,075,142 | 9/25/2025 |
| 1.12.0-beta.2 | 4,067,829 | 7/15/2025 |
| 1.12.0-beta.1 | 2,988,243 | 5/6/2025 |
| 1.11.0-beta.2 | 3,247,233 | 3/5/2025 |
| 1.11.0-beta.1 | 1,826,372 | 1/27/2025 |
| 1.10.0-beta.1 | 1,251,176 | 12/9/2024 |
| 1.9.0-beta.1 | 17,545,786 | 6/17/2024 |
| 1.8.0-beta.1 | 7,389,925 | 4/4/2024 |
| 1.7.0-beta.1 | 3,043,839 | 2/10/2024 |
| 1.6.0-beta.3 | 5,047,728 | 11/17/2023 |
| 1.6.0-beta.2 | 990,214 | 10/27/2023 |
| 1.5.1-beta.1 | 5,328,164 | 7/21/2023 |