-
Notifications
You must be signed in to change notification settings - Fork 52
/
sampler.go
72 lines (61 loc) · 2.27 KB
/
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
package opencensus
import (
"net/http"
"strings"
"flamingo.me/flamingo/v3/framework/config"
"go.opencensus.io/trace"
)
// URLPrefixSampler creates a sampling getter for ochttp.Server.
//
// If the whitelist is empty it is treated as allowed, otherwise checked first.
// If the blacklist is set it will disable sampling again.
// If takeParentDecision is set we allow the decision to be taken from incoming tracing,
// otherwise we enforce our decision
func URLPrefixSampler(whitelist, blacklist []string, allowParentTrace bool) func(*http.Request) trace.StartOptions {
return func(request *http.Request) trace.StartOptions {
path := request.URL.Path
// empty whitelist means all
sample := len(whitelist) == 0
// check whitelist if len is > 0, and decide if we should sample
for _, p := range whitelist {
if strings.HasPrefix(path, p) {
sample = true
break
}
}
// we do not sample, unless the parent is sampled
if !sample {
return trace.StartOptions{
Sampler: func(p trace.SamplingParameters) trace.SamplingDecision {
return trace.SamplingDecision{Sample: !allowParentTrace && p.ParentContext.IsSampled()}
},
}
}
// check sampling decision agains blacklist
for _, p := range blacklist {
if strings.HasPrefix(path, p) {
sample = false
break
}
}
// we sample, or the parent sampled
return trace.StartOptions{
Sampler: func(p trace.SamplingParameters) trace.SamplingDecision {
return trace.SamplingDecision{Sample: (!allowParentTrace && p.ParentContext.IsSampled()) || sample}
},
}
}
}
// ConfiguredURLPrefixSampler constructs the prefix GetStartOptions getter with the default opencensus configuration
type ConfiguredURLPrefixSampler struct {
Whitelist config.Slice `inject:"config:opencensus.tracing.sampler.whitelist"`
Blacklist config.Slice `inject:"config:opencensus.tracing.sampler.blacklist"`
AllowParentTrace bool `inject:"config:opencensus.tracing.sampler.allowParentTrace"`
}
// GetStartOptions constructor for ochttp.Server
func (c *ConfiguredURLPrefixSampler) GetStartOptions() func(*http.Request) trace.StartOptions {
var whitelist, blacklist []string
c.Whitelist.MapInto(&whitelist)
c.Blacklist.MapInto(&blacklist)
return URLPrefixSampler(whitelist, blacklist, c.AllowParentTrace)
}