Skip to content

Commit

Permalink
Fix: error logged on empty OTEL_TRACES_SAMPLER_ARG
Browse files Browse the repository at this point in the history
We were noticing errors like:

> time=2024-12-18T15:00:02.080Z level=ERROR
source=/go/pkg/mod/go.opentelemetry.io/contrib/samplers/[email protected]/sampler_remote_options.go:112
msg="env variable parsing failure" err="argument  is not of type
'<key>=<value>'"

In applications using the Jaeger sampler but had not set the
`OTEL_TRACES_SAMPLER_ARG` environment variable. The issue was:

* `os.Getenv` would return `""` when this var was not set, and
* `strings.Split("", ",")` would return `[]string{""}`

since `""` does not contain `=` an error is emitted.
  • Loading branch information
matthewhughes-uw committed Dec 18, 2024
1 parent 13b1cc5 commit 8a05d2c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion samplers/jaegerremote/sampler_remote_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ func getEnvOptions() ([]Option, []error) {
// list of errors which will be logged once logger is set by the user
var errs []error

args := strings.Split(os.Getenv("OTEL_TRACES_SAMPLER_ARG"), ",")
rawEnvArgs, ok := os.LookupEnv("OTEL_TRACES_SAMPLER_ARG")
if !ok {
return nil, nil
}

args := strings.Split(rawEnvArgs, ",")
for _, arg := range args {
keyValue := strings.Split(arg, "=")
if len(keyValue) != 2 {
Expand Down
13 changes: 13 additions & 0 deletions samplers/jaegerremote/sampler_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"os"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -653,4 +654,16 @@ func TestEnvVarSettingForNewTracer(t *testing.T) {
}
})
}

t.Run("No-op when env var not set", func(t *testing.T) {
// t.Setenv to restore this environment variable at the end of the test
t.Setenv("OTEL_TRACES_SAMPLER_ARG", "")
// unset it during the test
require.NoError(t, os.Unsetenv("OTEL_TRACES_SAMPLER_ARG"))

opts, errs := getEnvOptions()

require.Empty(t, errs)
require.Empty(t, opts)
})
}

0 comments on commit 8a05d2c

Please sign in to comment.