LinkMenuExpand(external link) DocumentSearchCopyCopied

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_INAttributes emitted
unset, empty, or unrecognizedLegacy experimental attributes
databaseStable attributes
database/dupBoth 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 nameCreated byNotes
OpenMySqlConnection.Open() and OpenAsync()Covers connection establishment or pool checkout.
ExecuteMySqlCommand.Execute*() and MySqlBatch.Execute*()Begins when the command is sent and ends when the MySqlDataReader is closed or disposed.
CommitMySqlTransaction.Commit() and CommitAsync()Covers the transaction commit round-trip.
RollbackMySqlTransaction.Rollback() and RollbackAsync()Covers explicit or implicit rollback.

Stable Attributes

These attributes are emitted when MySqlConnectorSemanticConventionsKinds.Stable is active.

Connection Attributes

AttributeTypeValue
db.system.namestringmysql
db.namespacestringDatabase name, if one is available without an additional network call.
server.addressstringServer host name, IP address, Unix socket path, or named-pipe host.
server.portintServer port when it is not the MySQL default port, 3306.
network.peer.addressstringResolved TCP/IP address when applicable.
network.peer.portintTCP port when network.peer.address is set.
db.connection_idstringMySQL 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

AttributeTypeValue
db.query.textstringMySqlCommand.CommandText for text commands.
db.operation.namestringSet only when known without parsing query text, e.g., CALL, COMMIT, ROLLBACK, or BATCH.
db.stored_procedure.namestringStored procedure name when CommandType is StoredProcedure.
db.operation.batch.sizeintNumber 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

AttributeTypeValue
error.typestringMySQL error number for MySqlException, or the canonical exception type for non-MySQL exceptions.
db.response.status_codestringMySQL 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 attributeStable replacementNotes
db.systemdb.system.nameBoth have value mysql.
db.namedb.namespaceDatabase name.
db.statementdb.query.textSQL text for text commands.
net.peer.nameserver.addressHost name, Unix socket path, or named-pipe path in legacy mode.
net.peer.portserver.portLegacy value is a string; stable value is an int.
net.peer.ipnetwork.peer.addressResolved TCP/IP address.
db.connection_stringnoneLegacy only; may contain sensitive data if Persist Security Info=true.
db.usernoneLegacy only.
net.transportnoneLegacy 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:

TagValue
exception.typeException type name.
exception.messageException message.
exception.stacktraceException.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 attributeValue
traceparentThe W3C traceparent value from Activity.Id.
tracestateThe 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.