Documentation
¶
Index ¶
- Constants
- func ContextWithRemote(ctx context.Context, sc SpanContext) context.Context
- func ContextWithSpan(ctx context.Context, s *Span) context.Context
- func Extract(ctx context.Context, h http.Header) context.Context
- func Inject(ctx context.Context, h http.Header)
- func TraceIDFromContext(ctx context.Context) string
- func Transport(base http.RoundTripper) http.RoundTripper
- type Event
- type Exporter
- type OTLPConfig
- type OTLPExporter
- type Span
- type SpanContext
- type SpanData
- type SpanID
- type SpanKind
- type StartOption
- type TraceID
- type Tracer
Constants ¶
const ( TraceparentHeader = "traceparent" TracestateHeader = "tracestate" )
TraceparentHeader and TracestateHeader are the W3C Trace Context headers.
Variables ¶
This section is empty.
Functions ¶
func ContextWithRemote ¶
func ContextWithRemote(ctx context.Context, sc SpanContext) context.Context
ContextWithRemote returns ctx carrying a parent received from another process (traceparent header, queue job metadata).
func ContextWithSpan ¶
ContextWithSpan returns ctx carrying span.
func Extract ¶
Extract returns ctx with the remote parent from the request's traceparent header, if it has a valid one.
func TraceIDFromContext ¶
TraceIDFromContext returns the current trace ID as hex, or "".
func Transport ¶
func Transport(base http.RoundTripper) http.RoundTripper
Transport wraps base (nil = http.DefaultTransport) so every outbound request gets a client span and a traceparent header.
Types ¶
type OTLPConfig ¶
type OTLPConfig struct {
// Endpoint is the full traces URL, e.g. http://localhost:4318/v1/traces.
Endpoint string
// Headers are sent with every export (API keys etc.).
Headers map[string]string
// ServiceName is the service.name resource attribute.
ServiceName string
// Resource holds extra resource attributes (deployment.environment, ...).
Resource map[string]string
// BatchSize flushes once this many spans are buffered (default 512).
BatchSize int
// FlushInterval flushes buffered spans at least this often (default 5s).
FlushInterval time.Duration
// MaxQueue drops spans beyond this many unsent (default 8192).
MaxQueue int
// Client overrides the HTTP client (default: 10s timeout).
Client *http.Client
}
OTLPConfig configures the OTLP/HTTP (JSON) exporter.
type OTLPExporter ¶
type OTLPExporter struct {
// contains filtered or unexported fields
}
OTLPExporter batches spans and POSTs them as OTLP/HTTP JSON.
func ConfigureFromEnv ¶
func ConfigureFromEnv() *OTLPExporter
ConfigureFromEnv installs an OTLP exporter on the global tracer when an OTLP endpoint is set (see the package doc) and returns it, or nil. Spans and traceparent propagation work either way; without an exporter they are simply not sent anywhere.
func NewOTLPExporter ¶
func NewOTLPExporter(cfg OTLPConfig) *OTLPExporter
NewOTLPExporter starts an exporter. Call Shutdown to flush on exit.
func (*OTLPExporter) ExportSpans ¶
func (e *OTLPExporter) ExportSpans(spans []SpanData)
ExportSpans implements Exporter. It never blocks on the network.
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span is one timed operation. Methods are safe for concurrent use and are no-ops on a nil span.
func SpanFromContext ¶
SpanFromContext returns the current span, or nil.
func (*Span) End ¶
func (s *Span) End()
End finishes the span and hands it to the exporter. Calling it again does nothing.
func (*Span) RecordError ¶
RecordError marks the span as failed.
func (*Span) SetAttr ¶
SetAttr records an attribute (string, bool, int, int64, float64 or anything else, which is formatted with %v).
func (*Span) SpanContext ¶
func (s *Span) SpanContext() SpanContext
SpanContext returns the span's propagation context.
type SpanContext ¶
type SpanContext struct {
TraceID TraceID
SpanID SpanID
Sampled bool
TraceState string // opaque vendor data, passed through unchanged
Remote bool
}
SpanContext is the part of a span that crosses process boundaries.
func ParseTraceparent ¶
func ParseTraceparent(h string) (SpanContext, error)
ParseTraceparent parses a W3C traceparent header value.
func SpanContextFromContext ¶
func SpanContextFromContext(ctx context.Context) SpanContext
SpanContextFromContext returns the current span's context, or a remote parent's, or an invalid SpanContext.
func (SpanContext) IsValid ¶
func (sc SpanContext) IsValid() bool
IsValid reports whether both IDs are set.
func (SpanContext) Traceparent ¶
func (sc SpanContext) Traceparent() string
Traceparent renders the W3C traceparent header value.
type SpanData ¶
type SpanData struct {
TraceID TraceID
SpanID SpanID
Parent SpanID
TraceState string
Name string
Kind SpanKind
Start, End time.Time
Attrs map[string]any
Events []Event
Error bool
ErrorString string
}
SpanData is a finished span as handed to an Exporter.
type StartOption ¶
type StartOption func(*Span)
StartOption configures a span.
func WithAttrs ¶
func WithAttrs(attrs map[string]any) StartOption
WithAttrs sets initial attributes.
func WithKind ¶
func WithKind(k SpanKind) StartOption
WithKind sets the span kind (default internal).
func WithParent ¶
func WithParent(sc SpanContext) StartOption
WithParent starts the span under sc instead of the span in ctx.
type TraceID ¶
type TraceID [16]byte
TraceID identifies a trace (16 bytes).
type Tracer ¶
type Tracer struct {
// contains filtered or unexported fields
}
Tracer starts spans and sends them to an exporter.
func (*Tracer) SetExporter ¶
SetExporter sets where finished spans go (nil drops them).
func (*Tracer) SetSampleRatio ¶
SetSampleRatio sets the share of new traces that are recorded (0..1). Traces continued from a caller follow the caller's sampling decision.