/
fallback_sampler.go
102 lines (85 loc) · 3.3 KB
/
fallback_sampler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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 xray // import "go.opentelemetry.io/contrib/samplers/aws/xray"
import (
"sync"
"time"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
// FallbackSampler does the sampling at a rate of 1 req/sec and 5% of additional requests.
type FallbackSampler struct {
lastTick time.Time
quotaBalance float64
defaultSampler sdktrace.Sampler
mu sync.RWMutex
}
// Compile time assertion that remoteSampler implements the Sampler interface.
var _ sdktrace.Sampler = (*FallbackSampler)(nil)
// NewFallbackSampler returns a FallbackSampler which samples 1 req/sec and additional 5% of requests using traceIDRatioBasedSampler.
func NewFallbackSampler() *FallbackSampler {
return &FallbackSampler{
defaultSampler: sdktrace.TraceIDRatioBased(0.05),
quotaBalance: 1.0,
}
}
// ShouldSample implements the logic of borrowing 1 req/sec and then use traceIDRatioBasedSampler to sample 5% of additional requests.
func (fs *FallbackSampler) ShouldSample(parameters sdktrace.SamplingParameters) sdktrace.SamplingResult {
// borrowing one request every second
if fs.take(time.Now(), 1.0) {
return sdktrace.SamplingResult{
Tracestate: trace.SpanContextFromContext(parameters.ParentContext).TraceState(),
Decision: sdktrace.RecordAndSample,
}
}
// traceIDRatioBasedSampler to sample 5% of additional requests every second
return fs.defaultSampler.ShouldSample(parameters)
}
// Description returns description of the sampler being used.
func (fs *FallbackSampler) Description() string {
return "FallbackSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests}"
}
// take consumes quota from reservoir, if any remains, then returns true. False otherwise.
func (fs *FallbackSampler) take(now time.Time, itemCost float64) bool {
fs.mu.Lock()
defer fs.mu.Unlock()
if fs.lastTick.IsZero() {
fs.lastTick = now
}
if fs.quotaBalance >= itemCost {
fs.quotaBalance -= itemCost
return true
}
// update quota balance based on elapsed time
fs.refreshQuotaBalanceLocked(now)
if fs.quotaBalance >= itemCost {
fs.quotaBalance -= itemCost
return true
}
return false
}
// refreshQuotaBalanceLocked refreshes the quotaBalance considering elapsedTime.
// It is assumed the lock is held when calling this.
func (fs *FallbackSampler) refreshQuotaBalanceLocked(now time.Time) {
elapsedTime := now.Sub(fs.lastTick)
fs.lastTick = now
// when elapsedTime is higher than 1 even then we need to keep quotaBalance
// near to 1 so making elapsedTime to 1 for only borrowing 1 per second case
if elapsedTime.Seconds() > 1.0 {
fs.quotaBalance += 1.0
} else {
// calculate how much credit have we accumulated since the last tick
fs.quotaBalance += elapsedTime.Seconds()
}
}