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
|
|
|
|
2020-03-10 17:25:11 +02:00
|
|
|
api "go.opentelemetry.io/otel/api/trace"
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
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 {
|
2020-05-02 14:17:11 +02:00
|
|
|
ParentContext api.SpanContext
|
2020-05-02 14:23:09 +02:00
|
|
|
TraceID api.ID
|
2019-08-02 22:52:55 +02:00
|
|
|
Name string
|
|
|
|
HasRemoteParent bool
|
2020-03-10 17:25:11 +02:00
|
|
|
Kind api.SpanKind
|
2020-08-18 05:25:03 +02:00
|
|
|
Attributes []label.KeyValue
|
2020-03-10 17:25:11 +02:00
|
|
|
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
|
2020-08-18 05:25:03 +02:00
|
|
|
Attributes []label.KeyValue
|
2020-03-10 17:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-06-11 17:27:21 +02:00
|
|
|
// ParentSample returns a Sampler that samples a trace only
|
|
|
|
// if the the span has a parent span and it is sampled. If the span has
|
|
|
|
// parent span but it is not sampled, neither will this span. If the span
|
|
|
|
// does not have a parent the fallback Sampler is used to determine if the
|
|
|
|
// span should be sampled.
|
|
|
|
func ParentSample(fallback Sampler) Sampler {
|
|
|
|
return parentSampler{fallback}
|
|
|
|
}
|
|
|
|
|
|
|
|
type parentSampler struct {
|
|
|
|
fallback Sampler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps parentSampler) ShouldSample(p SamplingParameters) SamplingResult {
|
|
|
|
if p.ParentContext.IsValid() {
|
|
|
|
if p.ParentContext.IsSampled() {
|
|
|
|
return SamplingResult{Decision: RecordAndSampled}
|
|
|
|
}
|
|
|
|
return SamplingResult{Decision: NotRecord}
|
2020-03-10 17:25:11 +02:00
|
|
|
}
|
2020-06-11 17:27:21 +02:00
|
|
|
return ps.fallback.ShouldSample(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps parentSampler) Description() string {
|
|
|
|
return fmt.Sprintf("ParentOrElse{%s}", ps.fallback.Description())
|
2020-02-03 20:22:52 +02:00
|
|
|
}
|