| 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 |
| 20 |
|
| 21 |
import ( |
| 22 |
"encoding/binary" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"os" |
| 26 |
"sync" |
| 27 |
"testing" |
| 28 |
"time" |
| 29 |
|
| 30 |
"github.com/go-logr/logr/testr" |
| 31 |
jaeger_api_v2 "github.com/jaegertracing/jaeger-idl/proto-gen/api_v2" |
| 32 |
"github.com/stretchr/testify/assert" |
| 33 |
"github.com/stretchr/testify/require" |
| 34 |
"go.opentelemetry.io/otel/sdk/trace" |
| 35 |
oteltrace "go.opentelemetry.io/otel/trace" |
| 36 |
|
| 37 |
"go.opentelemetry.io/contrib/samplers/jaegerremote/internal/testutils" |
| 38 |
) |
| 39 |
|
| 40 |
func TestRemotelyControlledSampler_updateConcurrentSafe(*testing.T) { |
| 41 |
initSampler := newProbabilisticSampler(0.123, false) |
| 42 |
fetcher := &testSamplingStrategyFetcher{response: []byte("probabilistic")} |
| 43 |
parser := new(testSamplingStrategyParser) |
| 44 |
sampler := New( |
| 45 |
"test", |
| 46 |
WithMaxOperations(42), |
| 47 |
WithOperationNameLateBinding(true), |
| 48 |
WithInitialSampler(initSampler), |
| 49 |
WithSamplingServerURL("my url"), |
| 50 |
WithSamplingRefreshInterval(time.Millisecond), |
| 51 |
WithSamplingStrategyFetcher(fetcher), |
| 52 |
withSamplingStrategyParser(parser), |
| 53 |
) |
| 54 |
|
| 55 |
defer sampler.Close() |
| 56 |
|
| 57 |
s := makeSamplingParameters(1, "test") |
| 58 |
|
| 59 |
var wg sync.WaitGroup |
| 60 |
|
| 61 |
wg.Go(func() { |
| 62 |
sampler.UpdateSampler() |
| 63 |
}) |
| 64 |
|
| 65 |
wg.Go(func() { |
| 66 |
sampler.ShouldSample(s) |
| 67 |
}) |
| 68 |
|
| 69 |
wg.Wait() |
| 70 |
} |
| 71 |
|
| 72 |
type testSamplingStrategyFetcher struct { |
| 73 |
response []byte |
| 74 |
} |
| 75 |
|
| 76 |
func (c *testSamplingStrategyFetcher) Fetch(string) ([]byte, error) { |
| 77 |
return c.response, nil |
| 78 |
} |
| 79 |
|
| 80 |
type testSamplingStrategyParser struct{} |
| 81 |
|
| 82 |
func (*testSamplingStrategyParser) Parse(response []byte) (any, error) { |
| 83 |
strategy := new(jaeger_api_v2.SamplingStrategyResponse) |
| 84 |
|
| 85 |
switch string(response) { |
| 86 |
case "probabilistic": |
| 87 |
strategy.StrategyType = jaeger_api_v2.SamplingStrategyType_PROBABILISTIC |
| 88 |
strategy.ProbabilisticSampling = &jaeger_api_v2.ProbabilisticSamplingStrategy{ |
| 89 |
SamplingRate: 0.85, |
| 90 |
} |
| 91 |
return strategy, nil |
| 92 |
case "rateLimiting": |
| 93 |
strategy.StrategyType = jaeger_api_v2.SamplingStrategyType_RATE_LIMITING |
| 94 |
strategy.RateLimitingSampling = &jaeger_api_v2.RateLimitingSamplingStrategy{ |
| 95 |
MaxTracesPerSecond: 100, |
| 96 |
} |
| 97 |
return strategy, nil |
| 98 |
} |
| 99 |
|
| 100 |
return nil, errors.New("unknown strategy test request") |
| 101 |
} |
| 102 |
|
| 103 |
func TestRemoteSamplerOptions(t *testing.T) { |
| 104 |
initSampler := newProbabilisticSampler(0.123, false) |
| 105 |
fetcher := new(fakeSamplingFetcher) |
| 106 |
parser := new(samplingStrategyParserImpl) |
| 107 |
logger := testr.New(t) |
| 108 |
sampler := New( |
| 109 |
"test", |
| 110 |
WithMaxOperations(42), |
| 111 |
WithOperationNameLateBinding(true), |
| 112 |
WithInitialSampler(initSampler), |
| 113 |
WithSamplingServerURL("my url"), |
| 114 |
WithSamplingRefreshInterval(42*time.Second), |
| 115 |
WithSamplingStrategyFetcher(fetcher), |
| 116 |
withSamplingStrategyParser(parser), |
| 117 |
WithLogger(logger), |
| 118 |
WithAttributesDisabled(), |
| 119 |
) |
| 120 |
defer sampler.Close() |
| 121 |
assert.Equal(t, 42, sampler.posParams.MaxOperations) |
| 122 |
assert.True(t, sampler.posParams.OperationNameLateBinding) |
| 123 |
assert.Same(t, initSampler, sampler.sampler) |
| 124 |
assert.Equal(t, "my url", sampler.samplingServerURL) |
| 125 |
assert.Equal(t, 42*time.Second, sampler.samplingRefreshInterval) |
| 126 |
assert.Same(t, fetcher, sampler.samplingFetcher) |
| 127 |
assert.Same(t, parser, sampler.samplingParser) |
| 128 |
assert.EqualValues(t, &perOperationSamplerUpdater{MaxOperations: 42, OperationNameLateBinding: true, attributesDisabled: true}, sampler.updaters[0]) |
| 129 |
assert.Equal(t, logger, sampler.logger) |
| 130 |
assert.True(t, sampler.attributesDisabled) |
| 131 |
} |
| 132 |
|
| 133 |
func TestRemoteSamplerOptionsDefaults(t *testing.T) { |
| 134 |
options := newConfig() |
| 135 |
sampler, ok := options.sampler.(*probabilisticSampler) |
| 136 |
assert.True(t, ok) |
| 137 |
assert.Equal(t, 0.001, sampler.samplingRate) |
| 138 |
|
| 139 |
assert.NotEmpty(t, options.samplingServerURL) |
| 140 |
assert.NotZero(t, options.samplingRefreshInterval) |
| 141 |
} |
| 142 |
|
| 143 |
func initAgent(t *testing.T) (*testutils.MockAgent, *Sampler) { |
| 144 |
agent, err := testutils.StartMockAgent() |
| 145 |
require.NoError(t, err) |
| 146 |
|
| 147 |
initialSampler := newProbabilisticSampler(0.001, false) |
| 148 |
sampler := New( |
| 149 |
"client app", |
| 150 |
WithSamplingServerURL("http://"+agent.SamplingServerAddr()), |
| 151 |
WithMaxOperations(testDefaultMaxOperations), |
| 152 |
WithInitialSampler(initialSampler), |
| 153 |
WithSamplingRefreshInterval(time.Minute), |
| 154 |
) |
| 155 |
sampler.Close() // stop timer-based updates, we want to call them manually |
| 156 |
|
| 157 |
return agent, sampler |
| 158 |
} |
| 159 |
|
| 160 |
func makeSamplingParameters(id uint64, operationName string) trace.SamplingParameters { |
| 161 |
var traceID oteltrace.TraceID |
| 162 |
binary.BigEndian.PutUint64(traceID[8:], id) |
| 163 |
|
| 164 |
return trace.SamplingParameters{ |
| 165 |
TraceID: traceID, |
| 166 |
Name: operationName, |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func TestRemotelyControlledSampler(t *testing.T) { |
| 171 |
agent, remoteSampler := initAgent(t) |
| 172 |
defer agent.Close() |
| 173 |
|
| 174 |
defaultSampler := newProbabilisticSampler(0.001, false) |
| 175 |
remoteSampler.setSampler(defaultSampler) |
| 176 |
|
| 177 |
agent.AddSamplingStrategy("client app", |
| 178 |
getSamplingStrategyResponse(jaeger_api_v2.SamplingStrategyType_PROBABILISTIC, testDefaultSamplingProbability)) |
| 179 |
remoteSampler.UpdateSampler() |
| 180 |
s1, ok := remoteSampler.sampler.(*probabilisticSampler) |
| 181 |
assert.True(t, ok) |
| 182 |
assert.Equal(t, testDefaultSamplingProbability, s1.samplingRate, "Sampler should have been updated") |
| 183 |
|
| 184 |
result := remoteSampler.ShouldSample(makeSamplingParameters(testMaxID+10, testOperationName)) |
| 185 |
assert.Equal(t, trace.Drop, result.Decision) |
| 186 |
result = remoteSampler.ShouldSample(makeSamplingParameters(testMaxID-10, testOperationName)) |
| 187 |
assert.Equal(t, trace.RecordAndSample, result.Decision) |
| 188 |
|
| 189 |
remoteSampler.setSampler(defaultSampler) |
| 190 |
|
| 191 |
c := make(chan time.Time) |
| 192 |
ticker := &time.Ticker{C: c} |
| 193 |
// reset closed so the next call to Close() correctly stops the polling goroutine |
| 194 |
remoteSampler.closed = 0 |
| 195 |
done := make(chan struct{}) |
| 196 |
go func() { |
| 197 |
defer close(done) |
| 198 |
remoteSampler.pollControllerWithTicker(ticker) |
| 199 |
}() |
| 200 |
|
| 201 |
c <- time.Now() // force update based on timer |
| 202 |
remoteSampler.Close() |
| 203 |
<-done |
| 204 |
|
| 205 |
s2, ok := remoteSampler.sampler.(*probabilisticSampler) |
| 206 |
assert.True(t, ok) |
| 207 |
assert.Equal(t, testDefaultSamplingProbability, s2.samplingRate, "Sampler should have been updated from timer") |
| 208 |
} |
| 209 |
|
| 210 |
func TestRemotelyControlledSampler_updateSampler(t *testing.T) { |
| 211 |
tests := []struct { |
| 212 |
probabilities map[string]float64 |
| 213 |
defaultProbability float64 |
| 214 |
expectedDefaultProbability float64 |
| 215 |
}{ |
| 216 |
{ |
| 217 |
probabilities: map[string]float64{testOperationName: 1.1}, |
| 218 |
defaultProbability: testDefaultSamplingProbability, |
| 219 |
expectedDefaultProbability: testDefaultSamplingProbability, |
| 220 |
}, |
| 221 |
{ |
| 222 |
probabilities: map[string]float64{testOperationName: testDefaultSamplingProbability}, |
| 223 |
defaultProbability: testDefaultSamplingProbability, |
| 224 |
expectedDefaultProbability: testDefaultSamplingProbability, |
| 225 |
}, |
| 226 |
{ |
| 227 |
probabilities: map[string]float64{ |
| 228 |
testOperationName: testDefaultSamplingProbability, |
| 229 |
testFirstTimeOperationName: testDefaultSamplingProbability, |
| 230 |
}, |
| 231 |
defaultProbability: testDefaultSamplingProbability, |
| 232 |
expectedDefaultProbability: testDefaultSamplingProbability, |
| 233 |
}, |
| 234 |
{ |
| 235 |
probabilities: map[string]float64{"new op": 1.1}, |
| 236 |
defaultProbability: testDefaultSamplingProbability, |
| 237 |
expectedDefaultProbability: testDefaultSamplingProbability, |
| 238 |
}, |
| 239 |
{ |
| 240 |
probabilities: map[string]float64{"new op": 1.1}, |
| 241 |
defaultProbability: 1.1, |
| 242 |
expectedDefaultProbability: 1.0, |
| 243 |
}, |
| 244 |
} |
| 245 |
|
| 246 |
for i, test := range tests { |
| 247 |
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) { |
| 248 |
agent, sampler := initAgent(t) |
| 249 |
defer agent.Close() |
| 250 |
|
| 251 |
initSampler, ok := sampler.sampler.(*probabilisticSampler) |
| 252 |
assert.True(t, ok) |
| 253 |
|
| 254 |
res := &jaeger_api_v2.SamplingStrategyResponse{ |
| 255 |
StrategyType: jaeger_api_v2.SamplingStrategyType_PROBABILISTIC, |
| 256 |
OperationSampling: &jaeger_api_v2.PerOperationSamplingStrategies{ |
| 257 |
DefaultSamplingProbability: test.defaultProbability, |
| 258 |
DefaultLowerBoundTracesPerSecond: 0.001, |
| 259 |
}, |
| 260 |
} |
| 261 |
for opName, prob := range test.probabilities { |
| 262 |
res.OperationSampling.PerOperationStrategies = append(res.OperationSampling.PerOperationStrategies, |
| 263 |
&jaeger_api_v2.OperationSamplingStrategy{ |
| 264 |
Operation: opName, |
| 265 |
ProbabilisticSampling: &jaeger_api_v2.ProbabilisticSamplingStrategy{ |
| 266 |
SamplingRate: prob, |
| 267 |
}, |
| 268 |
}, |
| 269 |
) |
| 270 |
} |
| 271 |
|
| 272 |
agent.AddSamplingStrategy("client app", res) |
| 273 |
sampler.UpdateSampler() |
| 274 |
|
| 275 |
s, ok := sampler.sampler.(*perOperationSampler) |
| 276 |
assert.True(t, ok) |
| 277 |
assert.NotEqual(t, initSampler, sampler.sampler, "Sampler should have been updated") |
| 278 |
assert.Equal(t, test.expectedDefaultProbability, s.defaultSampler.SamplingRate()) |
| 279 |
|
| 280 |
// First call is always sampled |
| 281 |
result := sampler.ShouldSample(makeSamplingParameters(testMaxID+10, testOperationName)) |
| 282 |
assert.Equal(t, trace.RecordAndSample, result.Decision) |
| 283 |
|
| 284 |
result = sampler.ShouldSample(makeSamplingParameters(testMaxID-10, testOperationName)) |
| 285 |
assert.Equal(t, trace.RecordAndSample, result.Decision) |
| 286 |
}) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func TestRemotelyControlledSampler_ImmediatelyUpdateOnStartup(t *testing.T) { |
| 291 |
initSampler := newProbabilisticSampler(0.123, false) |
| 292 |
fetcher := &testSamplingStrategyFetcher{response: []byte("rateLimiting")} |
| 293 |
parser := new(testSamplingStrategyParser) |
| 294 |
sampler := New( |
| 295 |
"test", |
| 296 |
WithMaxOperations(42), |
| 297 |
WithOperationNameLateBinding(true), |
| 298 |
WithInitialSampler(initSampler), |
| 299 |
WithSamplingServerURL("my url"), |
| 300 |
WithSamplingRefreshInterval(10*time.Minute), |
| 301 |
WithSamplingStrategyFetcher(fetcher), |
| 302 |
withSamplingStrategyParser(parser), |
| 303 |
) |
| 304 |
time.Sleep(100 * time.Millisecond) // waiting for s.pollController |
| 305 |
sampler.Close() // stop pollController, avoid date race |
| 306 |
s, ok := sampler.sampler.(*rateLimitingSampler) |
| 307 |
assert.True(t, ok) |
| 308 |
assert.Equal(t, float64(100), s.maxTracesPerSecond) |
| 309 |
} |
| 310 |
|
| 311 |
func TestRemotelyControlledSampler_multiStrategyResponse(t *testing.T) { |
| 312 |
agent, sampler := initAgent(t) |
| 313 |
defer agent.Close() |
| 314 |
initSampler, ok := sampler.sampler.(*probabilisticSampler) |
| 315 |
assert.True(t, ok) |
| 316 |
|
| 317 |
defaultSampingRate := 1.0 |
| 318 |
testUnusedOpName := "unused_op" |
| 319 |
testUnusedOpSamplingRate := 0.0 |
| 320 |
|
| 321 |
res := &jaeger_api_v2.SamplingStrategyResponse{ |
| 322 |
StrategyType: jaeger_api_v2.SamplingStrategyType_PROBABILISTIC, |
| 323 |
ProbabilisticSampling: &jaeger_api_v2.ProbabilisticSamplingStrategy{SamplingRate: defaultSampingRate}, |
| 324 |
OperationSampling: &jaeger_api_v2.PerOperationSamplingStrategies{ |
| 325 |
DefaultSamplingProbability: defaultSampingRate, |
| 326 |
DefaultLowerBoundTracesPerSecond: 0.001, |
| 327 |
PerOperationStrategies: []*jaeger_api_v2.OperationSamplingStrategy{ |
| 328 |
{ |
| 329 |
Operation: testUnusedOpName, |
| 330 |
ProbabilisticSampling: &jaeger_api_v2.ProbabilisticSamplingStrategy{ |
| 331 |
SamplingRate: testUnusedOpSamplingRate, |
| 332 |
}, |
| 333 |
}, |
| 334 |
}, |
| 335 |
}, |
| 336 |
} |
| 337 |
|
| 338 |
agent.AddSamplingStrategy("client app", res) |
| 339 |
sampler.UpdateSampler() |
| 340 |
s, ok := sampler.sampler.(*perOperationSampler) |
| 341 |
assert.True(t, ok) |
| 342 |
assert.NotEqual(t, initSampler, sampler.sampler, "Sampler should have been updated") |
| 343 |
assert.Equal(t, defaultSampingRate, s.defaultSampler.SamplingRate()) |
| 344 |
|
| 345 |
result := sampler.ShouldSample(makeSamplingParameters(testMaxID-10, testUnusedOpName)) |
| 346 |
assert.Equal(t, trace.RecordAndSample, result.Decision) // first call always pass |
| 347 |
result = sampler.ShouldSample(makeSamplingParameters(testMaxID, testUnusedOpName)) |
| 348 |
assert.Equal(t, trace.Drop, result.Decision) |
| 349 |
} |
| 350 |
|
| 351 |
func TestSamplerQueryError(t *testing.T) { |
| 352 |
agent, sampler := initAgent(t) |
| 353 |
defer agent.Close() |
| 354 |
|
| 355 |
// override the actual handler |
| 356 |
sampler.samplingFetcher = &fakeSamplingFetcher{} |
| 357 |
|
| 358 |
initSampler, ok := sampler.sampler.(*probabilisticSampler) |
| 359 |
assert.True(t, ok) |
| 360 |
|
| 361 |
sampler.Close() // stop timer-based updates, we want to call them manually |
| 362 |
|
| 363 |
sampler.UpdateSampler() |
| 364 |
assert.Equal(t, initSampler, sampler.sampler, "Sampler should not have been updated due to query error") |
| 365 |
} |
| 366 |
|
| 367 |
type fakeSamplingFetcher struct{} |
| 368 |
|
| 369 |
func (*fakeSamplingFetcher) Fetch(string) ([]byte, error) { |
| 370 |
return nil, errors.New("query error") |
| 371 |
} |
| 372 |
|
| 373 |
func TestRemotelyControlledSampler_updateSamplerFromAdaptiveSampler(t *testing.T) { |
| 374 |
agent, remoteSampler := initAgent(t) |
| 375 |
defer agent.Close() |
| 376 |
remoteSampler.Close() // close the second time (initAgent already called Close) |
| 377 |
|
| 378 |
strategies := &jaeger_api_v2.PerOperationSamplingStrategies{ |
| 379 |
DefaultSamplingProbability: testDefaultSamplingProbability, |
| 380 |
DefaultLowerBoundTracesPerSecond: 1.0, |
| 381 |
} |
| 382 |
adaptiveSampler := newPerOperationSampler(perOperationSamplerParams{ |
| 383 |
MaxOperations: testDefaultMaxOperations, |
| 384 |
Strategies: strategies, |
| 385 |
}, false) |
| 386 |
|
| 387 |
// Overwrite the sampler with an adaptive sampler |
| 388 |
remoteSampler.setSampler(adaptiveSampler) |
| 389 |
|
| 390 |
agent.AddSamplingStrategy("client app", |
| 391 |
getSamplingStrategyResponse(jaeger_api_v2.SamplingStrategyType_PROBABILISTIC, 0.5)) |
| 392 |
remoteSampler.UpdateSampler() |
| 393 |
|
| 394 |
// Sampler should have been updated to probabilistic |
| 395 |
_, ok := remoteSampler.sampler.(*probabilisticSampler) |
| 396 |
require.True(t, ok) |
| 397 |
|
| 398 |
// Overwrite the sampler with an adaptive sampler |
| 399 |
remoteSampler.setSampler(adaptiveSampler) |
| 400 |
|
| 401 |
agent.AddSamplingStrategy("client app", |
| 402 |
getSamplingStrategyResponse(jaeger_api_v2.SamplingStrategyType_RATE_LIMITING, 1)) |
| 403 |
remoteSampler.UpdateSampler() |
| 404 |
|
| 405 |
// Sampler should have been updated to ratelimiting |
| 406 |
_, ok = remoteSampler.sampler.(*rateLimitingSampler) |
| 407 |
require.True(t, ok) |
| 408 |
|
| 409 |
// Overwrite the sampler with an adaptive sampler |
| 410 |
remoteSampler.setSampler(adaptiveSampler) |
| 411 |
|
| 412 |
// Update existing adaptive sampler |
| 413 |
agent.AddSamplingStrategy("client app", &jaeger_api_v2.SamplingStrategyResponse{OperationSampling: strategies}) |
| 414 |
remoteSampler.UpdateSampler() |
| 415 |
} |
| 416 |
|
| 417 |
func TestRemotelyControlledSampler_updateRateLimitingOrProbabilisticSampler(t *testing.T) { |
| 418 |
probabilisticSampler := newProbabilisticSampler(0.002, false) |
| 419 |
otherProbabilisticSampler := newProbabilisticSampler(0.003, false) |
| 420 |
maxProbabilisticSampler := newProbabilisticSampler(1.0, false) |
| 421 |
|
| 422 |
rateLimitingSampler := newRateLimitingSampler(2, false) |
| 423 |
otherRateLimitingSampler := newRateLimitingSampler(3, false) |
| 424 |
|
| 425 |
testCases := []struct { |
| 426 |
res *jaeger_api_v2.SamplingStrategyResponse |
| 427 |
initSampler trace.Sampler |
| 428 |
expectedSampler trace.Sampler |
| 429 |
shouldErr bool |
| 430 |
referenceEquivalence bool |
| 431 |
caption string |
| 432 |
}{ |
| 433 |
{ |
| 434 |
res: getSamplingStrategyResponse(jaeger_api_v2.SamplingStrategyType_PROBABILISTIC, 1.5), |
| 435 |
initSampler: probabilisticSampler, |
| 436 |
expectedSampler: maxProbabilisticSampler, |
| 437 |
shouldErr: true, |
| 438 |
referenceEquivalence: false, |
| 439 |
caption: "invalid probabilistic strategy", |
| 440 |
}, |
| 441 |
{ |
| 442 |
res: getSamplingStrategyResponse(jaeger_api_v2.SamplingStrategyType_PROBABILISTIC, 0.002), |
| 443 |
initSampler: probabilisticSampler, |
| 444 |
expectedSampler: probabilisticSampler, |
| 445 |
shouldErr: false, |
| 446 |
referenceEquivalence: true, |
| 447 |
caption: "unchanged probabilistic strategy", |
| 448 |
}, |
| 449 |
{ |
| 450 |
res: getSamplingStrategyResponse(jaeger_api_v2.SamplingStrategyType_PROBABILISTIC, 0.003), |
| 451 |
initSampler: probabilisticSampler, |
| 452 |
expectedSampler: otherProbabilisticSampler, |
| 453 |
shouldErr: false, |
| 454 |
referenceEquivalence: false, |
| 455 |
caption: "valid probabilistic strategy", |
| 456 |
}, |
| 457 |
{ |
| 458 |
res: getSamplingStrategyResponse(jaeger_api_v2.SamplingStrategyType_RATE_LIMITING, 2), |
| 459 |
initSampler: rateLimitingSampler, |
| 460 |
expectedSampler: rateLimitingSampler, |
| 461 |
shouldErr: false, |
| 462 |
referenceEquivalence: true, |
| 463 |
caption: "unchanged rate limiting strategy", |
| 464 |
}, |
| 465 |
{ |
| 466 |
res: getSamplingStrategyResponse(jaeger_api_v2.SamplingStrategyType_RATE_LIMITING, 3), |
| 467 |
initSampler: rateLimitingSampler, |
| 468 |
expectedSampler: otherRateLimitingSampler, |
| 469 |
shouldErr: false, |
| 470 |
referenceEquivalence: false, |
| 471 |
caption: "valid rate limiting strategy", |
| 472 |
}, |
| 473 |
{ |
| 474 |
res: &jaeger_api_v2.SamplingStrategyResponse{}, |
| 475 |
initSampler: rateLimitingSampler, |
| 476 |
expectedSampler: rateLimitingSampler, |
| 477 |
shouldErr: true, |
| 478 |
referenceEquivalence: true, |
| 479 |
caption: "invalid strategy", |
| 480 |
}, |
| 481 |
} |
| 482 |
|
| 483 |
for _, tc := range testCases { |
| 484 |
testCase := tc // capture loop var |
| 485 |
t.Run(testCase.caption, func(t *testing.T) { |
| 486 |
remoteSampler := New( |
| 487 |
"test", |
| 488 |
WithInitialSampler(testCase.initSampler), |
| 489 |
) |
| 490 |
defer remoteSampler.Close() |
| 491 |
err := remoteSampler.updateSamplerViaUpdaters(testCase.res) |
| 492 |
if testCase.shouldErr { |
| 493 |
require.Error(t, err) |
| 494 |
return |
| 495 |
} |
| 496 |
if testCase.referenceEquivalence { |
| 497 |
assert.Equal(t, testCase.expectedSampler, remoteSampler.sampler) |
| 498 |
} else { |
| 499 |
type comparable interface { |
| 500 |
Equal(other trace.Sampler) bool |
| 501 |
} |
| 502 |
es, esOk := testCase.expectedSampler.(comparable) |
| 503 |
require.True(t, esOk, "expected sampler %+v must implement Equal()", testCase.expectedSampler) |
| 504 |
assert.True(t, es.Equal(remoteSampler.sampler), |
| 505 |
"sampler.Equal: want=%+v, have=%+v", testCase.expectedSampler, remoteSampler.sampler) |
| 506 |
} |
| 507 |
}) |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
func getSamplingStrategyResponse(strategyType jaeger_api_v2.SamplingStrategyType, value float64) *jaeger_api_v2.SamplingStrategyResponse { |
| 512 |
if strategyType == jaeger_api_v2.SamplingStrategyType_PROBABILISTIC { |
| 513 |
return &jaeger_api_v2.SamplingStrategyResponse{ |
| 514 |
StrategyType: jaeger_api_v2.SamplingStrategyType_PROBABILISTIC, |
| 515 |
ProbabilisticSampling: &jaeger_api_v2.ProbabilisticSamplingStrategy{ |
| 516 |
SamplingRate: value, |
| 517 |
}, |
| 518 |
} |
| 519 |
} |
| 520 |
if strategyType == jaeger_api_v2.SamplingStrategyType_RATE_LIMITING { |
| 521 |
return &jaeger_api_v2.SamplingStrategyResponse{ |
| 522 |
StrategyType: jaeger_api_v2.SamplingStrategyType_RATE_LIMITING, |
| 523 |
RateLimitingSampling: &jaeger_api_v2.RateLimitingSamplingStrategy{ |
| 524 |
MaxTracesPerSecond: int32(value), |
| 525 |
}, |
| 526 |
} |
| 527 |
} |
| 528 |
return nil |
| 529 |
} |
| 530 |
|
| 531 |
func TestSamplingStrategyParserImpl(t *testing.T) { |
| 532 |
assertProbabilistic := func(t *testing.T, s *jaeger_api_v2.SamplingStrategyResponse) { |
| 533 |
require.NotNil(t, s.GetProbabilisticSampling(), "output: %+v", s) |
| 534 |
require.Equal(t, 0.42, s.GetProbabilisticSampling().GetSamplingRate(), "output: %+v", s) |
| 535 |
} |
| 536 |
assertRateLimiting := func(t *testing.T, s *jaeger_api_v2.SamplingStrategyResponse) { |
| 537 |
require.NotNil(t, s.GetRateLimitingSampling(), "output: %+v", s) |
| 538 |
require.EqualValues(t, 42, s.GetRateLimitingSampling().GetMaxTracesPerSecond(), "output: %+v", s) |
| 539 |
} |
| 540 |
tests := []struct { |
| 541 |
name string |
| 542 |
json string |
| 543 |
assert func(t *testing.T, s *jaeger_api_v2.SamplingStrategyResponse) |
| 544 |
}{ |
| 545 |
{ |
| 546 |
name: "official JSON probabilistic", |
| 547 |
json: `{"strategyType":"PROBABILISTIC","probabilisticSampling":{"samplingRate":0.42}}`, |
| 548 |
assert: assertProbabilistic, |
| 549 |
}, |
| 550 |
{ |
| 551 |
name: "official JSON rate limiting", |
| 552 |
json: `{"strategyType":"RATE_LIMITING","rateLimitingSampling":{"maxTracesPerSecond":42}}`, |
| 553 |
assert: assertRateLimiting, |
| 554 |
}, |
| 555 |
{ |
| 556 |
name: "legacy JSON probabilistic", |
| 557 |
json: `{"strategyType":0,"probabilisticSampling":{"samplingRate":0.42}}`, |
| 558 |
assert: assertProbabilistic, |
| 559 |
}, |
| 560 |
{ |
| 561 |
name: "legacy JSON rate limiting", |
| 562 |
json: `{"strategyType":1,"rateLimitingSampling":{"maxTracesPerSecond":42}}`, |
| 563 |
assert: assertRateLimiting, |
| 564 |
}, |
| 565 |
} |
| 566 |
for _, test := range tests { |
| 567 |
t.Run(test.name, func(t *testing.T) { |
| 568 |
val, err := new(samplingStrategyParserImpl).Parse([]byte(test.json)) |
| 569 |
require.NoError(t, err) |
| 570 |
s := val.(*jaeger_api_v2.SamplingStrategyResponse) |
| 571 |
test.assert(t, s) |
| 572 |
}) |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
func TestSamplingStrategyParserImpl_Error(t *testing.T) { |
| 577 |
json := `{"strategyType":"foo_bar","probabilisticSampling":{"samplingRate":0.42}}` |
| 578 |
val, err := new(samplingStrategyParserImpl).Parse([]byte(json)) |
| 579 |
require.Error(t, err, "output: %+v", val) |
| 580 |
require.Contains(t, err.Error(), `unknown value "foo_bar"`) |
| 581 |
} |
| 582 |
|
| 583 |
func TestDefaultSamplingStrategyFetcher_Timeout(t *testing.T) { |
| 584 |
fetcher := newHTTPSamplingStrategyFetcher("") |
| 585 |
assert.Equal(t, defaultRemoteSamplingTimeout, fetcher.httpClient.Timeout) |
| 586 |
} |
| 587 |
|
| 588 |
func TestEnvVarSettingForNewTracer(t *testing.T) { |
| 589 |
type testConfig struct { |
| 590 |
samplingServerURL string |
| 591 |
samplingRefreshInterval time.Duration |
| 592 |
} |
| 593 |
|
| 594 |
tests := []struct { |
| 595 |
otelTraceSamplerArgs string |
| 596 |
expErrs []string |
| 597 |
codeOptions []Option |
| 598 |
expConfig testConfig |
| 599 |
}{ |
| 600 |
{ |
| 601 |
otelTraceSamplerArgs: "endpoint=http://localhost:14250,pollingIntervalMs=5000,initialSamplingRate=0.25", |
| 602 |
expErrs: []string{}, |
| 603 |
}, |
| 604 |
{ |
| 605 |
otelTraceSamplerArgs: "endpointhttp://localhost:14250,pollingIntervalMs=5x000,initialSamplingRate=0.xyz25,invalidKey=invalidValue", |
| 606 |
expErrs: []string{ |
| 607 |
"argument endpointhttp://localhost:14250 is not of type '<key>=<value>'", |
| 608 |
"pollingIntervalMs parsing failed", |
| 609 |
"initialSamplingRate parsing failed", |
| 610 |
"invalid argument invalidKey in OTEL_TRACE_SAMPLER_ARG", |
| 611 |
}, |
| 612 |
}, |
| 613 |
{ |
| 614 |
// Make sure we don't override values provided in code |
| 615 |
otelTraceSamplerArgs: "endpoint=http://localhost:14250,pollingIntervalMs=5000,initialSamplingRate=0.25", |
| 616 |
expErrs: []string{}, |
| 617 |
codeOptions: []Option{ |
| 618 |
WithSamplingServerURL("http://localhost:5778"), |
| 619 |
}, |
| 620 |
expConfig: testConfig{ |
| 621 |
samplingServerURL: "http://localhost:5778", |
| 622 |
samplingRefreshInterval: time.Millisecond * 5000, |
| 623 |
}, |
| 624 |
}, |
| 625 |
} |
| 626 |
|
| 627 |
for _, test := range tests { |
| 628 |
t.Run("", func(t *testing.T) { |
| 629 |
t.Setenv("OTEL_TRACES_SAMPLER_ARG", test.otelTraceSamplerArgs) |
| 630 |
|
| 631 |
_, errs := getEnvOptions() |
| 632 |
require.Len(t, errs, len(test.expErrs)) |
| 633 |
|
| 634 |
for i := range errs { |
| 635 |
require.ErrorContains(t, errs[i], test.expErrs[i]) |
| 636 |
} |
| 637 |
|
| 638 |
if test.codeOptions != nil { |
| 639 |
cfg := newConfig(test.codeOptions...) |
| 640 |
require.Equal(t, test.expConfig.samplingServerURL, cfg.samplingServerURL) |
| 641 |
require.Equal(t, test.expConfig.samplingRefreshInterval, cfg.samplingRefreshInterval) |
| 642 |
} |
| 643 |
}) |
| 644 |
} |
| 645 |
|
| 646 |
t.Run("No-op when env var not set or empty", func(t *testing.T) { |
| 647 |
for _, test := range []struct { |
| 648 |
desc string |
| 649 |
envSetup func() |
| 650 |
}{ |
| 651 |
{ |
| 652 |
"env var empty", |
| 653 |
func() { t.Setenv("OTEL_TRACES_SAMPLER_ARG", "") }, |
| 654 |
}, |
| 655 |
{ |
| 656 |
"env var unset", |
| 657 |
func() { |
| 658 |
// t.Setenv to restore this environment variable at the end of the test |
| 659 |
t.Setenv("OTEL_TRACES_SAMPLER_ARG", "") |
| 660 |
// unset it during the test |
| 661 |
require.NoError(t, os.Unsetenv("OTEL_TRACES_SAMPLER_ARG")) |
| 662 |
}, |
| 663 |
}, |
| 664 |
} { |
| 665 |
t.Run(test.desc, func(t *testing.T) { |
| 666 |
test.envSetup() |
| 667 |
opts, errs := getEnvOptions() |
| 668 |
|
| 669 |
require.Empty(t, errs) |
| 670 |
require.Empty(t, opts) |
| 671 |
}) |
| 672 |
} |
| 673 |
}) |
| 674 |
} |