Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jaegerremote: Add WithSamplingStrategyFetcher option #4045

Merged
merged 16 commits into from
Sep 29, 2023
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
morus12 marked this conversation as resolved.
Show resolved Hide resolved

### 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

Expand Down
9 changes: 3 additions & 6 deletions samplers/jaegerremote/sampler_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
},
}
}
Expand Down
8 changes: 5 additions & 3 deletions samplers/jaegerremote/sampler_remote_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
})
Expand Down
17 changes: 11 additions & 6 deletions samplers/jaegerremote/sampler_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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...),
)
Expand Down Expand Up @@ -334,7 +333,8 @@ func TestRemotelyControlledSampler_multiStrategyResponse(t *testing.T) {
Operation: testUnusedOpName,
ProbabilisticSampling: &jaeger_api_v2.ProbabilisticSamplingStrategy{
SamplingRate: testUnusedOpSamplingRate,
}},
},
},
},
},
}
Expand Down Expand Up @@ -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)
}
Loading