Documentation
¶
Index ¶
- func GetTracer(name string) trace.Tracer
- func HTTPTracingMiddleware(tracerName, route string) func(http.Handler) http.Handler
- func Init(ctx context.Context, config Config) (func(context.Context) error, error)
- func InitWithDefaults(ctx context.Context, serviceName string) (func(context.Context) error, error)
- func Shutdown(ctx context.Context) error
- type Config
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetTracer ¶
GetTracer returns a tracer for the given name. The name is typically the package or component name (e.g., "zen-back/http"). This tracer can be used to create spans for distributed tracing.
func HTTPTracingMiddleware ¶
HTTPTracingMiddleware creates HTTP middleware that adds tracing to HTTP requests. It extracts trace context from headers, creates a span for the request, and adds span attributes for HTTP method, route, URL, status code, and duration.
func Init ¶
Init initializes OpenTelemetry tracing for the application. This sets up the global tracer provider and configures trace context propagation.
The exporter type can be specified in config or via OTEL_EXPORTER_TYPE environment variable:
- "otlp" or "otlphttp": Uses OTLP HTTP exporter
- "stdout" or empty: Tracing disabled (no-op)
Returns a shutdown function that should be called during application shutdown, and an error if initialization fails.
func InitWithDefaults ¶
InitWithDefaults initializes OpenTelemetry with default configuration. Uses environment variables for configuration:
- OTEL_SERVICE_NAME (required, or use provided serviceName)
- OTEL_EXPORTER_TYPE (optional, defaults to "otlphttp")
- OTEL_EXPORTER_OTLP_ENDPOINT (optional, defaults to service-specific endpoint)
- DEPLOYMENT_ENV (optional, for environment tag)
Types ¶
type Config ¶
type Config struct {
ServiceName string // Service name (required)
ServiceVersion string // Service version (optional, defaults to "dev")
Environment string // Deployment environment (optional)
SamplingRate float64 // Sampling rate (0.0-1.0, default: 0.1 = 10%)
OTLPEndpoint string // OTLP endpoint (optional, uses env var or default)
ExporterType string // Exporter type: "otlp", "otlphttp", "stdout" (optional, uses env var)
Insecure bool // Use insecure connection for OTLP (default: true for development)
}
Config holds OpenTelemetry configuration
func DefaultConfig ¶
DefaultConfig returns a config with sensible defaults