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
6 changes: 6 additions & 0 deletions CHANGELOG.md
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

- Fixed panic when default HTTP round-tripper is not `*http.Transport` (#4045)
morus12 marked this conversation as resolved.
Show resolved Hide resolved

## [1.18.0/0.43.0/0.12.0] - 2023-08-28

Expand All @@ -19,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `NewMiddleware` function in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#2964)
- The `go.opentelemetry.io/contrib/exporters/autoexport` package to provide configuration of trace exporters with useful defaults and environment variable support. (#2753, #4100, #4130, #4132, #4134)
- `WithRouteTag` in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` adds HTTP route attribute to metrics. (#615)

morus12 marked this conversation as resolved.
Show resolved Hide resolved
- Add `WithSpanOptions` option in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#3768)
- Add testing support for Go 1.21. (#4233)

Expand Down
9 changes: 3 additions & 6 deletions samplers/jaegerremote/sampler_remote.go
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
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
31 changes: 25 additions & 6 deletions samplers/jaegerremote/sampler_remote_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"net/http"
"sync"
"testing"
"time"
Expand All @@ -46,7 +47,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 +81,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 +117,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 +302,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 +334,8 @@ func TestRemotelyControlledSampler_multiStrategyResponse(t *testing.T) {
Operation: testUnusedOpName,
ProbabilisticSampling: &jaeger_api_v2.ProbabilisticSamplingStrategy{
SamplingRate: testUnusedOpSamplingRate,
}},
},
},
},
},
}
Expand Down Expand Up @@ -586,3 +587,21 @@ 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)
}

func TestDefaultSamplingStrategyFetcher_NoPanic(t *testing.T) {
defaultTransport := http.DefaultTransport
http.DefaultTransport = http.NewFileTransport(http.Dir("/"))

t.Cleanup(func() {
http.DefaultTransport = defaultTransport
})

require.NotPanics(t, func() {
newHTTPSamplingStrategyFetcher("")
})
}