Documentation ¶
Overview ¶
Package trace contains support for OpenTelemetry distributed tracing.
This package is currently in a pre-GA phase. Backwards incompatible changes may be introduced in subsequent minor version releases as we work to track the evolving OpenTelemetry specification and user feedback.
The following assumes a basic familiarity with OpenTelemetry concepts. See https://opentelemetry.io.
Index ¶
- Constants
- type BatchSpanProcessorOption
- type BatchSpanProcessorOptions
- type IDGenerator
- type ParentBasedSamplerOption
- type ReadOnlySpan
- type ReadWriteSpan
- type Sampler
- type SamplingDecision
- type SamplingParameters
- type SamplingResult
- type SpanLimits
- type SpanProcessor
- type TracerProvider
- func (p *TracerProvider) ForceFlush(ctx context.Context) error
- func (p *TracerProvider) RegisterSpanProcessor(s SpanProcessor)
- func (p *TracerProvider) Shutdown(ctx context.Context) error
- func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer
- func (p *TracerProvider) UnregisterSpanProcessor(s SpanProcessor)
- type TracerProviderConfig
- type TracerProviderOption
- func WithBatcher(e export.SpanExporter, opts ...BatchSpanProcessorOption) TracerProviderOption
- func WithIDGenerator(g IDGenerator) TracerProviderOption
- func WithResource(r *resource.Resource) TracerProviderOption
- func WithSampler(s Sampler) TracerProviderOption
- func WithSpanLimits(sl SpanLimits) TracerProviderOption
- func WithSpanProcessor(sp SpanProcessor) TracerProviderOption
- func WithSyncer(e export.SpanExporter) TracerProviderOption
Examples ¶
Constants ¶
const ( DefaultMaxQueueSize = 2048 DefaultBatchTimeout = 5000 * time.Millisecond DefaultMaxExportBatchSize = 512 )
const ( // DefaultAttributeCountLimit is the default maximum allowed span attribute count. DefaultAttributeCountLimit = 128 // DefaultEventCountLimit is the default maximum allowed span event count. DefaultEventCountLimit = 128 // DefaultLinkCountLimit is the default maximum allowed span link count. DefaultLinkCountLimit = 128 // DefaultAttributePerEventCountLimit is the default maximum allowed attribute per span event count. DefaultAttributePerEventCountLimit = 128 // DefaultAttributePerLinkCountLimit is the default maximum allowed attribute per span link count. DefaultAttributePerLinkCountLimit = 128 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BatchSpanProcessorOption ¶
type BatchSpanProcessorOption func(o *BatchSpanProcessorOptions)
func WithBatchTimeout ¶
func WithBatchTimeout(delay time.Duration) BatchSpanProcessorOption
func WithBlocking ¶
func WithBlocking() BatchSpanProcessorOption
func WithMaxExportBatchSize ¶
func WithMaxExportBatchSize(size int) BatchSpanProcessorOption
func WithMaxQueueSize ¶
func WithMaxQueueSize(size int) BatchSpanProcessorOption
type BatchSpanProcessorOptions ¶
type BatchSpanProcessorOptions struct { // MaxQueueSize is the maximum queue size to buffer spans for delayed processing. If the // queue gets full it drops the spans. Use BlockOnQueueFull to change this behavior. // The default value of MaxQueueSize is 2048. MaxQueueSize int // BatchTimeout is the maximum duration for constructing a batch. Processor // forcefully sends available spans when timeout is reached. // The default value of BatchTimeout is 5000 msec. BatchTimeout time.Duration // MaxExportBatchSize is the maximum number of spans to process in a single batch. // If there are more than one batch worth of spans then it processes multiple batches // of spans one batch after the other without any delay. // The default value of MaxExportBatchSize is 512. MaxExportBatchSize int // BlockOnQueueFull blocks onEnd() and onStart() method if the queue is full // AND if BlockOnQueueFull is set to true. // Blocking option should be used carefully as it can severely affect the performance of an // application. BlockOnQueueFull bool }
type IDGenerator ¶ added in v0.15.0
type IDGenerator interface { NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID) NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID }
IDGenerator allows custom generators for TraceID and SpanID.
type ParentBasedSamplerOption ¶ added in v0.12.0
type ParentBasedSamplerOption interface { Apply(*config) // contains filtered or unexported methods }
ParentBasedSamplerOption configures the sampler for a particular sampling case.
func WithLocalParentNotSampled ¶ added in v0.12.0
func WithLocalParentNotSampled(s Sampler) ParentBasedSamplerOption
WithLocalParentNotSampled sets the sampler for the case of local parent which is not sampled.
func WithLocalParentSampled ¶ added in v0.12.0
func WithLocalParentSampled(s Sampler) ParentBasedSamplerOption
WithLocalParentSampled sets the sampler for the case of sampled local parent.
func WithRemoteParentNotSampled ¶ added in v0.12.0
func WithRemoteParentNotSampled(s Sampler) ParentBasedSamplerOption
WithRemoteParentNotSampled sets the sampler for the case of remote parent which is not sampled.
func WithRemoteParentSampled ¶ added in v0.12.0
func WithRemoteParentSampled(s Sampler) ParentBasedSamplerOption
WithRemoteParentSampled sets the sampler for the case of sampled remote parent.
type ReadOnlySpan ¶ added in v0.16.0
type ReadOnlySpan interface { Name() string SpanContext() trace.SpanContext Parent() trace.SpanContext SpanKind() trace.SpanKind StartTime() time.Time EndTime() time.Time Attributes() []attribute.KeyValue Links() []trace.Link Events() []trace.Event StatusCode() codes.Code StatusMessage() string Tracer() trace.Tracer IsRecording() bool InstrumentationLibrary() instrumentation.Library Resource() *resource.Resource Snapshot() *export.SpanSnapshot // contains filtered or unexported methods }
ReadOnlySpan allows reading information from the data structure underlying a trace.Span. It is used in places where reading information from a span is necessary but changing the span isn't necessary or allowed. TODO: Should we make the methods unexported? The purpose of this interface is controlling access to `span` fields, not having multiple implementations.
type ReadWriteSpan ¶ added in v0.16.0
type ReadWriteSpan interface { trace.Span ReadOnlySpan }
ReadWriteSpan exposes the same methods as trace.Span and in addition allows reading information from the underlying data structure. This interface exposes the union of the methods of trace.Span (which is a "write-only" span) and ReadOnlySpan. New methods for writing or reading span information should be added under trace.Span or ReadOnlySpan, respectively.
type Sampler ¶
type Sampler interface { ShouldSample(parameters SamplingParameters) SamplingResult Description() string }
Sampler decides whether a trace should be sampled and exported.
func AlwaysSample ¶
func AlwaysSample() Sampler
AlwaysSample returns a Sampler that samples every trace. Be careful about using this sampler in a production application with significant traffic: a new trace will be started and exported for every request.
func ParentBased ¶ added in v0.12.0
func ParentBased(root Sampler, samplers ...ParentBasedSamplerOption) Sampler
ParentBased returns a composite sampler which behaves differently, based on the parent of the span. If the span has no parent, the root(Sampler) is used to make sampling decision. If the span has a parent, depending on whether the parent is remote and whether it is sampled, one of the following samplers will apply: - remoteParentSampled(Sampler) (default: AlwaysOn) - remoteParentNotSampled(Sampler) (default: AlwaysOff) - localParentSampled(Sampler) (default: AlwaysOn) - localParentNotSampled(Sampler) (default: AlwaysOff)
func TraceIDRatioBased ¶ added in v0.12.0
TraceIDRatioBased samples a given fraction of traces. Fractions >= 1 will always sample. Fractions < 0 are treated as zero. To respect the parent trace's `SampledFlag`, the `TraceIDRatioBased` sampler should be used as a delegate of a `Parent` sampler.
type SamplingDecision ¶
type SamplingDecision uint8
SamplingDecision indicates whether a span is dropped, recorded and/or sampled.
const ( // Drop will not record the span and all attributes/events will be dropped Drop SamplingDecision = iota // Record indicates the span's `IsRecording() == true`, but `Sampled` flag // *must not* be set RecordOnly // RecordAndSample has span's `IsRecording() == true` and `Sampled` flag // *must* be set RecordAndSample )
Valid sampling decisions
type SamplingParameters ¶
type SamplingParameters struct { ParentContext trace.SpanContext TraceID trace.TraceID Name string HasRemoteParent bool Kind trace.SpanKind Attributes []attribute.KeyValue Links []trace.Link }
SamplingParameters contains the values passed to a Sampler.
type SamplingResult ¶
type SamplingResult struct { Decision SamplingDecision Attributes []attribute.KeyValue Tracestate trace.TraceState }
SamplingResult conveys a SamplingDecision, set of Attributes and a Tracestate.
type SpanLimits ¶ added in v0.18.0
type SpanLimits struct { // AttributeCountLimit is the maximum allowed span attribute count. AttributeCountLimit int // EventCountLimit is the maximum allowed span event count. EventCountLimit int // LinkCountLimit is the maximum allowed span link count. LinkCountLimit int // AttributePerEventCountLimit is the maximum allowed attribute per span event count. AttributePerEventCountLimit int // AttributePerLinkCountLimit is the maximum allowed attribute per span link count. AttributePerLinkCountLimit int }
SpanLimits represents the limits of a span.
type SpanProcessor ¶
type SpanProcessor interface { // OnStart is called when a span is started. It is called synchronously // and should not block. OnStart(parent context.Context, s ReadWriteSpan) // OnEnd is called when span is finished. It is called synchronously and // hence not block. OnEnd(s ReadOnlySpan) // Shutdown is called when the SDK shuts down. Any cleanup or release of // resources held by the processor should be done in this call. // // Calls to OnStart, OnEnd, or ForceFlush after this has been called // should be ignored. // // All timeouts and cancellations contained in ctx must be honored, this // should not block indefinitely. Shutdown(ctx context.Context) error // ForceFlush exports all ended spans to the configured Exporter that have not yet // been exported. It should only be called when absolutely necessary, such as when // using a FaaS provider that may suspend the process after an invocation, but before // the Processor can export the completed spans. ForceFlush(ctx context.Context) error }
SpanProcessor is a processing pipeline for spans in the trace signal. SpanProcessors registered with a TracerProvider and are called at the start and end of a Span's lifecycle, and are called in the order they are registered.
Example ¶
package main import ( "context" "time" "go.opentelemetry.io/otel/sdk/export/trace/tracetest" ) // DurationFilter is a SpanProcessor that filters spans that have lifetimes // outside of a defined range. type DurationFilter struct { // Next is the next SpanProcessor in the chain. Next SpanProcessor // Min is the duration under which spans are dropped. Min time.Duration // Max is the duration over which spans are dropped. Max time.Duration } func (f DurationFilter) OnStart(parent context.Context, s ReadWriteSpan) { f.Next.OnStart(parent, s) } func (f DurationFilter) Shutdown(ctx context.Context) error { return f.Next.Shutdown(ctx) } func (f DurationFilter) ForceFlush(ctx context.Context) error { return f.Next.ForceFlush(ctx) } func (f DurationFilter) OnEnd(s ReadOnlySpan) { if f.Min > 0 && s.EndTime().Sub(s.StartTime()) < f.Min { // Drop short lived spans. return } if f.Max > 0 && s.EndTime().Sub(s.StartTime()) > f.Max { // Drop long lived spans. return } f.Next.OnEnd(s) } // InstrumentationBlacklist is a SpanProcessor that drops all spans from // certain instrumentation. type InstrumentationBlacklist struct { // Next is the next SpanProcessor in the chain. Next SpanProcessor // Blacklist is the set of instrumentation names for which spans will be // dropped. Blacklist map[string]bool } func (f InstrumentationBlacklist) OnStart(parent context.Context, s ReadWriteSpan) { f.Next.OnStart(parent, s) } func (f InstrumentationBlacklist) Shutdown(ctx context.Context) error { return f.Next.Shutdown(ctx) } func (f InstrumentationBlacklist) ForceFlush(ctx context.Context) error { return f.Next.ForceFlush(ctx) } func (f InstrumentationBlacklist) OnEnd(s ReadOnlySpan) { if f.Blacklist != nil && f.Blacklist[s.InstrumentationLibrary().Name] { // Drop spans from this instrumentation return } f.Next.OnEnd(s) } func main() { exportSP := NewSimpleSpanProcessor(tracetest.NewNoopExporter()) // Build a SpanProcessor chain to filter out all spans from the pernicious // "naughty-instrumentation" dependency and only allow spans shorter than // an minute and longer than a second to be exported with the exportSP. filter := DurationFilter{ Next: InstrumentationBlacklist{ Next: exportSP, Blacklist: map[string]bool{ "naughty-instrumentation": true, }, }, Min: time.Second, Max: time.Minute, } _ = NewTracerProvider(WithSpanProcessor(filter)) // ... }
Output:
func NewBatchSpanProcessor ¶
func NewBatchSpanProcessor(exporter export.SpanExporter, options ...BatchSpanProcessorOption) SpanProcessor
NewBatchSpanProcessor creates a new SpanProcessor that will send completed span batches to the exporter with the supplied options.
If the exporter is nil, the span processor will preform no action.
func NewSimpleSpanProcessor ¶
func NewSimpleSpanProcessor(exporter export.SpanExporter) SpanProcessor
NewSimpleSpanProcessor returns a new SpanProcessor that will synchronously send completed spans to the exporter immediately.
type TracerProvider ¶ added in v0.12.0
type TracerProvider struct {
// contains filtered or unexported fields
}
func NewTracerProvider ¶ added in v0.12.0
func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider
NewTracerProvider returns a new and configured TracerProvider.
By default the returned TracerProvider is configured with:
- a ParentBased(AlwaysSample) Sampler
- a random number IDGenerator
- the resource.Default() Resource
- the default SpanLimits.
The passed opts are used to override these default values and configure the returned TracerProvider appropriately.
func (*TracerProvider) ForceFlush ¶ added in v0.19.0
func (p *TracerProvider) ForceFlush(ctx context.Context) error
ForceFlush immediately exports all spans that have not yet been exported for all the registered span processors.
func (*TracerProvider) RegisterSpanProcessor ¶ added in v0.12.0
func (p *TracerProvider) RegisterSpanProcessor(s SpanProcessor)
RegisterSpanProcessor adds the given SpanProcessor to the list of SpanProcessors
func (*TracerProvider) Shutdown ¶ added in v0.14.0
func (p *TracerProvider) Shutdown(ctx context.Context) error
Shutdown shuts down the span processors in the order they were registered.
func (*TracerProvider) Tracer ¶ added in v0.12.0
func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer
Tracer returns a Tracer with the given name and options. If a Tracer for the given name and options does not exist it is created, otherwise the existing Tracer is returned.
If name is empty, DefaultTracerName is used instead.
This method is safe to be called concurrently.
func (*TracerProvider) UnregisterSpanProcessor ¶ added in v0.12.0
func (p *TracerProvider) UnregisterSpanProcessor(s SpanProcessor)
UnregisterSpanProcessor removes the given SpanProcessor from the list of SpanProcessors
type TracerProviderConfig ¶ added in v0.12.0
type TracerProviderConfig struct {
// contains filtered or unexported fields
}
TracerProviderConfig
type TracerProviderOption ¶ added in v0.12.0
type TracerProviderOption func(*TracerProviderConfig)
func WithBatcher ¶
func WithBatcher(e export.SpanExporter, opts ...BatchSpanProcessorOption) TracerProviderOption
WithBatcher registers the exporter with the TracerProvider using a BatchSpanProcessor configured with the passed opts.
func WithIDGenerator ¶ added in v0.15.0
func WithIDGenerator(g IDGenerator) TracerProviderOption
WithIDGenerator returns a TracerProviderOption that will configure the IDGenerator g as a TracerProvider's IDGenerator. The configured IDGenerator is used by the Tracers the TracerProvider creates to generate new Span and Trace IDs.
If this option is not used, the TracerProvider will use a random number IDGenerator by default.
func WithResource ¶
func WithResource(r *resource.Resource) TracerProviderOption
WithResource returns a TracerProviderOption that will configure the Resource r as a TracerProvider's Resource. The configured Resource is referenced by all the Tracers the TracerProvider creates. It represents the entity producing telemetry.
If this option is not used, the TracerProvider will use the resource.Default() Resource by default.
func WithSampler ¶ added in v0.19.0
func WithSampler(s Sampler) TracerProviderOption
WithSampler returns a TracerProviderOption that will configure the Sampler s as a TracerProvider's Sampler. The configured Sampler is used by the Tracers the TracerProvider creates to make their sampling decisions for the Spans they create.
If this option is not used, the TracerProvider will use a ParentBased(AlwaysSample) Sampler by default.
func WithSpanLimits ¶ added in v0.19.0
func WithSpanLimits(sl SpanLimits) TracerProviderOption
WithSpanLimits returns a TracerProviderOption that will configure the SpanLimits sl as a TracerProvider's SpanLimits. The configured SpanLimits are used used by the Tracers the TracerProvider and the Spans they create to limit tracing resources used.
If this option is not used, the TracerProvider will use the default SpanLimits.
func WithSpanProcessor ¶ added in v0.12.0
func WithSpanProcessor(sp SpanProcessor) TracerProviderOption
WithSpanProcessor registers the SpanProcessor with a TracerProvider.
func WithSyncer ¶
func WithSyncer(e export.SpanExporter) TracerProviderOption
WithSyncer registers the exporter with the TracerProvider using a SimpleSpanProcessor.