telemetry

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package telemetry provides OpenTelemetry integration for distributed tracing.

Package telemetry provides OpenTelemetry integration for distributed tracing.

This package initializes OpenTelemetry with configuration loaded from standard environment variables. It sets up a global TracerProvider that can be used throughout the application via otel.Tracer().

Environment Variables:

OTEL_ENABLED                    - Enable/disable tracing (default: false)
OTEL_SERVICE_NAME               - Service name (default: perf-analyzer)
OTEL_SERVICE_VERSION            - Service version (default: unknown)
OTEL_EXPORTER_OTLP_ENDPOINT     - OTLP collector endpoint
OTEL_EXPORTER_OTLP_PROTOCOL     - Protocol: grpc or http/protobuf (default: grpc)
OTEL_EXPORTER_OTLP_HEADERS      - Headers for authentication (e.g., Authorization=Bearer xxx)
OTEL_EXPORTER_OTLP_INSECURE     - Use insecure connection (default: false)
OTEL_TRACES_SAMPLER             - Sampler type (default: always_on)
OTEL_TRACES_SAMPLER_ARG         - Sampler argument (e.g., ratio)
OTEL_RESOURCE_ATTRIBUTES        - Additional resource attributes

Usage:

func main() {
    ctx := context.Background()

    // Initialize OpenTelemetry
    shutdown, err := telemetry.Init(ctx)
    if err != nil {
        log.Printf("Failed to initialize telemetry: %v", err)
    }
    defer shutdown(ctx)

    // Use global tracer in your code
    ctx, span := otel.Tracer("my-service").Start(ctx, "operation")
    defer span.End()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enabled

func Enabled() bool

Enabled returns whether OpenTelemetry tracing is enabled.

Types

type Config

type Config struct {
	// Enabled indicates whether OpenTelemetry tracing is enabled.
	// Loaded from OTEL_ENABLED environment variable.
	Enabled bool

	// ServiceName is the name of the service.
	// Loaded from OTEL_SERVICE_NAME, defaults to "perf-analyzer".
	ServiceName string

	// ServiceVersion is the version of the service.
	// Loaded from OTEL_SERVICE_VERSION, defaults to "unknown".
	ServiceVersion string

	// Endpoint is the OTLP collector endpoint.
	// Loaded from OTEL_EXPORTER_OTLP_ENDPOINT.
	Endpoint string

	// Protocol is the OTLP protocol (grpc or http/protobuf).
	// Loaded from OTEL_EXPORTER_OTLP_PROTOCOL, defaults to "grpc".
	Protocol string

	// Headers contains custom headers for OTLP exporter (e.g., Authorization).
	// Loaded from OTEL_EXPORTER_OTLP_HEADERS.
	// Format: "key1=value1,key2=value2"
	Headers map[string]string

	// Insecure indicates whether to use insecure connection.
	// Loaded from OTEL_EXPORTER_OTLP_INSECURE.
	Insecure bool

	// Sampler is the sampler type.
	// Loaded from OTEL_TRACES_SAMPLER.
	// Supported values: always_on, always_off, traceidratio,
	// parentbased_always_on, parentbased_always_off, parentbased_traceidratio.
	// Defaults to always_on (full sampling).
	Sampler string

	// SamplerArg is the sampler argument (e.g., ratio for traceidratio).
	// Loaded from OTEL_TRACES_SAMPLER_ARG.
	SamplerArg string

	// ResourceAttrs contains additional resource attributes.
	// Loaded from OTEL_RESOURCE_ATTRIBUTES.
	// Format: "key1=value1,key2=value2"
	ResourceAttrs map[string]string
}

Config holds OpenTelemetry configuration loaded from environment variables.

func GetConfig

func GetConfig() *Config

GetConfig returns the current telemetry configuration.

func LoadFromEnv

func LoadFromEnv() *Config

LoadFromEnv loads configuration from environment variables.

type ShutdownFunc

type ShutdownFunc func(ctx context.Context) error

ShutdownFunc is a function that shuts down the TracerProvider.

func Init

func Init(ctx context.Context) (ShutdownFunc, error)

Init initializes OpenTelemetry and sets up the global TracerProvider. If OTEL_ENABLED is not "true", it returns a no-op shutdown function and the global TracerProvider remains as the default no-op provider.

The function is safe to call multiple times, but only the first call will initialize the TracerProvider.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL