🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@metis-data/pg-interceptor

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metis-data/pg-interceptor

Metis package for pg instrumentation

Source
npmnpm
Version
2.3.24
Version published
Weekly downloads
3
-62.5%
Maintainers
1
Weekly downloads
 
Created
Source

metis

Metis Pg Instrumentation

Using the Documentation of Metis.

Installation

$ npm i @metis-data/pg-interceptor --save

Setup

  • Create tracing file (tracer):
import opentelemetry from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { Resource } from '@opentelemetry/resources';
import {
  BatchSpanProcessor,
  ConsoleSpanExporter,
  SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { getMetisExporter, MetisHttpInstrumentation, MetisPgInstrumentation } from '@metis-data/pg-interceptor';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';

let tracerProvider;
const connectionString = process.env.PG_CONNECTION_STRING;

export const startMetisInstrumentation = () => {
  tracerProvider = new BasicTracerProvider({
    resource: new Resource({
      [SemanticResourceAttributes.SERVICE_NAME]: process.env.METIS_SERVICE_NAME,
      [SemanticResourceAttributes.SERVICE_VERSION]: 'service-version',
    }),
  });

  const metisExporter = getMetisExporter(process.env.METIS_API_KEY);

  tracerProvider.addSpanProcessor(new BatchSpanProcessor(metisExporter));
  
  if (process.env.OTEL_DEBUG) {
      tracerProvider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
  }

  const contextManager = new AsyncHooksContextManager();

  contextManager.enable();
  opentelemetry.context.setGlobalContextManager(contextManager);

  tracerProvider.register();

  // Urls regex to exclude from instrumentation
  const excludeUrls = [/favicon.ico/];
  registerInstrumentations({
    instrumentations: [
      new MetisPgInstrumentation({ connectionString }),
      new MetisHttpInstrumentation(excludeUrls)
    ],
  });
};

Environment:

  • PG_CONNECTION_STRING: your database connection details e.g 'postgres://user:password@host:port/database'
  • METIS_API_KEY: Metis Api Key generated at Metis
  • OTEL_DEBUG: Console Span Exporter to have the span logged to console - optional

Pass a valid connectionString to Metis Interceptor, can be set through <process.env.PG_CONNECTION_STRING>, This will generate pg pool to collect queries plans.

:warning: Note: If for any reason connectionString is not available during the tracer setup, there is an option to set it later as follows:

import { setPgConnection } from '@metis-data/pg-interceptor';

// When the connectionString available
setPgConnection(connectionString);

Until connection string is passed, instrumentation will work but won't collect the queries plans.

For more details about tracing setup and components, visit our docs

  • Import the tracing in your main app file:
import { startMetisInstrumentation } from './tracer';
startMetisInstrumentation();

// imports and bootstrap

FAQs

Package last updated on 26 Mar 2023

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