Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@opentelemetry/instrumentation-express

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-express

OpenTelemetry express automatic instrumentation package.

  • 0.34.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2M
decreased by-53.81%
Maintainers
3
Weekly downloads
 
Created

What is @opentelemetry/instrumentation-express?

@opentelemetry/instrumentation-express is a package that provides automatic instrumentation for Express.js applications. It allows developers to collect and export telemetry data such as traces and metrics, which can be used for monitoring and performance analysis.

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

Automatic Tracing

This code sets up automatic tracing for an Express.js application. It initializes a tracer provider, adds a span processor to export spans to the console, and registers the Express instrumentation.

const { NodeTracerProvider } = require('@opentelemetry/node');
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { ConsoleSpanExporter } = require('@opentelemetry/tracing');
const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');

const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();

const expressInstrumentation = new ExpressInstrumentation();
expressInstrumentation.setTracerProvider(provider);

Custom Span Attributes

This code demonstrates how to add custom attributes to spans in an Express.js application. It retrieves the current active span and sets a custom attribute.

const express = require('express');
const { context, trace } = require('@opentelemetry/api');

const app = express();

app.use((req, res, next) => {
  const span = trace.getSpan(context.active());
  if (span) {
    span.setAttribute('custom-attribute', 'custom-value');
  }
  next();
});

Error Handling

This code shows how to record exceptions in spans for error handling in an Express.js application. It captures the current active span and records the exception.

const express = require('express');
const { context, trace } = require('@opentelemetry/api');

const app = express();

app.use((err, req, res, next) => {
  const span = trace.getSpan(context.active());
  if (span) {
    span.recordException(err);
  }
  res.status(500).send('Internal Server Error');
});

Other packages similar to @opentelemetry/instrumentation-express

Keywords

FAQs

Package last updated on 04 Jan 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc