![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@opentelemetry/instrumentation-mysql
Advanced tools
OpenTelemetry instrumentation for `mysql` database client for 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.
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 });
}
},
});
@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 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 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.
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+
.
npm install --save @opentelemetry/instrumentation-mysql
mysql
versions >=2.0.0 <3
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/mysql for a short example.
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 |
This package uses @opentelemetry/semantic-conventions
version 1.22+
, which implements Semantic Convention Version 1.7.0
Attributes collected:
Attribute | Short Description |
---|---|
db.connection_string | The connection string used to connect to the database. |
db.name | This attribute is used to report the name of the database being accessed. |
db.operation | The name of the operation being executed. |
db.statement | The database statement being executed. |
db.system | An identifier for the database management system (DBMS) product being used. |
db.user | Username for accessing the database. |
net.peer.name | Remote hostname or similar. |
net.peer.port | Remote port number. |
Apache 2.0 - See LICENSE for more information.
FAQs
OpenTelemetry instrumentation for `mysql` database client for MySQL
The npm package @opentelemetry/instrumentation-mysql receives a total of 2,904,747 weekly downloads. As such, @opentelemetry/instrumentation-mysql popularity was classified as popular.
We found that @opentelemetry/instrumentation-mysql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.