Tracing
MySqlConnector instruments database operations with .NET’s ActivitySource API. The source name is MySqlConnector; subscribe to it from OpenTelemetry with AddSource("MySqlConnector").
OpenTelemetry
Install the OpenTelemetry NuGet package and an exporter such as OpenTelemetry.Exporter.OpenTelemetryProtocol.
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyApplication"))
.AddSource("MySqlConnector")
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri("http://otel-collector:4318/v1/traces");
options.Protocol = OtlpExportProtocol.HttpProtobuf;
})
.Build();
When using MySqlDataSourceBuilder, configure MySqlConnector tracing options with ConfigureTracing:
await using var dataSource = new MySqlDataSourceBuilder(connectionString)
.ConfigureTracing(options => options
.WithSemanticConventionsKinds(MySqlConnectorSemanticConventionsKinds.Stable))
.Build();
Semantic Conventions
MySqlConnector supports both legacy experimental database attributes and stable OpenTelemetry database attributes.
These are controlled by the OTEL_SEMCONV_STABILITY_OPT_IN environment variable, which can be set to one of the following values:
OTEL_SEMCONV_STABILITY_OPT_IN | Attributes emitted |
|---|---|
| unset, empty, or unrecognized | Legacy experimental attributes |
database | Stable attributes |
database/dup | Both legacy experimental and stable attributes |
Use database/dup while migrating dashboards, alerts, and collector processors, then switch to database when all consumers use stable names.
You can override the environment variable for a specific data source:
await using var dataSource = new MySqlDataSourceBuilder(connectionString)
.ConfigureTracing(options => options.WithSemanticConventionsKinds(
MySqlConnectorSemanticConventionsKinds.Experimental |
MySqlConnectorSemanticConventionsKinds.Stable))
.Build();
Spans
All spans use ActivityKind.Client.
| Span name | Created by | Notes |
|---|---|---|
Open | MySqlConnection.Open() and OpenAsync() | Covers connection establishment or pool checkout. |
Execute | MySqlCommand.Execute*() and MySqlBatch.Execute*() | Begins when the command is sent and ends when the MySqlDataReader is closed or disposed. |
Commit | MySqlTransaction.Commit() and CommitAsync() | Covers the transaction commit round-trip. |
Rollback | MySqlTransaction.Rollback() and RollbackAsync() | Covers explicit or implicit rollback. |
Stable Attributes
These attributes are emitted when MySqlConnectorSemanticConventionsKinds.Stable is active.
Connection Attributes
| Attribute | Type | Value |
|---|---|---|
db.system.name | string | mysql |
db.namespace | string | Database name, if one is available without an additional network call. |
server.address | string | Server host name, IP address, Unix socket path, or named-pipe host. |
server.port | int | Server port when it is not the MySQL default port, 3306. |
network.peer.address | string | Resolved TCP/IP address when applicable. |
network.peer.port | int | TCP port when network.peer.address is set. |
db.connection_id | string | MySQL server-assigned connection/thread ID. This is a MySqlConnector extension attribute. |
db.connection_string and db.user are legacy attributes and are not emitted in stable-only mode.
Execute Attributes
| Attribute | Type | Value |
|---|---|---|
db.query.text | string | MySqlCommand.CommandText for text commands. |
db.operation.name | string | Set only when known without parsing query text, e.g., CALL, COMMIT, ROLLBACK, or BATCH. |
db.stored_procedure.name | string | Stored procedure name when CommandType is StoredProcedure. |
db.operation.batch.size | int | Number of commands in a MySqlBatch, when the batch contains at least two commands. |
MySqlConnector does not emit db.collection.name, db.query.summary, db.query.parameter.<key>, or db.response.returned_rows.
Error Attributes
| Attribute | Type | Value |
|---|---|---|
error.type | string | MySQL error number for MySqlException, or the canonical exception type for non-MySQL exceptions. |
db.response.status_code | string | MySQL error number as a string, e.g., "1045". |
When an exception occurs, the span status is set to Error.
Legacy Attribute Mapping
When MySqlConnectorSemanticConventionsKinds.Experimental is active, MySqlConnector emits legacy attribute names. In database/dup mode, both legacy and stable attributes are emitted when a stable replacement exists.
| Legacy attribute | Stable replacement | Notes |
|---|---|---|
db.system | db.system.name | Both have value mysql. |
db.name | db.namespace | Database name. |
db.statement | db.query.text | SQL text for text commands. |
net.peer.name | server.address | Host name, Unix socket path, or named-pipe path in legacy mode. |
net.peer.port | server.port | Legacy value is a string; stable value is an int. |
net.peer.ip | network.peer.address | Resolved TCP/IP address. |
db.connection_string | none | Legacy only; may contain sensitive data if Persist Security Info=true. |
db.user | none | Legacy only. |
net.transport | none | Legacy only; there is no stable database-span replacement. |
Events
When an exception occurs, the span status is set to Error.
Stable: exception
Added when MySqlConnectorSemanticConventionsKinds.Stable is active.
Legacy experimental: db.client.operation.exception
Added when MySqlConnectorSemanticConventionsKinds.Experimental is active.
Both event names contain:
| Tag | Value |
|---|---|
exception.type | Exception type name. |
exception.message | Exception message. |
exception.stacktrace | Exception.ToString() output. |
read-result-set-header
This opt-in event is emitted on Execute spans when a result-set header is received from the server. Enable it with:
await using var dataSource = new MySqlDataSourceBuilder(connectionString)
.ConfigureTracing(options => options.EnableResultSetHeaderEvent())
.Build();
W3C Trace Context Propagation
When an Execute span is active and the .NET Activity uses W3C ID format, MySqlConnector sends the active trace context to MySQL Server as query attributes when the server supports query attributes.
| Query attribute | Value |
|---|---|
traceparent | The W3C traceparent value from Activity.Id. |
tracestate | The W3C tracestate value, when non-empty. |
If traceparent or tracestate is already present in MySqlCommand.Attributes, MySqlConnector does not add a duplicate value.
Privacy Notes
db.query.text records command text. Parameterized queries are recommended because parameter values are not recorded, but literal values embedded directly in SQL text can appear in telemetry.
Legacy db.connection_string and db.user can also contain sensitive deployment information. Use stable-only mode, or drop these attributes in the OpenTelemetry Collector, if they should not be exported.