otelx

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package otelx provides OpenTelemetry tracing and metrics setup and utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Enabled controls whether tracing is enabled.
	Enabled bool `mapstructure:"enabled"`

	// ServiceName is the name of the service for tracing.
	ServiceName string `mapstructure:"service_name"`

	// ServiceVersion is the version of the service.
	ServiceVersion string `mapstructure:"service_version"`

	// Environment is the deployment environment (development, staging, production).
	Environment string `mapstructure:"environment"`

	// Exporter specifies the exporter type: "otlp", "stdout", or "noop".
	Exporter string `mapstructure:"exporter"`

	// OTLP contains OTLP exporter configuration.
	OTLP OTLPConfig `mapstructure:"otlp"`

	// SampleRate is the sampling rate (0.0 to 1.0). 1.0 means sample all traces.
	SampleRate float64 `mapstructure:"sample_rate"`
}

Config holds OpenTelemetry configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default configuration for development.

type OTLPConfig

type OTLPConfig struct {
	// Endpoint is the OTLP collector endpoint (e.g., "localhost:4318").
	Endpoint string `mapstructure:"endpoint"`

	// Insecure disables TLS for the connection.
	Insecure bool `mapstructure:"insecure"`

	// Protocol is the transport protocol: "http" or "grpc".
	Protocol string `mapstructure:"protocol"`
}

OTLPConfig holds OTLP exporter configuration.

type Provider added in v1.11.0

type Provider struct {
	// contains filtered or unexported fields
}

Provider wraps the OpenTelemetry tracer, meter, and logger providers with shutdown capability.

func Setup

func Setup(ctx context.Context, cfg Config) (*Provider, error)

Setup initializes OpenTelemetry tracing and metrics based on the provided configuration. Returns a Provider that should be shut down when the application exits.

func (*Provider) Shutdown added in v1.11.0

func (p *Provider) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down all providers.

func (*Provider) SlogHandler added in v1.12.0

func (p *Provider) SlogHandler() slog.Handler

SlogHandler returns an slog.Handler that sends logs via OTel. Use this to create a fan-out handler alongside the existing stdout handler.

Jump to

Keyboard shortcuts

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