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 ¶
- func FxModule() fx.Option
- type Attribute
- type Carrier
- type Configuration
- type ExporterType
- type MapCarrier
- type Option
- func WithEndpoint(endpoint string) Option
- func WithExporter(e ExporterType) Option
- func WithHeaders(headers map[string]string) Option
- func WithInsecure(insecure bool) Option
- func WithResourceAttributes(attrs ...Attribute) Option
- func WithSampleRatio(ratio float64) Option
- func WithServiceName(name string) Option
- func WithType(t TracerType) Option
- type Span
- type SpanContext
- type SpanKind
- type StartConfig
- type StartOption
- type StatusCode
- type Tracer
- type TracerType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Attribute ¶
Attribute is a typed key/value attached to spans and events.
type Carrier ¶
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 ¶
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 ¶
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 ¶
WithHeaders attaches headers to the OTLP exporter (auth, tenant routing).
func WithInsecure ¶
WithInsecure disables transport security for the OTLP exporter.
func WithResourceAttributes ¶
WithResourceAttributes adds extra resource attributes (env, region, ...).
func WithSampleRatio ¶
WithSampleRatio sets the head-based sampler ratio in [0,1]. 0 disables sampling, 1 samples everything.
func WithServiceName ¶
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 ¶
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.
type StartConfig ¶
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.
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. |