tracer

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package tracer describes interfaces for a tracer.Tracer used to start spans, propagate trace context across process boundaries, and record events on the active span. The default implementation is backed by OpenTelemetry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FxModule

func FxModule() fx.Option

FxModule wires the tracer into an Fx application. The returned Tracer is shut down on fx.OnStop.

Types

type Attribute

type Attribute struct {
	Key   string
	Value any
}

Attribute is a typed key/value attached to spans and events.

func Bool

func Bool(key string, value bool) Attribute

Bool returns an Attribute with a bool value.

func Float64

func Float64(key string, value float64) Attribute

Float64 returns an Attribute with a float64 value.

func Int

func Int(key string, value int) Attribute

Int returns an Attribute with an int value.

func Int64

func Int64(key string, value int64) Attribute

Int64 returns an Attribute with an int64 value.

func String

func String(key, value string) Attribute

String returns an Attribute with a string value.

type Carrier

type Carrier interface {
	Get(key string) string
	Set(key, value string)
	Keys() []string
}

Carrier is the contract used by Inject/Extract to read or write trace headers from an arbitrary transport (HTTP headers, gRPC metadata, AMQP headers, NATS message headers, ...).

type Configuration

type Configuration struct {
	ServiceName string  `required:"true"  envconfig:"SVC_NAME"`
	Type        string  `required:"false" envconfig:"TRACER_TYPE"             default:"otel"`
	Exporter    string  `required:"false" envconfig:"TRACER_EXPORTER"         default:"otlphttp"`
	Endpoint    string  `required:"false" envconfig:"OTEL_EXPORTER_OTLP_ENDPOINT"`
	Insecure    bool    `required:"false" envconfig:"TRACER_INSECURE"         default:"true"`
	SampleRatio float64 `required:"false" envconfig:"TRACER_SAMPLE_RATIO"     default:"1.0"`
	Enabled     bool    `required:"false" envconfig:"TRACER_ENABLED"          default:"true"`
}

Configuration is the env-driven config used by the Fx module. It honours the OTEL_* env vars where reasonable so it composes with the OpenTelemetry SDK conventions.

type ExporterType

type ExporterType string

ExporterType defines the kind of OTel exporter to install.

const (
	ExporterOTLPHTTP ExporterType = "otlphttp"
	ExporterOTLPGRPC ExporterType = "otlpgrpc"
	ExporterNone     ExporterType = "none"
)

func ParseExporter

func ParseExporter(s string) ExporterType

ParseExporter parses a string into an ExporterType. Unknown values fall through to ExporterOTLPHTTP.

type MapCarrier

type MapCarrier map[string]string

MapCarrier adapts a string map to the Carrier interface. It is useful for tests and for transports that already expose headers as map[string]string.

func (MapCarrier) Get

func (m MapCarrier) Get(key string) string

func (MapCarrier) Keys

func (m MapCarrier) Keys() []string

func (MapCarrier) Set

func (m MapCarrier) Set(key, value string)

type Option

type Option func(*config)

Option configures the tracer at construction time.

func WithEndpoint

func WithEndpoint(endpoint string) Option

WithEndpoint sets the OTLP exporter endpoint (host:port for grpc, scheme://host:port for http). Empty falls back to OTel SDK env vars.

func WithExporter

func WithExporter(e ExporterType) Option

WithExporter selects the OTel exporter wire protocol.

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders attaches headers to the OTLP exporter (auth, tenant routing).

func WithInsecure

func WithInsecure(insecure bool) Option

WithInsecure disables transport security for the OTLP exporter.

func WithResourceAttributes

func WithResourceAttributes(attrs ...Attribute) Option

WithResourceAttributes adds extra resource attributes (env, region, ...).

func WithSampleRatio

func WithSampleRatio(ratio float64) Option

WithSampleRatio sets the head-based sampler ratio in [0,1]. 0 disables sampling, 1 samples everything.

func WithServiceName

func WithServiceName(name string) Option

WithServiceName sets the service.name resource attribute.

func WithType

func WithType(t TracerType) Option

WithType selects the tracer implementation (otel / noop).

type Span

type Span interface {
	End()
	SetAttributes(attrs ...Attribute)
	SetStatus(code StatusCode, description string)
	RecordError(err error, attrs ...Attribute)
	AddEvent(name string, attrs ...Attribute)
	SpanContext() SpanContext
	IsRecording() bool
}

Span is the recording handle returned by Tracer.Start.

type SpanContext

type SpanContext struct {
	TraceID    string
	SpanID     string
	TraceFlags byte
	Remote     bool
}

SpanContext identifies a span across processes.

func (SpanContext) IsValid

func (s SpanContext) IsValid() bool

IsValid reports whether the SpanContext carries a usable trace id.

type SpanKind

type SpanKind int

SpanKind mirrors OTel span kinds without forcing callers to import OTel.

const (
	SpanKindInternal SpanKind = iota
	SpanKindServer
	SpanKindClient
	SpanKindProducer
	SpanKindConsumer
)

type StartConfig

type StartConfig struct {
	Kind       SpanKind
	Attributes []Attribute
}

StartConfig is the resolved set of options for Tracer.Start.

type StartOption

type StartOption func(*StartConfig)

StartOption configures a single Tracer.Start call.

func WithAttributes

func WithAttributes(attrs ...Attribute) StartOption

WithAttributes attaches initial attributes to the started span.

func WithSpanKind

func WithSpanKind(kind SpanKind) StartOption

WithSpanKind sets the kind of the started span.

type StatusCode

type StatusCode int

StatusCode mirrors OTel span status codes.

const (
	StatusUnset StatusCode = iota
	StatusOK
	StatusError
)

type Tracer

type Tracer interface {
	// Start opens a new span as a child of any span already in ctx and returns
	// the derived context plus the span handle. Callers must End() the span.
	Start(ctx context.Context, name string, opts ...StartOption) (context.Context, Span)

	// SpanFromContext returns the span currently recorded in ctx, or a no-op
	// span if none is present.
	SpanFromContext(ctx context.Context) Span

	// Inject writes the current span context into carrier using the configured
	// propagator (defaults to W3C tracecontext + baggage).
	Inject(ctx context.Context, carrier Carrier)

	// Extract reads a span context from carrier and returns a context with it
	// installed as the remote parent.
	Extract(ctx context.Context, carrier Carrier) context.Context

	// Shutdown flushes any buffered spans and releases exporter resources.
	// Safe to call multiple times.
	Shutdown(ctx context.Context) error
}

Tracer is the surface every transport interacts with. Implementations must be safe for concurrent use.

func New

func New(opts ...Option) (Tracer, error)

New constructs a Tracer. Returns the tracer and a shutdown function. When type is NoopTracer the shutdown is a no-op.

type TracerType

type TracerType string

TracerType defines the type of tracer implementation.

const (
	OTelTracer TracerType = "otel"
	NoopTracer TracerType = "noop"
)

func ParseType

func ParseType(s string) TracerType

ParseType parses a string into a TracerType (case-insensitive). Unknown values fall through to OTelTracer so misconfiguration doesn't disable tracing silently in production.

Directories

Path Synopsis
Package internal hides OTel SDK types behind a thin interface so the public tracer package can stay free of vendor-specific imports.
Package internal hides OTel SDK types behind a thin interface so the public tracer package can stay free of vendor-specific imports.
Package tracertest provides test doubles for tracer.Tracer.
Package tracertest provides test doubles for tracer.Tracer.

Jump to

Keyboard shortcuts

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