Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/tracing/internal/jaegerremote/sampler_remote_options.go
4096 views
1
// Copyright The OpenTelemetry Authors
2
// Copyright (c) 2021 The Jaeger Authors.
3
// Copyright (c) 2017 Uber Technologies, Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
//nolint:all
18
package jaegerremote
19
20
import (
21
"time"
22
23
"go.opentelemetry.io/otel/sdk/trace"
24
)
25
26
type config struct {
27
sampler trace.Sampler
28
samplingServerURL string
29
samplingRefreshInterval time.Duration
30
samplingFetcher samplingStrategyFetcher
31
samplingParser samplingStrategyParser
32
updaters []samplerUpdater
33
posParams perOperationSamplerParams
34
}
35
36
// newConfig returns an appropriately configured config.
37
func newConfig(options ...Option) config {
38
c := config{
39
sampler: newProbabilisticSampler(0.001),
40
samplingServerURL: defaultSamplingServerURL,
41
samplingRefreshInterval: defaultSamplingRefreshInterval,
42
samplingFetcher: newHTTPSamplingStrategyFetcher(defaultSamplingServerURL),
43
samplingParser: new(samplingStrategyParserImpl),
44
updaters: []samplerUpdater{
45
new(probabilisticSamplerUpdater),
46
new(rateLimitingSamplerUpdater),
47
},
48
posParams: perOperationSamplerParams{
49
MaxOperations: defaultSamplingMaxOperations,
50
OperationNameLateBinding: defaultSamplingOperationNameLateBinding,
51
},
52
}
53
for _, option := range options {
54
option.apply(&c)
55
}
56
c.updaters = append([]samplerUpdater{&perOperationSamplerUpdater{
57
MaxOperations: c.posParams.MaxOperations,
58
OperationNameLateBinding: c.posParams.OperationNameLateBinding,
59
}}, c.updaters...)
60
return c
61
}
62
63
// Option applies configuration settings to a Sampler.
64
type Option interface {
65
apply(*config)
66
}
67
68
type optionFunc func(*config)
69
70
func (fn optionFunc) apply(c *config) {
71
fn(c)
72
}
73
74
// WithInitialSampler creates a Option that sets the initial sampler
75
// to use before a remote sampler is created and used.
76
func WithInitialSampler(sampler trace.Sampler) Option {
77
return optionFunc(func(c *config) {
78
c.sampler = sampler
79
})
80
}
81
82
// WithSamplingServerURL creates a Option that sets the sampling server url
83
// of the local agent that contains the sampling strategies.
84
func WithSamplingServerURL(samplingServerURL string) Option {
85
return optionFunc(func(c *config) {
86
c.samplingServerURL = samplingServerURL
87
// The default port of jaeger agent is 5778, but there are other ports specified by the user, so the sampling address and fetch address are strongly bound
88
c.samplingFetcher = newHTTPSamplingStrategyFetcher(samplingServerURL)
89
})
90
}
91
92
// WithMaxOperations creates a Option that sets the maximum number of
93
// operations the sampler will keep track of.
94
func WithMaxOperations(maxOperations int) Option {
95
return optionFunc(func(c *config) {
96
c.posParams.MaxOperations = maxOperations
97
})
98
}
99
100
// WithOperationNameLateBinding creates a Option that sets the respective
101
// field in the perOperationSamplerParams.
102
func WithOperationNameLateBinding(enable bool) Option {
103
return optionFunc(func(c *config) {
104
c.posParams.OperationNameLateBinding = enable
105
})
106
}
107
108
// WithSamplingRefreshInterval creates a Option that sets how often the
109
// sampler will poll local agent for the appropriate sampling strategy.
110
func WithSamplingRefreshInterval(samplingRefreshInterval time.Duration) Option {
111
return optionFunc(func(c *config) {
112
c.samplingRefreshInterval = samplingRefreshInterval
113
})
114
}
115
116
// samplingStrategyFetcher creates a Option that initializes sampling strategy fetcher.
117
func withSamplingStrategyFetcher(fetcher samplingStrategyFetcher) Option {
118
return optionFunc(func(c *config) {
119
c.samplingFetcher = fetcher
120
})
121
}
122
123
// samplingStrategyParser creates a Option that initializes sampling strategy parser.
124
func withSamplingStrategyParser(parser samplingStrategyParser) Option {
125
return optionFunc(func(c *config) {
126
c.samplingParser = parser
127
})
128
}
129
130
// withUpdaters creates a Option that initializes sampler updaters.
131
func withUpdaters(updaters ...samplerUpdater) Option {
132
return optionFunc(func(c *config) {
133
c.updaters = updaters
134
})
135
}
136
137