Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- Treat empty `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG` values as unset in `go.opentelemetry.io/otel/sdk/trace`.

<!-- Released section -->
<!-- Don't change this section unless doing release -->

Expand Down
51 changes: 51 additions & 0 deletions sdk/trace/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,57 @@ func TestTracerProviderSamplerConfigFromEnv(t *testing.T) {
}
}

func TestTracerProviderSamplerConfigFromEnvEmptyValues(t *testing.T) {
tests := []struct {
name string
sampler string
samplerArg string
setSamplerArg bool
description string
}{
{
name: "empty sampler",
sampler: "",
description: ParentBased(AlwaysSample()).Description(),
},
{
name: "empty traceidratio sampler arg",
sampler: "traceidratio",
samplerArg: "",
setSamplerArg: true,
description: TraceIDRatioBased(1.0).Description(),
},
{
name: "empty parentbased traceidratio sampler arg",
sampler: "parentbased_traceidratio",
samplerArg: "",
setSamplerArg: true,
description: ParentBased(TraceIDRatioBased(1.0)).Description(),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
handler.Reset()
t.Cleanup(handler.Reset)

t.Setenv(envTracesSampler, test.sampler)
if test.setSamplerArg {
t.Setenv(envTracesSamplerArg, test.samplerArg)
}

stp := NewTracerProvider(WithSyncer(NewTestExporter()))
t.Cleanup(func() {
//nolint:usetesting // required to avoid getting a canceled context at cleanup.
require.NoError(t, stp.Shutdown(context.Background()))
})

assert.Equal(t, test.description, stp.sampler.Description())
assert.Empty(t, handler.errs)
})
}
}

func testStoredError(t *testing.T, target any) {
t.Helper()

Expand Down
9 changes: 4 additions & 5 deletions sdk/trace/sampler_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ func (e samplerArgParseError) Unwrap() error {
}

func samplerFromEnv() (Sampler, error) {
sampler, ok := os.LookupEnv(tracesSamplerKey)
if !ok {
sampler := strings.ToLower(strings.TrimSpace(os.Getenv(tracesSamplerKey)))
if sampler == "" {
return nil, nil
}

sampler = strings.ToLower(strings.TrimSpace(sampler))
samplerArg, hasSamplerArg := os.LookupEnv(tracesSamplerArgKey)
samplerArg = strings.TrimSpace(samplerArg)
samplerArg := strings.TrimSpace(os.Getenv(tracesSamplerArgKey))
hasSamplerArg := samplerArg != ""

switch sampler {
case samplerAlwaysOn:
Expand Down
Loading