@opentelemetry/instrumentation-fetch
TypeScript icon, indicating that this package has built-in type declarations

0.52.0 • Public • Published

OpenTelemetry Fetch Instrumentation for web

NPM Published Version Apache License

Note: This is an experimental package under active development. New releases may include breaking changes.

This module provides auto instrumentation for web using fetch. (Note: This instrumentation does not instrument Node.js' fetch. See @opentelemetry/instrumentation-undici for that.)

Installation

npm install --save @opentelemetry/instrumentation-fetch

Usage

import {
  ConsoleSpanExporter,
  SimpleSpanProcessor,
  WebTracerProvider,
} from '@opentelemetry/sdk-trace-web';
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
import { ZoneContextManager } from '@opentelemetry/context-zone';
import { registerInstrumentations } from '@opentelemetry/instrumentation';

const provider = new WebTracerProvider();

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

provider.register({
  contextManager: new ZoneContextManager(),
});

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

// or plugin can be also initialised separately and then set the tracer provider or meter provider
const fetchInstrumentation = new FetchInstrumentation();
const provider = new WebTracerProvider();
provider.register({
  contextManager: new ZoneContextManager(),
});
fetchInstrumentation.setTracerProvider(provider);

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));


// and some test

fetch('http://localhost:8090/fetch.js');

Example Screenshots

Screenshot of the running example Screenshot of the running example Screenshot of the running example

See examples/tracer-web/fetch for a short example.

Fetch Instrumentation options

Fetch instrumentation plugin has few options available to choose from. You can set the following:

Options Type Description
applyCustomAttributesOnSpan HttpCustomAttributeFunction Function for adding custom attributes
ignoreNetworkEvents boolean Disable network events being added as span events (network events are added by default)

Useful links

License

Apache 2.0 - See LICENSE for more information.

Package Sidebar

Install

npm i @opentelemetry/instrumentation-fetch

Weekly Downloads

157,683

Version

0.52.0

License

Apache-2.0

Unpacked Size

179 kB

Total Files

48

Last publish

Collaborators

  • pichlermarc
  • bogdandrutu
  • dyladan