OpenTelemetry JS

repository·main·Indexed 11 days ago

https://github.com/open-telemetry/opentelemetry-js

The JavaScript implementation of the OpenTelemetry framework for collecting traces, metrics, and logs from Node.js and browser environments. It provides the @opentelemetry/api for interacting with the framework and the @opentelemetry/sdk-trace for processing and exporting telemetry data.

Tokens
155K
Snippets
510
Records
683
Agent score
80%

What's inside OpenTelemetry JS

  1. Overview of OpenTelemetry JS packages

    main

    OpenTelemetry JS is organized into several functional categories to support tracing, metrics, and interoperability across server and browser environments.

    API Packages

    Used to define the interfaces and models without providing the underlying implementation. Ideal for library authors to instrument their code without forcing a specific SDK on the end-user.

    • @opentelemetry/api: Provides TypeScript interfaces, enums, and no-op implementations for the core trace and metrics model. Works in both server and browser.
    • @opentelemetry/core: Provides default and no-op implementations of the OpenTelemetry API for trace and metrics.

    Implementation / SDKs

    Used to actually collect and process telemetry data.

    • @opentelemetry/sdk-trace: Provides full control over instrumentation and span creation. Note: It does not load async_hooks or any instrumentation by default.
    • @opentelemetry/sdk-metrics: Provides instruments and meters for reporting time series data.

    Shims

    • @opentelemetry/shim-opentracing: Allows existing OpenTracing instrumentation to report to OpenTelemetry.
  2. Trace HTTPS requests from client to server

    main

    This example demonstrates how to use OpenTelemetry HTTPS instrumentation to automatically collect trace data and export it to a backend like Zipkin or Jaeger. It covers key tracing concepts including:

    • Root Span: The initial span created on the client.
    • Child Span: Spans created within the client's execution flow.
    • Remote Parent Spans: Spans on the server that are linked to the client's context.
    • SpanContext Propagation: The mechanism that carries trace information from the client to the server.
    • Span Events and Attributes: Adding metadata and lifecycle events to spans.
  3. Explore OpenTelemetry JavaScript Examples

    main

    The examples/ directory provides real-world applications demonstrating how to use OpenTelemetry JavaScript. These examples range from beginner-level introductory instrumentation to intermediate-level specialized instrumentation (HTTP, gRPC, OTLP).

    Maintained Examples are updated with every release to ensure they follow the latest best practices and features. Use these as a reference for implementing tracing, metrics, and specific instrumentations in your own projects.

  4. Supported Runtimes and Language Features

    main

    OpenTelemetry JS supports various runtimes with the following considerations:

    Node.js

    Only Node.js Active or Maintenance LTS versions are supported. Previous versions may work but are not tested or guaranteed.

    Web Browsers

    Client instrumentation for the browser is currently experimental and unspecified.

    OpenTelemetry sets minimum support based on language features. The current minimum is ECMAScript 2022. If you target environments that do not support ES2022, you must transpile the code and provide necessary polyfills. Support issues for environments lacking ES2022 support will be closed as "won't fix."

    TypeScript

    OpenTelemetry JS is built with TypeScript v5.2.2. It is recommended to use version v5.2.2 or higher for your projects. Support for older TypeScript versions follows the DefinitelyTyped support policy (2-year window).

  5. Use async_hooks-based Context Managers in Node.js

    main

    The @opentelemetry/context-async-hooks package provides ContextManager implementations specifically for Node.js environments using the async_hooks module. This allows OpenTelemetry to propagate context (like trace IDs) across asynchronous operations.

    Important Environment Note:

    • For Browser environments, do not use this package. Instead, use opentelemetry-context-zone or opentelemetry-context-zone-peer-dep.
  6. What is Propagation and why is it used?

    main

    Propagation is the process of sending span context fields—such as traceId, spanId, traceFlags, and baggage—to downstream services. This ensures that spans created by downstream services can be properly associated with the current span, maintaining a continuous trace across distributed systems.

    Propagation is typically achieved using HTTP headers or RPC metadata. Common well-known formats supported by OpenTelemetry include:

    • W3C Trace Context: Supported via W3CTraceContextPropagator.
    • B3: Supported via B3Propagator.
    • Jaeger: Supported via JaegerPropagator.
  7. What is an OpenTelemetry Resource?

    main

    An OpenTelemetry Resource is an immutable representation of the entity producing telemetry. It contains attributes that describe the source of the data, such as a process running in a Kubernetes container, including details like Pod name, namespace, or Deployment name.

    Standard attributes for resources are defined in the @opentelemetry/semantic-conventions package.

  8. How OpenTelemetry API handles version compatibility

    main

    The OpenTelemetry API uses a variable on the global object to store the global API. This allows it to handle scenarios where multiple versions of the API might exist in a single node_modules tree due to dependency resolution.

    When an API method is called, it checks for a compatible version on the global object. If the version used by a dependency is incompatible with the version used by the end user, the package will receive a no-op implementation instead of failing.