otel

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package otel bootstraps OpenTelemetry tracing and provides helpers to inject a trace id into the context (and therefore into logs), open child spans, and propagate trace context across service boundaries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSpan

func AddSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)

AddSpan starts a child span on the tracer stored in ctx. If no tracer is present it returns ctx unchanged and a no-op span, so callers never need a nil check.

func ExtractFromRequest

func ExtractFromRequest(ctx context.Context, r *http.Request) context.Context

ExtractFromRequest returns a context seeded with the trace context carried by an incoming request's headers.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID returns the trace id stored in ctx, or an empty string if none. It is designed to be passed as logger.TraceIDFn so every log line carries the active trace id.

func InitTracing

func InitTracing(ctx context.Context, cfg Config) (trace.Tracer, func(context.Context) error, error)

InitTracing configures a global TracerProvider exporting via OTLP/gRPC and returns a Tracer plus a shutdown function. The shutdown flushes pending spans.

func InjectToRequest

func InjectToRequest(ctx context.Context, r *http.Request)

InjectToRequest writes the current trace context into an outgoing request's headers so the callee can continue the trace.

func InjectTracing

func InjectTracing(ctx context.Context, tracer trace.Tracer) context.Context

InjectTracing stores the tracer in ctx and ensures a trace id is available (generating one when there is no active span), so downstream logging can pick it up via GetTraceID. Call it once per request, before handling.

Types

type Config

type Config struct {
	// ServiceName tags every span; also used to create the named Tracer.
	ServiceName string
	// Endpoint is the OTLP/gRPC collector endpoint, e.g. "localhost:4317".
	Endpoint string
	// Insecure disables TLS to the collector (typical for local/in-cluster).
	Insecure bool
	// Probability is the sampling ratio in [0,1] applied to non-excluded routes.
	Probability float64
	// ExcludedRoutes are never sampled (health/readiness probes, etc.).
	ExcludedRoutes map[string]struct{}
}

Config configures tracing.

Jump to

Keyboard shortcuts

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