2021-08-09 07:19:24 +00:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sampler decides whether a trace should be sampled and exported.
|
|
|
|
type Sampler interface {
|
2023-02-01 17:06:36 +00:00
|
|
|
// DO NOT CHANGE: any modification will not be backwards compatible and
|
|
|
|
// must never be done outside of a new major release.
|
|
|
|
|
|
|
|
// ShouldSample returns a SamplingResult based on a decision made from the
|
|
|
|
// passed parameters.
|
2021-08-09 07:19:24 +00:00
|
|
|
ShouldSample(parameters SamplingParameters) SamplingResult
|
2023-02-01 17:06:36 +00:00
|
|
|
// DO NOT CHANGE: any modification will not be backwards compatible and
|
|
|
|
// must never be done outside of a new major release.
|
|
|
|
|
|
|
|
// Description returns information describing the Sampler.
|
2021-08-09 07:19:24 +00:00
|
|
|
Description() string
|
2023-02-01 17:06:36 +00:00
|
|
|
// DO NOT CHANGE: any modification will not be backwards compatible and
|
|
|
|
// must never be done outside of a new major release.
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SamplingParameters contains the values passed to a Sampler.
|
|
|
|
type SamplingParameters struct {
|
|
|
|
ParentContext context.Context
|
|
|
|
TraceID trace.TraceID
|
|
|
|
Name string
|
|
|
|
Kind trace.SpanKind
|
|
|
|
Attributes []attribute.KeyValue
|
|
|
|
Links []trace.Link
|
|
|
|
}
|
|
|
|
|
|
|
|
// SamplingDecision indicates whether a span is dropped, recorded and/or sampled.
|
|
|
|
type SamplingDecision uint8
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// Valid sampling decisions.
|
2021-08-09 07:19:24 +00:00
|
|
|
const (
|
2023-02-01 17:06:36 +00:00
|
|
|
// Drop will not record the span and all attributes/events will be dropped.
|
2021-08-09 07:19:24 +00:00
|
|
|
Drop SamplingDecision = iota
|
|
|
|
|
|
|
|
// Record indicates the span's `IsRecording() == true`, but `Sampled` flag
|
2023-02-01 17:06:36 +00:00
|
|
|
// *must not* be set.
|
2021-08-09 07:19:24 +00:00
|
|
|
RecordOnly
|
|
|
|
|
|
|
|
// RecordAndSample has span's `IsRecording() == true` and `Sampled` flag
|
2023-02-01 17:06:36 +00:00
|
|
|
// *must* be set.
|
2021-08-09 07:19:24 +00:00
|
|
|
RecordAndSample
|
|
|
|
)
|
|
|
|
|
|
|
|
// SamplingResult conveys a SamplingDecision, set of Attributes and a Tracestate.
|
|
|
|
type SamplingResult struct {
|
|
|
|
Decision SamplingDecision
|
|
|
|
Attributes []attribute.KeyValue
|
|
|
|
Tracestate trace.TraceState
|
|
|
|
}
|
|
|
|
|
|
|
|
type traceIDRatioSampler struct {
|
|
|
|
traceIDUpperBound uint64
|
|
|
|
description string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts traceIDRatioSampler) ShouldSample(p SamplingParameters) SamplingResult {
|
|
|
|
psc := trace.SpanContextFromContext(p.ParentContext)
|
2023-12-18 20:31:00 +00:00
|
|
|
x := binary.BigEndian.Uint64(p.TraceID[8:16]) >> 1
|
2021-08-09 07:19:24 +00:00
|
|
|
if x < ts.traceIDUpperBound {
|
|
|
|
return SamplingResult{
|
|
|
|
Decision: RecordAndSample,
|
|
|
|
Tracestate: psc.TraceState(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SamplingResult{
|
|
|
|
Decision: Drop,
|
|
|
|
Tracestate: psc.TraceState(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts traceIDRatioSampler) Description() string {
|
|
|
|
return ts.description
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2023-02-01 17:06:36 +00:00
|
|
|
//
|
|
|
|
//nolint:revive // revive complains about stutter of `trace.TraceIDRatioBased`
|
2021-08-09 07:19:24 +00:00
|
|
|
func TraceIDRatioBased(fraction float64) Sampler {
|
|
|
|
if fraction >= 1 {
|
|
|
|
return AlwaysSample()
|
|
|
|
}
|
|
|
|
|
|
|
|
if fraction <= 0 {
|
|
|
|
fraction = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return &traceIDRatioSampler{
|
|
|
|
traceIDUpperBound: uint64(fraction * (1 << 63)),
|
|
|
|
description: fmt.Sprintf("TraceIDRatioBased{%g}", fraction),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type alwaysOnSampler struct{}
|
|
|
|
|
|
|
|
func (as alwaysOnSampler) ShouldSample(p SamplingParameters) SamplingResult {
|
|
|
|
return SamplingResult{
|
|
|
|
Decision: RecordAndSample,
|
|
|
|
Tracestate: trace.SpanContextFromContext(p.ParentContext).TraceState(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as alwaysOnSampler) Description() string {
|
|
|
|
return "AlwaysOnSampler"
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 AlwaysSample() Sampler {
|
|
|
|
return alwaysOnSampler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type alwaysOffSampler struct{}
|
|
|
|
|
|
|
|
func (as alwaysOffSampler) ShouldSample(p SamplingParameters) SamplingResult {
|
|
|
|
return SamplingResult{
|
|
|
|
Decision: Drop,
|
|
|
|
Tracestate: trace.SpanContextFromContext(p.ParentContext).TraceState(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as alwaysOffSampler) Description() string {
|
|
|
|
return "AlwaysOffSampler"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeverSample returns a Sampler that samples no traces.
|
|
|
|
func NeverSample() Sampler {
|
|
|
|
return alwaysOffSampler{}
|
|
|
|
}
|
|
|
|
|
2023-12-21 16:20:48 +00:00
|
|
|
// ParentBased returns a sampler decorator which behaves differently,
|
2021-08-09 07:19:24 +00:00
|
|
|
// based on the parent of the span. If the span has no parent,
|
2023-12-21 16:20:48 +00:00
|
|
|
// the decorated sampler is used to make sampling decision. If the span has
|
2021-08-09 07:19:24 +00:00
|
|
|
// a parent, depending on whether the parent is remote and whether it
|
|
|
|
// is sampled, one of the following samplers will apply:
|
2023-12-18 20:31:00 +00:00
|
|
|
// - remoteParentSampled(Sampler) (default: AlwaysOn)
|
|
|
|
// - remoteParentNotSampled(Sampler) (default: AlwaysOff)
|
|
|
|
// - localParentSampled(Sampler) (default: AlwaysOn)
|
|
|
|
// - localParentNotSampled(Sampler) (default: AlwaysOff)
|
2021-08-09 07:19:24 +00:00
|
|
|
func ParentBased(root Sampler, samplers ...ParentBasedSamplerOption) Sampler {
|
|
|
|
return parentBased{
|
|
|
|
root: root,
|
|
|
|
config: configureSamplersForParentBased(samplers),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type parentBased struct {
|
|
|
|
root Sampler
|
2023-02-01 17:06:36 +00:00
|
|
|
config samplerConfig
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
func configureSamplersForParentBased(samplers []ParentBasedSamplerOption) samplerConfig {
|
|
|
|
c := samplerConfig{
|
2021-08-09 07:19:24 +00:00
|
|
|
remoteParentSampled: AlwaysSample(),
|
|
|
|
remoteParentNotSampled: NeverSample(),
|
|
|
|
localParentSampled: AlwaysSample(),
|
|
|
|
localParentNotSampled: NeverSample(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, so := range samplers {
|
2023-02-01 17:06:36 +00:00
|
|
|
c = so.apply(c)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// samplerConfig is a group of options for parentBased sampler.
|
|
|
|
type samplerConfig struct {
|
2021-08-09 07:19:24 +00:00
|
|
|
remoteParentSampled, remoteParentNotSampled Sampler
|
|
|
|
localParentSampled, localParentNotSampled Sampler
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParentBasedSamplerOption configures the sampler for a particular sampling case.
|
|
|
|
type ParentBasedSamplerOption interface {
|
2023-02-01 17:06:36 +00:00
|
|
|
apply(samplerConfig) samplerConfig
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithRemoteParentSampled sets the sampler for the case of sampled remote parent.
|
|
|
|
func WithRemoteParentSampled(s Sampler) ParentBasedSamplerOption {
|
|
|
|
return remoteParentSampledOption{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteParentSampledOption struct {
|
|
|
|
s Sampler
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
func (o remoteParentSampledOption) apply(config samplerConfig) samplerConfig {
|
2021-08-09 07:19:24 +00:00
|
|
|
config.remoteParentSampled = o.s
|
2023-02-01 17:06:36 +00:00
|
|
|
return config
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithRemoteParentNotSampled sets the sampler for the case of remote parent
|
|
|
|
// which is not sampled.
|
|
|
|
func WithRemoteParentNotSampled(s Sampler) ParentBasedSamplerOption {
|
|
|
|
return remoteParentNotSampledOption{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteParentNotSampledOption struct {
|
|
|
|
s Sampler
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
func (o remoteParentNotSampledOption) apply(config samplerConfig) samplerConfig {
|
2021-08-09 07:19:24 +00:00
|
|
|
config.remoteParentNotSampled = o.s
|
2023-02-01 17:06:36 +00:00
|
|
|
return config
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithLocalParentSampled sets the sampler for the case of sampled local parent.
|
|
|
|
func WithLocalParentSampled(s Sampler) ParentBasedSamplerOption {
|
|
|
|
return localParentSampledOption{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
type localParentSampledOption struct {
|
|
|
|
s Sampler
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
func (o localParentSampledOption) apply(config samplerConfig) samplerConfig {
|
2021-08-09 07:19:24 +00:00
|
|
|
config.localParentSampled = o.s
|
2023-02-01 17:06:36 +00:00
|
|
|
return config
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithLocalParentNotSampled sets the sampler for the case of local parent
|
|
|
|
// which is not sampled.
|
|
|
|
func WithLocalParentNotSampled(s Sampler) ParentBasedSamplerOption {
|
|
|
|
return localParentNotSampledOption{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
type localParentNotSampledOption struct {
|
|
|
|
s Sampler
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
func (o localParentNotSampledOption) apply(config samplerConfig) samplerConfig {
|
2021-08-09 07:19:24 +00:00
|
|
|
config.localParentNotSampled = o.s
|
2023-02-01 17:06:36 +00:00
|
|
|
return config
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pb parentBased) ShouldSample(p SamplingParameters) SamplingResult {
|
|
|
|
psc := trace.SpanContextFromContext(p.ParentContext)
|
|
|
|
if psc.IsValid() {
|
|
|
|
if psc.IsRemote() {
|
|
|
|
if psc.IsSampled() {
|
|
|
|
return pb.config.remoteParentSampled.ShouldSample(p)
|
|
|
|
}
|
|
|
|
return pb.config.remoteParentNotSampled.ShouldSample(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
if psc.IsSampled() {
|
|
|
|
return pb.config.localParentSampled.ShouldSample(p)
|
|
|
|
}
|
|
|
|
return pb.config.localParentNotSampled.ShouldSample(p)
|
|
|
|
}
|
|
|
|
return pb.root.ShouldSample(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb parentBased) Description() string {
|
|
|
|
return fmt.Sprintf("ParentBased{root:%s,remoteParentSampled:%s,"+
|
|
|
|
"remoteParentNotSampled:%s,localParentSampled:%s,localParentNotSampled:%s}",
|
|
|
|
pb.root.Description(),
|
|
|
|
pb.config.remoteParentSampled.Description(),
|
|
|
|
pb.config.remoteParentNotSampled.Description(),
|
|
|
|
pb.config.localParentSampled.Description(),
|
|
|
|
pb.config.localParentNotSampled.Description(),
|
|
|
|
)
|
|
|
|
}
|