New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@opentelemetry/instrumentation-fetch

Package Overview
Dependencies
Maintainers
3
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opentelemetry/instrumentation-fetch

OpenTelemetry instrumentation for fetch http client in web browsers

  • 0.57.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
470K
decreased by-2.98%
Maintainers
3
Weekly downloads
 
Created

What is @opentelemetry/instrumentation-fetch?

@opentelemetry/instrumentation-fetch is an npm package that provides automatic instrumentation for the Fetch API, allowing developers to collect telemetry data such as traces and metrics for HTTP requests made using the Fetch API. This is useful for monitoring and debugging web applications.

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

Automatic Tracing

This feature allows you to automatically trace HTTP requests made using the Fetch API. The code sample demonstrates how to set up the FetchInstrumentation and enable it to start collecting trace data.

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

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

const fetchInstrumentation = new FetchInstrumentation();
fetchInstrumentation.enable();

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log(json));

Custom Attributes

This feature allows you to add custom attributes to the spans created for Fetch API requests. The code sample shows how to set a custom attribute on each span.

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

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

const fetchInstrumentation = new FetchInstrumentation({
  applyCustomAttributesOnSpan: (span, request, response) => {
    span.setAttribute('custom-attribute', 'custom-value');
  }
});
fetchInstrumentation.enable();

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log(json));

Error Handling

This feature allows you to handle errors in Fetch API requests and optionally ignore certain URLs or HTTP methods from being instrumented. The code sample demonstrates how to set up error handling and ignore specific URLs and methods.

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

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

const fetchInstrumentation = new FetchInstrumentation({
  ignoreUrls: [/example\.com/],
  ignoreMethods: ['POST']
});
fetchInstrumentation.enable();

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(error => console.error('Fetch error:', error));

Other packages similar to @opentelemetry/instrumentation-fetch

Keywords

FAQs

Package last updated on 13 Feb 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

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