What is @opentelemetry/instrumentation-mysql?
@opentelemetry/instrumentation-mysql is an npm package that provides automatic instrumentation for MySQL database operations using the OpenTelemetry framework. This allows developers to collect and export telemetry data such as traces and metrics from MySQL queries, which can be used for monitoring and performance analysis.
What are @opentelemetry/instrumentation-mysql's main functionalities?
Automatic Instrumentation
This feature allows you to automatically instrument MySQL operations without modifying your existing codebase. The provided code sample shows how to set up the MySQL instrumentation with OpenTelemetry.
const { NodeTracerProvider } = require('@opentelemetry/node');
const { MySQLInstrumentation } = require('@opentelemetry/instrumentation-mysql');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
instrumentations: [
new MySQLInstrumentation(),
],
});
Custom Span Attributes
This feature allows you to add custom attributes to spans generated by MySQL operations. The code sample demonstrates how to add a custom attribute to the span that includes the MySQL response.
const { MySQLInstrumentation } = require('@opentelemetry/instrumentation-mysql');
const mysqlInstrumentation = new MySQLInstrumentation({
responseHook: (span, response) => {
span.setAttribute('mysql.response', JSON.stringify(response));
},
});
Error Handling
This feature allows you to handle errors in MySQL operations and set the span status accordingly. The code sample shows how to set the span status to an error code if the MySQL response contains an error.
const { MySQLInstrumentation } = require('@opentelemetry/instrumentation-mysql');
const mysqlInstrumentation = new MySQLInstrumentation({
responseHook: (span, response) => {
if (response.error) {
span.setStatus({ code: 2, message: response.error.message });
}
},
});
Other packages similar to @opentelemetry/instrumentation-mysql
@opentelemetry/instrumentation-pg
@opentelemetry/instrumentation-pg provides similar functionality for PostgreSQL databases. It allows for automatic instrumentation of PostgreSQL operations, enabling the collection of telemetry data for monitoring and performance analysis. The main difference is that it is specifically designed for PostgreSQL rather than MySQL.
mysql2
mysql2 is a MySQL client for Node.js that supports Promises and async/await. While it does not provide automatic instrumentation, it can be used in conjunction with OpenTelemetry to manually instrument MySQL operations. This package is more focused on providing a robust and performant MySQL client rather than telemetry.
sequelize
sequelize is a promise-based Node.js ORM for various SQL databases, including MySQL. It does not provide automatic instrumentation out of the box, but it can be integrated with OpenTelemetry for manual instrumentation. Sequelize is more focused on providing an ORM layer for database operations rather than telemetry.
OpenTelemetry mysql Instrumentation for Node.js
This module provides automatic instrumentation for the mysql
module, which may be loaded using the @opentelemetry/sdk-trace-node
package and is included in the @opentelemetry/auto-instrumentations-node
bundle.
If total installation size is not constrained, it is recommended to use the @opentelemetry/auto-instrumentations-node
bundle with @opentelemetry/sdk-node for the most seamless instrumentation experience.
Compatible with OpenTelemetry JS API and SDK 1.0+
.
Installation
npm install --save @opentelemetry/instrumentation-mysql
Supported Versions
Usage
OpenTelemetry MySQL Instrumentation allows the user to automatically collect trace data and export them to the backend of choice, to give observability to distributed systems when working with mysql.
To load a specific plugin (MySQL in this case), specify it in the registerInstrumentations's configuration
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { MySQLInstrumentation } = require('@opentelemetry/instrumentation-mysql');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
instrumentations: [
new MySQLInstrumentation(),
],
})
See examples for a short example.
MySQL instrumentation Options
Options | Type | Default | Description |
---|
enhancedDatabaseReporting | boolean | false | If true, an attribute containing the query's parameters will be attached the spans generated to represent the query |
| | | |
Useful links
License
Apache 2.0 - See LICENSE for more information.