otelprovider

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: Apache-2.0 Imports: 18 Imported by: 3

README

OTelProvider

An opinionated helper library for easily setting up OpenTelemetry log and trace providers in Go applications.

Overview

OTelProvider simplifies the process of configuring OpenTelemetry (OTel) in your Go applications by providing ready-to-use functions for setting up tracing and logging with sensible defaults. This library helps reduce boilerplate code and ensures consistent telemetry configuration across your services.

Features

  • Simple API for initializing OpenTelemetry trace providers
  • Structured logging with OpenTelemetry integration
  • Resource detection for common environment information
  • Support for multiple exporters (OTLP, Console)
  • Environment-based configuration

Installation

go get github.com/spechtlabs/go-otel-utils/otelprovider

Usage

Basic Setup
package main

import (
    "context"
    "log"
    
    "github.com/spechtlabs/go-otel-utils/otelprovider"
)

func main() {
    ctx := context.Background()
    
    // Initialize the trace provider
    tp, err := otelprovider.NewTraceProvider(ctx, "my-service")
    if err != nil {
        log.Fatalf("Failed to initialize trace provider: %v", err)
    }
    defer tp.Shutdown(ctx)
    
    // Initialize the logger
    logger, err := otelprovider.NewLogger("my-service", "development")
    if err != nil {
        log.Fatalf("Failed to initialize logger: %v", err)
    }
    
    // Your application code here
    logger.Info("Application started successfully")
    
    // Create spans and add logs as needed
    ctx, span := tp.Tracer("component-name").Start(ctx, "operation-name")
    defer span.End()
    
    // ...
}
Complete Example

See the example/ directory for a complete working example of how to use the otelprovider in your application.

Configuration Options

The library offers various configuration options through environment variables:

  • OTEL_EXPORTER_OTLP_ENDPOINT: Endpoint for the OTLP exporter
  • OTEL_SERVICE_NAME: Default service name if not specified
  • OTEL_ENVIRONMENT: Environment (development, staging, production)
  • LOG_LEVEL: Logging level (debug, info, warn, error)

Opinionated Decisions

The otelprovider library makes several opinionated choices to simplify telemetry setup:

  1. Sensible Defaults: Provides reasonable default configurations that work out of the box
  2. Resource Detection: Automatically detects and attaches environment information to telemetry data
  3. Structured Logging: Uses structured logging for better searchability and context
  4. Correlation: Ensures logs are correlated with traces for better observability
  5. Environment-Aware: Configures exporters and sampling based on the runtime environment

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(opts ...LoggerOption) *log.LoggerProvider

func NewTracer

func NewTracer(opts ...TracerOption) *trace.TracerProvider

Types

type Logger

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

type LoggerOption

type LoggerOption func(t *Logger)

TracerOption applies a configuration to the given config.

func WithGrpcLogEndpoint

func WithGrpcLogEndpoint(otelGrpcEndpoint string) LoggerOption

func WithHttpLogEndpoint

func WithHttpLogEndpoint(otelHttpEndpoint string) LoggerOption

func WithLogAutomaticEnv

func WithLogAutomaticEnv() LoggerOption

func WithLogInsecure

func WithLogInsecure() LoggerOption

func WithLogResources

func WithLogResources(res *resource.Resource) LoggerOption

func WithoutRegisterLogProvider

func WithoutRegisterLogProvider() LoggerOption

type Tracer

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

type TracerOption

type TracerOption func(t *Tracer)

TracerOption applies a configuration to the given config.

func WithGrpcTraceEndpoint

func WithGrpcTraceEndpoint(otelGrpcEndpoint string) TracerOption

func WithHttpTraceEndpoint

func WithHttpTraceEndpoint(otelHttpEndpoint string) TracerOption

func WithTraceAutomaticEnv

func WithTraceAutomaticEnv() TracerOption

func WithTraceInsecure

func WithTraceInsecure() TracerOption

func WithTraceResources

func WithTraceResources(res *resource.Resource) TracerOption

func WithoutRegisterTraceProvider

func WithoutRegisterTraceProvider() TracerOption

Jump to

Keyboard shortcuts

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