From b3569d7dd2a914b64ff732fe2d2fe4e0fbd576f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Wyszy=C5=84ski?= Date: Fri, 29 Sep 2023 16:12:12 +0200 Subject: [PATCH] jaegerremote: Add WithSamplingStrategyFetcher option (#4045) --- CHANGELOG.md | 5 +++++ samplers/jaegerremote/sampler_remote.go | 9 +++------ samplers/jaegerremote/sampler_remote_options.go | 8 +++++--- samplers/jaegerremote/sampler_remote_test.go | 17 +++++++++++------ 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b4e3bbee8d..d8a9aea6226 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108) +- Add `WithSamplingStrategyFetcher` which sets custom fetcher implementation. (#4045) + +### Fixed + +- Do not panic when the default HTTP round-tripper is not `*http.Transport`. (#4045) ## [1.20.0/0.45.0/0.14.0] - 2023-09-28 diff --git a/samplers/jaegerremote/sampler_remote.go b/samplers/jaegerremote/sampler_remote.go index 6deb29427ac..45929adcb95 100644 --- a/samplers/jaegerremote/sampler_remote.go +++ b/samplers/jaegerremote/sampler_remote.go @@ -39,8 +39,8 @@ const ( defaultSamplingOperationNameLateBinding = true ) -// samplingStrategyFetcher is used to fetch sampling strategy updates from remote server. -type samplingStrategyFetcher interface { +// SamplingStrategyFetcher is used to fetch sampling strategy updates from remote server. +type SamplingStrategyFetcher interface { Fetch(service string) ([]byte, error) } @@ -273,13 +273,10 @@ type httpSamplingStrategyFetcher struct { } func newHTTPSamplingStrategyFetcher(serverURL string) *httpSamplingStrategyFetcher { - customTransport := http.DefaultTransport.(*http.Transport).Clone() - customTransport.ResponseHeaderTimeout = defaultRemoteSamplingTimeout - return &httpSamplingStrategyFetcher{ serverURL: serverURL, httpClient: http.Client{ - Transport: customTransport, + Timeout: defaultRemoteSamplingTimeout, }, } } diff --git a/samplers/jaegerremote/sampler_remote_options.go b/samplers/jaegerremote/sampler_remote_options.go index 31f95fb5ebb..9708821549f 100644 --- a/samplers/jaegerremote/sampler_remote_options.go +++ b/samplers/jaegerremote/sampler_remote_options.go @@ -28,7 +28,7 @@ type config struct { sampler trace.Sampler samplingServerURL string samplingRefreshInterval time.Duration - samplingFetcher samplingStrategyFetcher + samplingFetcher SamplingStrategyFetcher samplingParser samplingStrategyParser updaters []samplerUpdater posParams perOperationSamplerParams @@ -123,8 +123,10 @@ func WithLogger(logger logr.Logger) Option { }) } -// samplingStrategyFetcher creates a Option that initializes sampling strategy fetcher. -func withSamplingStrategyFetcher(fetcher samplingStrategyFetcher) Option { +// WithSamplingStrategyFetcher creates an Option that initializes the sampling strategy fetcher. +// Custom fetcher can be used for setting custom headers, timeouts, etc., or getting +// sampling strategies from a different source, like files. +func WithSamplingStrategyFetcher(fetcher SamplingStrategyFetcher) Option { return optionFunc(func(c *config) { c.samplingFetcher = fetcher }) diff --git a/samplers/jaegerremote/sampler_remote_test.go b/samplers/jaegerremote/sampler_remote_test.go index f84456b0b20..ac29e27f7b2 100644 --- a/samplers/jaegerremote/sampler_remote_test.go +++ b/samplers/jaegerremote/sampler_remote_test.go @@ -46,7 +46,7 @@ func TestRemotelyControlledSampler_updateConcurrentSafe(t *testing.T) { WithInitialSampler(initSampler), WithSamplingServerURL("my url"), WithSamplingRefreshInterval(time.Millisecond), - withSamplingStrategyFetcher(fetcher), + WithSamplingStrategyFetcher(fetcher), withSamplingStrategyParser(parser), withUpdaters(updaters...), ) @@ -80,8 +80,7 @@ func (c *testSamplingStrategyFetcher) Fetch(serviceName string) ([]byte, error) return c.response, nil } -type testSamplingStrategyParser struct { -} +type testSamplingStrategyParser struct{} func (p *testSamplingStrategyParser) Parse(response []byte) (interface{}, error) { strategy := new(jaeger_api_v2.SamplingStrategyResponse) @@ -117,7 +116,7 @@ func TestRemoteSamplerOptions(t *testing.T) { WithInitialSampler(initSampler), WithSamplingServerURL("my url"), WithSamplingRefreshInterval(42*time.Second), - withSamplingStrategyFetcher(fetcher), + WithSamplingStrategyFetcher(fetcher), withSamplingStrategyParser(parser), withUpdaters(updaters...), WithLogger(logger), @@ -302,7 +301,7 @@ func TestRemotelyControlledSampler_ImmediatelyUpdateOnStartup(t *testing.T) { WithInitialSampler(initSampler), WithSamplingServerURL("my url"), WithSamplingRefreshInterval(10*time.Minute), - withSamplingStrategyFetcher(fetcher), + WithSamplingStrategyFetcher(fetcher), withSamplingStrategyParser(parser), withUpdaters(updaters...), ) @@ -334,7 +333,8 @@ func TestRemotelyControlledSampler_multiStrategyResponse(t *testing.T) { Operation: testUnusedOpName, ProbabilisticSampling: &jaeger_api_v2.ProbabilisticSamplingStrategy{ SamplingRate: testUnusedOpSamplingRate, - }}, + }, + }, }, }, } @@ -586,3 +586,8 @@ func TestSamplingStrategyParserImpl_Error(t *testing.T) { require.Error(t, err, "output: %+v", val) require.Contains(t, err.Error(), `unknown value "foo_bar"`) } + +func TestDefaultSamplingStrategyFetcher_Timeout(t *testing.T) { + fetcher := newHTTPSamplingStrategyFetcher("") + assert.Equal(t, defaultRemoteSamplingTimeout, fetcher.httpClient.Timeout) +}