opentelemetry-go-extra

repository·main·Indexed 18 days ago

https://github.com/uptrace/opentelemetry-go-extra

A collection of OpenTelemetry instrumentations for Go (version 0.3.2) providing observability—including metrics, traces, and logs—for popular libraries such as GORM, sqlx, logrus, Beego, Echo, Gin, gocql, gomemcache, Gorilla Mux, gRPC, and the MongoDB driver.

Tokens
19.8K
Snippets
90
Records
104
Agent score
59%

What's inside opentelemetry-go-extra

  1. Available OpenTelemetry instrumentations for Go

    main

    This repository provides a collection of OpenTelemetry instrumentations for various Go libraries and frameworks. The instrumentations support Metrics, Traces, and Logs depending on the specific package.

    Key instrumentations include:

    • Databases/SQL: database/sql, GORM 1, GORM 2, sqlx, and sqlboiler (all support Metrics and Traces).
    • Logging: logrus (Traces) and otelzap (Logs).
    • APIs/Frameworks: graphql-go (Traces).

    You can find more instrumentations via the Uptrace instrument registry or the OpenTelemetry registry.

  2. How otellogrus instrumentation works

    main

    The otellogrus instrumentation records logrus log messages as events on an existing OpenTelemetry span.

    Important Requirements:

    • The log message is only recorded if the context.Context passed to the log call contains an active span.
    • You must use logrus.WithContext(ctx) to ensure the span is propagated to the hook.
  3. How otelgraphql implements the graphql-go tracer interface

    main

    The otelgraphql package provides an implementation of the graphql-go tracer interface. It records GraphQL operations using the OpenTelemetry API by implementing the following methods (all accepting context.Context):

    • TraceValidation: Traces the schema validation step that occurs before the operation.
    • TraceQuery: Traces the entire operation (query or mutation) as a single span.
    • TraceField: Traces specific field operations. These spans are typically intended to be sub-spans of the TraceQuery span.

    Additionally, because graphql-go resolver methods receive context.Context, you can manually create field-specific sub-spans within your resolvers if the standard TraceField instrumentation does not meet your needs.

  4. Run the otellogrus instrumentation example with different exporters

    main

    The otellogrus example demonstrates how to use OpenTelemetry instrumentation with the Logrus logger. You can switch between different exporters by setting specific environment variables before running the application.

    Stdout exporter (Default)

    To output traces to the standard output, simply run:

    go run .

    Jaeger exporter

    To send traces to a Jaeger collector, set the OTEL_EXPORTER_JAEGER_ENDPOINT environment variable:

    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run .

    Uptrace exporter

    To send traces to Uptrace, provide your Data Source Name (DSN) via the UPTRACE_DSN environment variable:

    UPTRACE_DSN="https://<token>@uptrace.dev/<project_id>" go run .
    # Stdout exporter (default):
    go run .
    
    # Jaeger exporter:
    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run .
    
    # Uptrace exporter:
    UPTRACE_DSN="https://<token>@uptrace.dev/<project_id>" go run .
  5. Run the gRPC OpenTelemetry instrumentation example

    main

    The gRPC instrumentation example can be executed using different OpenTelemetry exporters by setting specific environment variables. By default, the example uses the Stdout exporter.

    Using the Stdout exporter (Default)

    To run the server with default settings:

    go run .

    Using the Jaeger exporter

    To export traces to a Jaeger endpoint, set the OTEL_EXPORTER_JAEGER_ENDPOINT environment variable.

    Start the server:

    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run server/server.go

    Start the client:

    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run client/client.go

    Using the Uptrace exporter

    To export traces to Uptrace, set the UPTRACE_DSN environment variable with your DSN (Data Source Name).

    Start the server:

    UPTRACE_DSN="https://<token>@uptrace.dev/<project_id>" go run server/server.go

    Start the client:

    UPTRACE_DSN="https://<token>@uptrace.dev/<project_id>" go run client/client.go
    # Example: Running the server with Jaeger
    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run server/server.go
  6. Run the net/http OpenTelemetry example with different exporters

    main

    The net/http example can be executed using different OpenTelemetry exporters by setting specific environment variables. By default, it uses the Stdout exporter.

    To use other backends, set the following environment variables before running the application:

    • Jaeger: Set OTEL_EXPORTER_JAEGER_ENDPOINT to your Jaeger collector endpoint.
    • Uptrace: Set UPTRACE_DSN with your Uptrace Data Source Name (DSN) containing your token and project ID.
    # Stdout exporter (default)
    go run .
    
    # Jaeger exporter
    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run .
    
    # Uptrace exporter
    UPTRACE_DSN="https://<token>@uptrace.dev/<project_id>" go run .
  7. Run the otelzap instrumentation example with different exporters

    main

    The otelzap instrumentation example can be executed using different OpenTelemetry exporters by setting specific environment variables. By default, the example uses the Stdout exporter.

    To use the Jaeger exporter, set the OTEL_EXPORTER_JAEGER_ENDPOINT environment variable.

    To use the Uptrace exporter, set the UPTRACE_DSN environment variable with your project credentials.

    # Stdout exporter (default)
    go run .
    
    # Jaeger exporter
    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run .
    
    # Uptrace exporter
    UPTRACE_DSN="https://<token>@uptrace.dev/<project_id>" go run .
  8. Run the Prometheus exporter example

    main

    This example demonstrates how to send OpenTelemetry metrics to Prometheus. It uses Docker Compose to orchestrate the services required to view and scrape metrics.

    To run the example, execute:

    docker-compose up -d

    Once running, two services are available:

    • Metrics Target: https://localhost:8088/metrics (The endpoint that exports OpenTelemetry metrics).
    • Prometheus UI: http://localhost:9090/graph (The Prometheus instance configured to scrape the metrics target).

    You can verify the metrics by querying the counter test_my_counter in the Prometheus graph interface.

  9. Use otelzap to record logs as span events

    main

    otelzap records Zap log messages as events on an existing OpenTelemetry span. To work, you must pass a context.Context containing an active span as the first argument. If the context does not contain a span, nothing is recorded.

    You can use either the .Ctx(ctx) pattern or the .ErrorContext(ctx, ...) pattern. Both are designed to be fast and allocation-free.

    import (
        "go.uber.org/zap"
        "github.com/uptrace/opentelemetry-go-extra/otelzap"
    )
    
    // Wrap zap logger to extend Zap with API that accepts a context.Context.
    log := otelzap.New(zap.NewExample())
    
    // Option 1: Using .Ctx(ctx)
    log.Ctx(ctx).Error("hello from zap",
        zap.Error(errors.New("hello world")),
        zap.String("foo", "bar"))
    
    // Option 2: Using .ErrorContext(ctx, ...)
    log.ErrorContext(ctx, "hello from zap",
        zap.Error(errors.New("hello world")),
        zap.String("foo", "bar"))
  10. Run the Gomemcache OpenTelemetry example

    main

    The Gomemcache example demonstrates OpenTelemetry instrumentation for the gomemcache client. To run the example, you must have a running Memcached server (e.g., via Docker) and can choose between different exporters using environment variables.

    Prerequisites

    Start a Memcached server using Docker Compose:

    docker-compose up -d

    Running with different exporters

    1. Stdout exporter (Default) Logs traces directly to standard output:

    go run .

    2. Jaeger exporter Sends traces to a Jaeger endpoint:

    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run .

    3. Uptrace exporter Sends traces to Uptrace using a Data Source Name (DSN):

    UPTRACE_DSN="https://<token>@uptrace.dev/<project_id>" go run .
    # Example: Running with Jaeger exporter
    OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces go run .