MySQL

MySQL is observed through the client span each query produces. This page lists the OpenTelemetry instrumentation for each language, what the span carries, and how statement text is handled.

The MySQL driver in your application is instrumented by OpenTelemetry, and every query becomes a client span inside the trace of the request that issued it. Nothing is installed on the database, so the same setup applies to a self-hosted server, RDS, Aurora, Cloud SQL, and any MySQL-compatible service.

What is collected

SpanOne CLIENT span per query, a child of the span that was active when the query ran
Identitydb.system.name = mysql (db.system in instrumentation releases that predate the stable database conventions)
StatementThe statement text and the operation
TargetDatabase name, server address and port
OutcomeDuration, and an error status with the driver's message when the query fails

The span reports how long the application waited. What MySQL did during that wait is not included: there is no server-side span path for MySQL comparable to pg_tracing for PostgreSQL.

Enabling it

The Sophonz Node.js SDK loads the instrumentation for both mysql2 (1.4.2 up to, not including, 4) and mysql (2.x). Query spans are produced with no configuration.

import { init } from '@sophonz/node-sdk';
 
init({
  service: 'checkout-api',
  apiKey: process.env.SOPHONZ_API_KEY,
  instrumentations: {
    '@opentelemetry/instrumentation-mysql2': {
      maskStatement: true,
    },
  },
});

maskStatement is off by default, so mysql2 spans carry the statement as it was sent. See Statement text.

Statement text

What ends up in the statement attribute differs by language.

LanguageDefault
Node.js mysql2The statement as sent, including literal values. maskStatement: true replaces numbers and quoted strings with ?
PythonThe statement with its placeholders. Bound parameters are not recorded
JavaSanitized. Literal values are replaced with ?

Queries built by string concatenation put their values in the statement text. Use bound parameters, or turn masking on, before sending production traffic.

SQLCommenter

mysql2 (addSqlCommenterCommentToQueries) and the Python MySQL instrumentations (enable_commenter) can append the W3C traceparent to each statement as a comment. The comment reaches the server with the statement, but nothing in MySQL turns it into spans, so the trace still ends at the client span. Leave it off unless something downstream reads it.

In Sophonz

Query spans sit under the request span in the trace, together with the browser or mobile session that started the request. They belong to the application's service: the Service Flow panel draws a node per service, so MySQL does not appear as a node of its own.

Because the attributes are stored with the span, they can be queried in Grafana.

SELECT name, count() AS calls, avg(durationNano) / 1e6 AS avg_ms
FROM sophonz_traces.distributed_sophonz_index_v2
WHERE $__timeFilter(timestamp)
  AND (stringTagMap['db.system.name'] = 'mysql' OR stringTagMap['db.system'] = 'mysql')
GROUP BY name
ORDER BY avg_ms DESC
LIMIT 20