Documentation
ΒΆ
Overview ΒΆ
Package otelkit provides a simplified, opinionated wrapper around OpenTelemetry tracing for Go applications.
This package offers easy-to-use APIs for creating and managing distributed traces while hiding the complexity of the underlying OpenTelemetry SDK. It provides zero-configuration setup with sensible defaults, while still allowing full customization when needed.
Quick Start (Recommended) ΒΆ
For most use cases, start with SetupTracing:
ctx := context.Background()
shutdown, err := otelkit.SetupTracing(ctx, "my-service")
if err != nil {
log.Fatal(err)
}
defer shutdown(ctx)
tracer := otelkit.New("my-service")
ctx, span := tracer.Start(ctx, "operation-name")
defer span.End()
Advanced Configuration ΒΆ
For custom configuration, use NewProviderConfig with NewProvider:
config := otelkit.NewProviderConfig("my-service", "v1.0.0").
WithOTLPExporter("https://api.honeycomb.io", "http", false).
WithSampling("probabilistic", 0.05)
provider, err := otelkit.NewProvider(ctx, config)
if err != nil {
log.Fatal(err)
}
defer provider.Shutdown(ctx)
HTTP Middleware ΒΆ
For HTTP request tracing:
tracer := otelkit.New("web-service")
middleware := otelkit.NewHttpMiddleware(tracer)
r.Use(middleware.Middleware)
The package handles: - OTLP exporter configuration (HTTP/gRPC) - Sampling strategies (probabilistic, always_on, always_off) - Resource management with service metadata - Context propagation for distributed tracing - HTTP and gRPC instrumentation - Error recording and span utilities
Configuration can be done via environment variables or programmatically. See the README for comprehensive examples and configuration options.
Index ΒΆ
- Constants
- func AddAttributes(span trace.Span, attrs ...attribute.KeyValue)
- func AddEvent(span trace.Span, name string, attrs ...attribute.KeyValue)
- func AddTimedEvent(span trace.Span, name string, duration time.Duration)
- func EndSpan(span trace.Span)
- func IsRecording(span trace.Span) bool
- func MustSetupTracing(ctx context.Context, serviceName string, serviceVersion ...string) func(context.Context) errordeprecated
- func New(name string) *tracer.Tracer
- func NewDefaultProvider(ctx context.Context, serviceName string, serviceVersion ...string) (*sdktrace.TracerProvider, error)
- func NewHttpMiddleware(tracer *tracer.Tracer) *middleware.HTTPMiddleware
- func NewProvider(ctx context.Context, cfg *provider.ProviderConfig) (*sdktrace.TracerProvider, error)
- func NewProviderConfig(serviceName, serviceVersion string) *provider.ProviderConfig
- func RecordError(span trace.Span, err error)
- func RecordErrorWithCode(span trace.Span, err error, code string, message string)
- func SetGlobalTracerProvider(tp *sdktrace.TracerProvider)
- func SetupCustomTracing(ctx context.Context, cfg *provider.ProviderConfig) (*sdktrace.TracerProvider, error)deprecated
- func SetupTracing(ctx context.Context, serviceName string, serviceVersion ...string) (func(context.Context) error, error)
- func SetupTracingWithDefaults(ctx context.Context, serviceName, serviceVersion string) (func(context.Context) error, error)deprecated
- func ShutdownTracerProvider(ctx context.Context, tp *sdktrace.TracerProvider) error
- type ConfigError
- type HTTPMiddleware
- type InitializationError
- type ProviderConfig
- type Span
- type TracedHTTPClient
- type Tracer
Constants ΒΆ
const ErrorCodeExternalService = tracer.ErrorCodeExternalService
ErrorCodeExternalService is a constant for external service errors.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func AddAttributes ΒΆ
AddAttributes safely adds one or more attributes to the given span. If the span is nil, this function is a no-op.
func AddEvent ΒΆ
AddEvent safely adds a named event with optional attributes to the span. If the span is nil, this function is a no-op.
func AddTimedEvent ΒΆ
AddTimedEvent adds an event with duration information to the span.
func IsRecording ΒΆ
IsRecording checks if the span is currently recording telemetry data. Returns false if the span is nil or if the span context is invalid.
func MustSetupTracing
deprecated
func MustSetupTracing(ctx context.Context, serviceName string, serviceVersion ...string) func(context.Context) error
MustSetupTracing is like SetupTracing but panics on error. Use this for simple programs where you want to fail fast.
Deprecated: Handle errors explicitly instead. This function will be removed in v1.0.0.
func New ΒΆ
New creates a new tracer instance with the given name. This is the main entry point for creating tracers that will be used throughout your application for manual span creation.
Example:
tracer := otelkit.New("my-service")
ctx, span := tracer.Start(ctx, "operation-name")
defer span.End()
func NewDefaultProvider ΒΆ
func NewDefaultProvider(ctx context.Context, serviceName string, serviceVersion ...string) (*sdktrace.TracerProvider, error)
NewDefaultProvider creates a tracer provider with default settings and sets it as the global provider. This is a convenience function for quick setup in development or simple applications.
Example:
provider, err := otelkit.NewDefaultProvider(ctx, "my-service", "v1.0.0")
if err != nil {
log.Fatal(err)
}
defer provider.Shutdown(ctx)
func NewHttpMiddleware ΒΆ
func NewHttpMiddleware(tracer *tracer.Tracer) *middleware.HTTPMiddleware
NewHttpMiddleware creates HTTP middleware for automatic request tracing. This middleware automatically creates spans for HTTP requests and adds useful attributes like HTTP method, URL, status code, and user agent.
Example:
tracer := otelkit.New("web-service")
middleware := otelkit.NewHttpMiddleware(tracer)
r.Use(middleware.Middleware)
func NewProvider ΒΆ
func NewProvider(ctx context.Context, cfg *provider.ProviderConfig) (*sdktrace.TracerProvider, error)
NewProvider creates and configures a new TracerProvider using the provided configuration, then sets it as the global OpenTelemetry provider (only once per application lifecycle). This is the recommended way to initialize tracing when you need custom configuration.
Example:
config := otelkit.NewProviderConfig("payment-service", "v1.2.3").
WithOTLPExporter("https://api.honeycomb.io", "http", false).
WithSampling("probabilistic", 0.05)
provider, err := otelkit.NewProvider(ctx, config)
if err != nil {
log.Fatal(err)
}
defer provider.Shutdown(ctx)
func NewProviderConfig ΒΆ
func NewProviderConfig(serviceName, serviceVersion string) *provider.ProviderConfig
NewProviderConfig creates a new ProviderConfig with sensible defaults for advanced configuration. This is the starting point for custom tracer provider configuration when you need more control than the default setup provides.
Example:
config := otelkit.NewProviderConfig("my-service", "v1.0.0").
WithOTLPExporter("https://api.honeycomb.io", "http", false).
WithSampling("probabilistic", 0.05)
func RecordError ΒΆ
RecordError safely records an error on the span and sets the span status to error. This function handles nil checks for both span and error.
func RecordErrorWithCode ΒΆ
RecordErrorWithCode safely records an error on the span with a custom error code and message.
func SetGlobalTracerProvider ΒΆ
func SetGlobalTracerProvider(tp *sdktrace.TracerProvider)
SetGlobalTracerProvider sets the global tracer provider. This is typically called automatically by the setup functions, but can be called manually if needed.
func SetupCustomTracing
deprecated
func SetupCustomTracing(ctx context.Context, cfg *provider.ProviderConfig) (*sdktrace.TracerProvider, error)
SetupCustomTracing provides full control over the tracing setup. Use this when you need custom configuration that goes beyond environment variables.
Deprecated: Use NewProviderConfig() with NewProvider() for advanced configuration. This function will be removed in v1.0.0.
func SetupTracing ΒΆ
func SetupTracing(ctx context.Context, serviceName string, serviceVersion ...string) (func(context.Context) error, error)
SetupTracing initializes OpenTelemetry tracing with sensible defaults. This is the simplest way to get started with tracing.
Example:
shutdown, err := otelkit.SetupTracing(ctx, "my-service")
if err != nil {
log.Fatal(err)
}
defer shutdown(ctx)
func SetupTracingWithDefaults
deprecated
func SetupTracingWithDefaults(ctx context.Context, serviceName, serviceVersion string) (func(context.Context) error, error)
SetupTracingWithDefaults initializes tracing with hardcoded defaults. This is useful for quick setup without environment variables.
Deprecated: Use SetupTracing instead. This function will be removed in v1.0.0.
func ShutdownTracerProvider ΒΆ
func ShutdownTracerProvider(ctx context.Context, tp *sdktrace.TracerProvider) error
ShutdownTracerProvider gracefully shuts down the tracer provider, ensuring all pending spans are exported before the application terminates.
Example:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := otelkit.ShutdownTracerProvider(ctx, provider); err != nil {
log.Printf("Error during tracer shutdown: %v", err)
}
Types ΒΆ
type ConfigError ΒΆ
type ConfigError = config.ConfigError
ConfigError represents a configuration validation error.
type HTTPMiddleware ΒΆ
type HTTPMiddleware = middleware.HTTPMiddleware
HTTPMiddleware is an alias for the HTTP middleware type. This provides a cleaner API surface for users of this package.
type InitializationError ΒΆ
type InitializationError = config.InitializationError
InitializationError represents an error during tracer provider initialization.
type ProviderConfig ΒΆ
type ProviderConfig = provider.ProviderConfig
ProviderConfig is an alias for the provider configuration type. This provides a cleaner API surface for users of this package.
type Span ΒΆ
Span is an alias for the underlying OpenTelemetry span interface. This provides a cleaner API surface for users of this package.
type TracedHTTPClient ΒΆ
type TracedHTTPClient = tracer.TracedHTTPClient
TracedHTTPClient is an alias for the traced HTTP client type.
func NewTracedHTTPClient ΒΆ
func NewTracedHTTPClient(client *http.Client, tr *Tracer, service string) *TracedHTTPClient
NewTracedHTTPClient creates a new traced HTTP client.
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Package main demonstrates basic OpenTelemetry tracing with HTTP middleware.
|
Package main demonstrates basic OpenTelemetry tracing with HTTP middleware. |
|
dummy_service
command
|
|
|
gin
command
Package main demonstrates OpenTelemetry tracing with Gin framework integration.
|
Package main demonstrates OpenTelemetry tracing with Gin framework integration. |
|
production
command
Package main demonstrates a production-ready OpenTelemetry tracing setup with comprehensive configuration, error handling, and best practices for real-world applications.
|
Package main demonstrates a production-ready OpenTelemetry tracing setup with comprehensive configuration, error handling, and best practices for real-world applications. |
|
traced_http_client
command
|
|
|
internal
|
|
|
Package middleware provides HTTP middleware for automatic request tracing.
|
Package middleware provides HTTP middleware for automatic request tracing. |
|
Package provider implements the factory and builder patterns for creating and configuring OpenTelemetry tracer providers.
|
Package provider implements the factory and builder patterns for creating and configuring OpenTelemetry tracer providers. |
|
Package tracer provides span utility functions for OpenTelemetry tracing.
|
Package tracer provides span utility functions for OpenTelemetry tracing. |