🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@opentelemetry/instrumentation-mysql2

Package Overview
Dependencies
Maintainers
3
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opentelemetry/instrumentation-mysql2

OpenTelemetry instrumentation for `mysql2` database client for MySQL

0.48.0
latest
npm
Version published
Weekly downloads
4.7M
1.15%
Maintainers
3
Weekly downloads
 
Created

What is @opentelemetry/instrumentation-mysql2?

@opentelemetry/instrumentation-mysql2 is an npm package that provides automatic instrumentation for the mysql2 library using OpenTelemetry. This allows developers to collect and export telemetry data such as traces and metrics from their MySQL2 database operations, which can be used for monitoring and performance analysis.

What are @opentelemetry/instrumentation-mysql2's main functionalities?

Automatic Tracing

This feature allows you to automatically trace MySQL2 queries. The code sample demonstrates how to set up the OpenTelemetry NodeTracerProvider and register the MySQL2 instrumentation. Once set up, any MySQL2 queries executed will be automatically traced.

const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { MySQL2Instrumentation } = require('@opentelemetry/instrumentation-mysql2');

const provider = new NodeTracerProvider();
provider.register();

registerInstrumentations({
  instrumentations: [
    new MySQL2Instrumentation(),
  ],
});

const mysql = require('mysql2');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

connection.query('SELECT 1', (err, results) => {
  if (err) throw err;
  console.log(results);
  connection.end();
});

Custom Span Attributes

This feature allows you to add custom attributes to spans. The code sample shows how to use the `responseHook` option to add the number of rows returned by a MySQL2 query as a custom attribute to the span.

const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { MySQL2Instrumentation } = require('@opentelemetry/instrumentation-mysql2');

const provider = new NodeTracerProvider();
provider.register();

registerInstrumentations({
  instrumentations: [
    new MySQL2Instrumentation({
      responseHook: (span, response) => {
        span.setAttribute('mysql2.rows', response.length);
      }
    }),
  ],
});

const mysql = require('mysql2');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

connection.query('SELECT 1', (err, results) => {
  if (err) throw err;
  console.log(results);
  connection.end();
});

Other packages similar to @opentelemetry/instrumentation-mysql2

Keywords

instrumentation

FAQs

Package last updated on 02 Jun 2025

Did you know?

Socket

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.

Install

Related posts