2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-08-02 22:52:55 +02:00
|
|
|
//
|
|
|
|
// 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 (
|
2019-10-23 08:01:33 +02:00
|
|
|
"encoding/binary"
|
2020-03-10 17:25:11 +02:00
|
|
|
"fmt"
|
2019-10-23 08:01:33 +02:00
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2020-03-10 17:25:11 +02:00
|
|
|
api "go.opentelemetry.io/otel/api/trace"
|
2019-08-02 22:52:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sampler decides whether a trace should be sampled and exported.
|
2020-03-10 17:25:11 +02:00
|
|
|
type Sampler interface {
|
|
|
|
ShouldSample(SamplingParameters) SamplingResult
|
|
|
|
Description() string
|
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
|
|
|
|
// SamplingParameters contains the values passed to a Sampler.
|
|
|
|
type SamplingParameters struct {
|
|
|
|
ParentContext core.SpanContext
|
|
|
|
TraceID core.TraceID
|
2019-10-28 19:05:06 +02:00
|
|
|
SpanID core.SpanID
|
2019-08-02 22:52:55 +02:00
|
|
|
Name string
|
|
|
|
HasRemoteParent bool
|
2020-03-10 17:25:11 +02:00
|
|
|
Kind api.SpanKind
|
|
|
|
Attributes []core.KeyValue
|
|
|
|
Links []api.Link
|
|
|
|
}
|
|
|
|
|
|
|
|
// SamplingDecision indicates whether a span is recorded and sampled.
|
|
|
|
type SamplingDecision uint8
|
|
|
|
|
|
|
|
// Valid sampling decisions
|
|
|
|
const (
|
|
|
|
NotRecord SamplingDecision = iota
|
|
|
|
Record
|
|
|
|
RecordAndSampled
|
|
|
|
)
|
|
|
|
|
|
|
|
// SamplingResult conveys a SamplingDecision and a set of Attributes.
|
|
|
|
type SamplingResult struct {
|
|
|
|
Decision SamplingDecision
|
|
|
|
Attributes []core.KeyValue
|
|
|
|
}
|
|
|
|
|
|
|
|
type probabilitySampler struct {
|
|
|
|
traceIDUpperBound uint64
|
|
|
|
description string
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:25:11 +02:00
|
|
|
func (ps probabilitySampler) ShouldSample(p SamplingParameters) SamplingResult {
|
|
|
|
if p.ParentContext.IsSampled() {
|
|
|
|
return SamplingResult{Decision: RecordAndSampled}
|
|
|
|
}
|
|
|
|
|
|
|
|
x := binary.BigEndian.Uint64(p.TraceID[0:8]) >> 1
|
|
|
|
if x < ps.traceIDUpperBound {
|
|
|
|
return SamplingResult{Decision: RecordAndSampled}
|
|
|
|
}
|
|
|
|
return SamplingResult{Decision: NotRecord}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps probabilitySampler) Description() string {
|
|
|
|
return ps.description
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 01:53:50 +02:00
|
|
|
// ProbabilitySampler samples a given fraction of traces. Fractions >= 1 will
|
|
|
|
// always sample. If the parent span is sampled, then it's child spans will
|
2020-03-13 21:17:52 +02:00
|
|
|
// automatically be sampled. Fractions < 0 are treated as zero, but spans may
|
2019-10-30 01:53:50 +02:00
|
|
|
// still be sampled if their parent is.
|
2019-08-02 22:52:55 +02:00
|
|
|
func ProbabilitySampler(fraction float64) Sampler {
|
2019-10-30 01:53:50 +02:00
|
|
|
if fraction >= 1 {
|
2019-08-02 22:52:55 +02:00
|
|
|
return AlwaysSample()
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:53:50 +02:00
|
|
|
if fraction <= 0 {
|
|
|
|
fraction = 0
|
|
|
|
}
|
2020-03-10 17:25:11 +02:00
|
|
|
|
|
|
|
return &probabilitySampler{
|
|
|
|
traceIDUpperBound: uint64(fraction * (1 << 63)),
|
|
|
|
description: fmt.Sprintf("ProbabilitySampler{%g}", fraction),
|
2019-12-09 23:03:11 +02:00
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:25:11 +02:00
|
|
|
type alwaysOnSampler struct{}
|
|
|
|
|
|
|
|
func (as alwaysOnSampler) ShouldSample(p SamplingParameters) SamplingResult {
|
|
|
|
return SamplingResult{Decision: RecordAndSampled}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as alwaysOnSampler) Description() string {
|
|
|
|
return "AlwaysOnSampler"
|
|
|
|
}
|
|
|
|
|
2019-08-02 22:52:55 +02:00
|
|
|
// 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 {
|
2020-03-10 17:25:11 +02:00
|
|
|
return alwaysOnSampler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type alwaysOffSampler struct{}
|
|
|
|
|
|
|
|
func (as alwaysOffSampler) ShouldSample(p SamplingParameters) SamplingResult {
|
|
|
|
return SamplingResult{Decision: NotRecord}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as alwaysOffSampler) Description() string {
|
|
|
|
return "AlwaysOffSampler"
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NeverSample returns a Sampler that samples no traces.
|
|
|
|
func NeverSample() Sampler {
|
2020-03-10 17:25:11 +02:00
|
|
|
return alwaysOffSampler{}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
2020-02-03 20:22:52 +02:00
|
|
|
|
|
|
|
// AlwaysParentSample returns a Sampler that samples a trace only
|
|
|
|
// if the parent span is sampled.
|
|
|
|
// This Sampler is a passthrough to the ProbabilitySampler with
|
|
|
|
// a fraction of value 0.
|
|
|
|
func AlwaysParentSample() Sampler {
|
2020-03-10 17:25:11 +02:00
|
|
|
return &probabilitySampler{
|
|
|
|
traceIDUpperBound: 0,
|
|
|
|
description: "AlwaysParentSampler",
|
|
|
|
}
|
2020-02-03 20:22:52 +02:00
|
|
|
}
|