| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Copyright (c) 2021 The Jaeger Authors. |
| 5 |
// Copyright (c) 2017 Uber Technologies, Inc. |
| 6 |
// |
| 7 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
// you may not use this file except in compliance with the License. |
| 9 |
// You may obtain a copy of the License at |
| 10 |
// |
| 11 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
// |
| 13 |
// Unless required by applicable law or agreed to in writing, software |
| 14 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
// See the License for the specific language governing permissions and |
| 17 |
// limitations under the License. |
| 18 |
|
| 19 |
package jaegerremote // import "go.opentelemetry.io/contrib/samplers/jaegerremote" |
| 20 |
|
| 21 |
import ( |
| 22 |
"fmt" |
| 23 |
"os" |
| 24 |
"strconv" |
| 25 |
"strings" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/go-logr/logr" |
| 29 |
"go.opentelemetry.io/otel/sdk/trace" |
| 30 |
) |
| 31 |
|
| 32 |
type config struct { |
| 33 |
sampler trace.Sampler |
| 34 |
samplingServerURL string |
| 35 |
samplingRefreshInterval time.Duration |
| 36 |
samplingFetcher SamplingStrategyFetcher |
| 37 |
samplingParser samplingStrategyParser |
| 38 |
updaters []samplerUpdater |
| 39 |
posParams perOperationSamplerParams |
| 40 |
logger logr.Logger |
| 41 |
attributesDisabled bool |
| 42 |
} |
| 43 |
|
| 44 |
func getEnvOptions() ([]Option, []error) { |
| 45 |
var options []Option |
| 46 |
// list of errors which will be logged once logger is set by the user |
| 47 |
var errs []error |
| 48 |
|
| 49 |
rawEnvArgs := os.Getenv("OTEL_TRACES_SAMPLER_ARG") |
| 50 |
if rawEnvArgs == "" { |
| 51 |
return nil, nil |
| 52 |
} |
| 53 |
|
| 54 |
args := strings.SplitSeq(rawEnvArgs, ",") |
| 55 |
for arg := range args { |
| 56 |
keyValue := strings.Split(arg, "=") |
| 57 |
if len(keyValue) != 2 { |
| 58 |
errs = append(errs, fmt.Errorf("argument %s is not of type '<key>=<value>'", arg)) |
| 59 |
continue |
| 60 |
} |
| 61 |
key := strings.Trim(keyValue[0], " ") |
| 62 |
value := strings.Trim(keyValue[1], " ") |
| 63 |
|
| 64 |
switch key { |
| 65 |
case "endpoint": |
| 66 |
options = append(options, WithSamplingServerURL(value)) |
| 67 |
case "pollingIntervalMs": |
| 68 |
intervalMs, err := strconv.Atoi(value) |
| 69 |
if err != nil { |
| 70 |
errs = append(errs, fmt.Errorf("%s parsing failed with :%w", key, err)) |
| 71 |
continue |
| 72 |
} |
| 73 |
options = append(options, WithSamplingRefreshInterval(time.Duration(intervalMs)*time.Millisecond)) |
| 74 |
case "initialSamplingRate": |
| 75 |
samplingRate, err := strconv.ParseFloat(value, 64) |
| 76 |
if err != nil { |
| 77 |
errs = append(errs, fmt.Errorf("%s parsing failed with :%w", key, err)) |
| 78 |
continue |
| 79 |
} |
| 80 |
options = append(options, WithInitialSampler(trace.TraceIDRatioBased(samplingRate))) |
| 81 |
default: |
| 82 |
errs = append(errs, fmt.Errorf("invalid argument %s in OTEL_TRACE_SAMPLER_ARG", key)) |
| 83 |
} |
| 84 |
} |
| 85 |
return options, errs |
| 86 |
} |
| 87 |
|
| 88 |
// newConfig returns an appropriately configured config. |
| 89 |
func newConfig(options ...Option) config { |
| 90 |
c := config{ |
| 91 |
samplingServerURL: defaultSamplingServerURL, |
| 92 |
samplingRefreshInterval: defaultSamplingRefreshInterval, |
| 93 |
samplingFetcher: newHTTPSamplingStrategyFetcher(defaultSamplingServerURL), |
| 94 |
samplingParser: new(samplingStrategyParserImpl), |
| 95 |
posParams: perOperationSamplerParams{ |
| 96 |
MaxOperations: defaultSamplingMaxOperations, |
| 97 |
OperationNameLateBinding: defaultSamplingOperationNameLateBinding, |
| 98 |
}, |
| 99 |
logger: logr.Discard(), |
| 100 |
} |
| 101 |
|
| 102 |
envOptions, errs := getEnvOptions() |
| 103 |
for _, option := range envOptions { |
| 104 |
option.apply(&c) |
| 105 |
} |
| 106 |
|
| 107 |
for _, option := range options { |
| 108 |
option.apply(&c) |
| 109 |
} |
| 110 |
|
| 111 |
for _, err := range errs { |
| 112 |
c.logger.Error(err, "env variable parsing failure") |
| 113 |
} |
| 114 |
c.updaters = []samplerUpdater{ |
| 115 |
&perOperationSamplerUpdater{ |
| 116 |
MaxOperations: c.posParams.MaxOperations, |
| 117 |
OperationNameLateBinding: c.posParams.OperationNameLateBinding, |
| 118 |
attributesDisabled: c.attributesDisabled, |
| 119 |
}, |
| 120 |
&probabilisticSamplerUpdater{attributesDisabled: c.attributesDisabled}, |
| 121 |
&rateLimitingSamplerUpdater{attributesDisabled: c.attributesDisabled}, |
| 122 |
} |
| 123 |
|
| 124 |
if c.sampler == nil { |
| 125 |
c.sampler = newProbabilisticSampler(0.001, c.attributesDisabled) |
| 126 |
} |
| 127 |
|
| 128 |
return c |
| 129 |
} |
| 130 |
|
| 131 |
// Option applies configuration settings to a Sampler. |
| 132 |
type Option interface { |
| 133 |
apply(*config) |
| 134 |
} |
| 135 |
|
| 136 |
type optionFunc func(*config) |
| 137 |
|
| 138 |
func (fn optionFunc) apply(c *config) { |
| 139 |
fn(c) |
| 140 |
} |
| 141 |
|
| 142 |
// WithInitialSampler creates a Option that sets the initial sampler |
| 143 |
// to use before a remote sampler is created and used. |
| 144 |
func WithInitialSampler(sampler trace.Sampler) Option { |
| 145 |
return optionFunc(func(c *config) { |
| 146 |
c.sampler = sampler |
| 147 |
}) |
| 148 |
} |
| 149 |
|
| 150 |
// WithSamplingServerURL creates a Option that sets the sampling server url |
| 151 |
// of the local agent that contains the sampling strategies. |
| 152 |
func WithSamplingServerURL(samplingServerURL string) Option { |
| 153 |
return optionFunc(func(c *config) { |
| 154 |
c.samplingServerURL = samplingServerURL |
| 155 |
// 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 |
| 156 |
c.samplingFetcher = newHTTPSamplingStrategyFetcher(samplingServerURL) |
| 157 |
}) |
| 158 |
} |
| 159 |
|
| 160 |
// WithMaxOperations creates a Option that sets the maximum number of |
| 161 |
// operations the sampler will keep track of. |
| 162 |
func WithMaxOperations(maxOperations int) Option { |
| 163 |
return optionFunc(func(c *config) { |
| 164 |
c.posParams.MaxOperations = maxOperations |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
// WithOperationNameLateBinding creates a Option that sets the respective |
| 169 |
// field in the perOperationSamplerParams. |
| 170 |
func WithOperationNameLateBinding(enable bool) Option { |
| 171 |
return optionFunc(func(c *config) { |
| 172 |
c.posParams.OperationNameLateBinding = enable |
| 173 |
}) |
| 174 |
} |
| 175 |
|
| 176 |
// WithSamplingRefreshInterval creates a Option that sets how often the |
| 177 |
// sampler will poll local agent for the appropriate sampling strategy. |
| 178 |
func WithSamplingRefreshInterval(samplingRefreshInterval time.Duration) Option { |
| 179 |
return optionFunc(func(c *config) { |
| 180 |
c.samplingRefreshInterval = samplingRefreshInterval |
| 181 |
}) |
| 182 |
} |
| 183 |
|
| 184 |
// WithLogger configures the sampler to log operation and debug information with logger. |
| 185 |
func WithLogger(logger logr.Logger) Option { |
| 186 |
return optionFunc(func(c *config) { |
| 187 |
c.logger = logger |
| 188 |
}) |
| 189 |
} |
| 190 |
|
| 191 |
// WithSamplingStrategyFetcher creates an Option that initializes the sampling strategy fetcher. |
| 192 |
// Custom fetcher can be used for setting custom headers, timeouts, etc., or getting |
| 193 |
// sampling strategies from a different source, like files. |
| 194 |
func WithSamplingStrategyFetcher(fetcher SamplingStrategyFetcher) Option { |
| 195 |
return optionFunc(func(c *config) { |
| 196 |
c.samplingFetcher = fetcher |
| 197 |
}) |
| 198 |
} |
| 199 |
|
| 200 |
// WithAttributesDisabled configures the sampler to disable setting attributes jaeger.sampler.type and jaeger.sampler.param. |
| 201 |
func WithAttributesDisabled() Option { |
| 202 |
return optionFunc(func(c *config) { |
| 203 |
c.attributesDisabled = true |
| 204 |
}) |
| 205 |
} |
| 206 |
|
| 207 |
// samplingStrategyParser creates a Option that initializes sampling strategy parser. |
| 208 |
func withSamplingStrategyParser(parser samplingStrategyParser) Option { |
| 209 |
return optionFunc(func(c *config) { |
| 210 |
c.samplingParser = parser |
| 211 |
}) |
| 212 |
} |