OpenTelemetry JavaScript Contributions

repository·main·Indexed 21 days ago

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

A repository for community-maintained OpenTelemetry JavaScript contributions, including instrumentations, metapackages, propagators, and resource detectors that extend the core OpenTelemetry JS SDK. It provides examples and support for instrumenting various frameworks and libraries such as Express, Koa, GraphQL, MongoDB, MySQL, Bunyan, and React/Preact.

Tokens
154.5K
Snippets
464
Records
702
Agent score
73%

What's inside opentelemetry-js-contrib

  1. Overview of Redis Instrumentation Example

    main

    This example demonstrates how to use OpenTelemetry Redis Instrumentation to automatically collect trace data from Redis cache calls within an Express API. It is designed to showcase distributed tracing capabilities, including:

    • Root Spans (on the Client)
    • Child Spans (on the Client)
    • Child Spans from a Remote Parent (on the Server)
    • SpanContext Propagation (passing context from Client to Server)
    • Span Events and Span Attributes
  2. Overview of OpenTelemetry MySQL Instrumentation

    main

    OpenTelemetry MySQL Instrumentation provides automatic collection of trace data and metrics for applications using the mysql npm module. It enables observability for distributed systems by capturing interactions with a MySQL backend and exporting them to backends like Zipkin or Grafana.

    The instrumentation supports various connection methods:

    • Direct Connection Query
    • Pool Connection Query
    • Cluster Pool Connection Query
  3. Overview of OpenTelemetry JavaScript Contrib

    main

    opentelemetry-js-contrib is a repository for community-maintained OpenTelemetry JavaScript contributions that are not part of the core API or SDK. It provides additional capabilities to extend OpenTelemetry functionality in JavaScript environments.

    Key types of components included in this repository:

    • Instrumentations (instrumentation-*): Enable automatic collection of tracing data for various libraries and frameworks.
    • Metapackages (auto-*): Bundled packages (often for Node.js or Web) that simplify the installation of multiple instrumentations.
    • Context Propagators (propagator-*): Tools for propagating context across service boundaries.
    • Resource Detectors (resource-detector-*): Tools to automatically collect attributes about the entity producing telemetry (e.g., Kubernetes Pod names, namespaces, or Deployment names).

    For core API and SDK details, refer to the official OpenTelemetry JS documentation.

  4. Instrument GraphQL servers with @opentelemetry/instrumentation-graphql

    main

    The @opentelemetry/instrumentation-graphql package provides instrumentation for Node.js applications using GraphQL. Because it instruments the GraphQL core directly, it is compatible with various GraphQL server implementations, including:

    • Apollo GraphQL (apollo-server)
    • GraphQL HTTP Server Middleware (express-graphql)

    This instrumentation allows you to capture spans for GraphQL operations. In the provided example, trace data is exported using the @opentelemetry/exporter-trace-otlp-http exporter.

  5. Available AWS Resource Detectors

    main

    The @opentelemetry/resource-detector-aws package provides several detectors to automatically populate OpenTelemetry resource attributes based on the AWS environment where your process is running. It uses @opentelemetry/semantic-conventions version 1.22+ (Semantic Convention Version 1.7.0).

    Supported detectors include:

    • AWS Beanstalk Detector: For processes on AWS Elastic Beanstalk.
    • AWS EC2 Detector: For processes on Amazon EC2 (including ECS on EC2, but not Fargate).
    • AWS ECS Detector: For containers running on Amazon ECS.
    • AWS EKS Detector: For containers running on Amazon EKS.
    • AWS Lambda Detector: For functions running on AWS Lambda.
  6. How Bunyan instrumentation works

    main

    The @opentelemetry/instrumentation-bunyan module provides two primary capabilities:

    1. Log sending: Automatically adds a Bunyan stream to your logger that sends log records to the OpenTelemetry Logs SDK. If no Logger provider is configured in the OpenTelemetry SDK, this stream becomes a no-op.
    2. Log correlation: Automatically injects trace-context into Bunyan log records when they are emitted within an active tracing span. This allows you to link logs to specific traces.

    Supported Bunyan versions: >=1.0.0 <2

    const { trace } = require('@opentelemetry/api');
    const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/sdk-trace');
    const { SimpleLogRecordProcessor, ConsoleLogRecordExporter } = require('@opentelemetry/sdk-logs');
    const { NodeSDK } = require('@opentelemetry/sdk-node');
    const { BunyanInstrumentation } = require('@opentelemetry/instrumentation-bunyan');
    
    const sdk = new NodeSDK({
      spanProcessors: [
        new SimpleSpanProcessor(new ConsoleSpanExporter()),
      ],
      logRecordProcessors: [
        new SimpleLogRecordProcessor({ exporter: new ConsoleLogRecordExporter() }),
      ],
      instrumentations: [
        new BunyanInstrumentation({}),
      ]
    });
    sdk.start();
    
    const bunyan = require('bunyan');
    const logger = bunyan.createLogger({name: 'example'});
    
    // Log sending in action
    logger.info('hi');
    
    // Log correlation in action
    const tracer = trace.getTracer('example');
    tracer.startActiveSpan('manual-span', span => {
      logger.info('in a span');
      // Log record will include trace_id, span_id, and trace_flags
    });
  7. Use the BaggageSpanProcessor to add baggage to spans

    main

    The BaggageSpanProcessor reads entries stored in Baggage from the parent context and adds those keys and values as attributes to the span when it starts.

    Important Security Warning: Do not put sensitive information in Baggage. Baggage entries are propagated via outgoing HTTP headers, meaning any data added to Baggage will be visible in those headers and sent to external services.

  8. Understand the AWS X-Ray header format

    main

    The AWSXRayPropagator translates OpenTelemetry SpanContext into the AWS X-Amzn-Trace-Id header format.

    Note: TraceState is currently not propagated.

    Header Structure

    An example header looks like this: X-Amzn-Trace-Id: Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1

    The header consists of three parts:

    1. Root: The AWS X-Ray format trace ID. It follows the format (spec-version)-(timestamp)-(UUID).
      • spec_version: Currently only 1 is valid.
      • timestamp: A 32-bit number in base16 format (corresponds to the first 8 characters of the OpenTelemetry trace ID).
      • UUID: A 96-bit random number in base16 format (corresponds to the last 10 characters of the OpenTelemetry trace ID).
    2. Parent: The ID of the AWS X-Ray Segment. This is a 64-bit random number in base16 format, populated from the OpenTelemetry Span ID.
    3. Sampled: The sampling decision. This is populated from OpenTelemetry trace flags. Valid values used by this propagator are 0 and 1. (If the value is ?, a new trace will be started).