You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@opentelemetry/instrumentation-pg

Package Overview
Dependencies
Maintainers
3
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opentelemetry/instrumentation-pg

OpenTelemetry instrumentation for `pg` and `pg-pool` database client for PostgreSQL

0.52.0
latest
Source
npm
Version published
Weekly downloads
4.5M
0.92%
Maintainers
3
Weekly downloads
 
Created

What is @opentelemetry/instrumentation-pg?

@opentelemetry/instrumentation-pg is an npm package that provides automatic instrumentation for the 'pg' (node-postgres) module, enabling the collection of telemetry data such as traces and metrics for PostgreSQL database operations. This helps in monitoring and debugging database interactions in applications.

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

Automatic Tracing

This feature allows automatic tracing of PostgreSQL operations. The code sample demonstrates how to set up the NodeTracerProvider and register the PgInstrumentation to start collecting trace data.

const { NodeTracerProvider } = require('@opentelemetry/node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');

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

registerInstrumentations({
  instrumentations: [
    new PgInstrumentation(),
  ],
  tracerProvider: provider,
});

Custom Span Attributes

This feature allows adding custom attributes to spans. The code sample shows how to configure the PgInstrumentation to include additional information such as the number of rows returned in the response.

const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');

const pgInstrumentation = new PgInstrumentation({
  enhancedDatabaseReporting: true,
  responseHook: (span, responseInfo) => {
    span.setAttribute('db.response.rows', responseInfo.rowCount);
  },
});

Error Handling

This feature allows capturing and reporting errors in database operations. The code sample demonstrates how to set the span status to error if an error occurs during the database operation.

const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');

const pgInstrumentation = new PgInstrumentation({
  responseHook: (span, responseInfo) => {
    if (responseInfo.error) {
      span.setStatus({ code: 2, message: responseInfo.error.message });
    }
  },
});

Other packages similar to @opentelemetry/instrumentation-pg

Keywords

instrumentation

FAQs

Package last updated on 18 Mar 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