Path: blob/main/pkg/flow/tracing/internal/jaegerremote/sampler_remote_options.go
4096 views
// Copyright The OpenTelemetry Authors1// Copyright (c) 2021 The Jaeger Authors.2// Copyright (c) 2017 Uber Technologies, Inc.3//4// Licensed under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516//nolint:all17package jaegerremote1819import (20"time"2122"go.opentelemetry.io/otel/sdk/trace"23)2425type config struct {26sampler trace.Sampler27samplingServerURL string28samplingRefreshInterval time.Duration29samplingFetcher samplingStrategyFetcher30samplingParser samplingStrategyParser31updaters []samplerUpdater32posParams perOperationSamplerParams33}3435// newConfig returns an appropriately configured config.36func newConfig(options ...Option) config {37c := config{38sampler: newProbabilisticSampler(0.001),39samplingServerURL: defaultSamplingServerURL,40samplingRefreshInterval: defaultSamplingRefreshInterval,41samplingFetcher: newHTTPSamplingStrategyFetcher(defaultSamplingServerURL),42samplingParser: new(samplingStrategyParserImpl),43updaters: []samplerUpdater{44new(probabilisticSamplerUpdater),45new(rateLimitingSamplerUpdater),46},47posParams: perOperationSamplerParams{48MaxOperations: defaultSamplingMaxOperations,49OperationNameLateBinding: defaultSamplingOperationNameLateBinding,50},51}52for _, option := range options {53option.apply(&c)54}55c.updaters = append([]samplerUpdater{&perOperationSamplerUpdater{56MaxOperations: c.posParams.MaxOperations,57OperationNameLateBinding: c.posParams.OperationNameLateBinding,58}}, c.updaters...)59return c60}6162// Option applies configuration settings to a Sampler.63type Option interface {64apply(*config)65}6667type optionFunc func(*config)6869func (fn optionFunc) apply(c *config) {70fn(c)71}7273// WithInitialSampler creates a Option that sets the initial sampler74// to use before a remote sampler is created and used.75func WithInitialSampler(sampler trace.Sampler) Option {76return optionFunc(func(c *config) {77c.sampler = sampler78})79}8081// WithSamplingServerURL creates a Option that sets the sampling server url82// of the local agent that contains the sampling strategies.83func WithSamplingServerURL(samplingServerURL string) Option {84return optionFunc(func(c *config) {85c.samplingServerURL = samplingServerURL86// 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 bound87c.samplingFetcher = newHTTPSamplingStrategyFetcher(samplingServerURL)88})89}9091// WithMaxOperations creates a Option that sets the maximum number of92// operations the sampler will keep track of.93func WithMaxOperations(maxOperations int) Option {94return optionFunc(func(c *config) {95c.posParams.MaxOperations = maxOperations96})97}9899// WithOperationNameLateBinding creates a Option that sets the respective100// field in the perOperationSamplerParams.101func WithOperationNameLateBinding(enable bool) Option {102return optionFunc(func(c *config) {103c.posParams.OperationNameLateBinding = enable104})105}106107// WithSamplingRefreshInterval creates a Option that sets how often the108// sampler will poll local agent for the appropriate sampling strategy.109func WithSamplingRefreshInterval(samplingRefreshInterval time.Duration) Option {110return optionFunc(func(c *config) {111c.samplingRefreshInterval = samplingRefreshInterval112})113}114115// samplingStrategyFetcher creates a Option that initializes sampling strategy fetcher.116func withSamplingStrategyFetcher(fetcher samplingStrategyFetcher) Option {117return optionFunc(func(c *config) {118c.samplingFetcher = fetcher119})120}121122// samplingStrategyParser creates a Option that initializes sampling strategy parser.123func withSamplingStrategyParser(parser samplingStrategyParser) Option {124return optionFunc(func(c *config) {125c.samplingParser = parser126})127}128129// withUpdaters creates a Option that initializes sampler updaters.130func withUpdaters(updaters ...samplerUpdater) Option {131return optionFunc(func(c *config) {132c.updaters = updaters133})134}135136137