1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-29 23:07:45 +02:00

sdk/trace: Manage trace environment variables in testing package (#6552)

Fix: #6542

Co-authored-by: Damien Mathieu <42@dmathieu.com>
This commit is contained in:
Zhengke Zhou
2025-03-28 16:45:34 +08:00
committed by GitHub
parent f88f3f19c7
commit 63f3cfe1b5
4 changed files with 11 additions and 42 deletions

View File

@@ -14,10 +14,14 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
ottest "go.opentelemetry.io/otel/sdk/internal/internaltest"
"go.opentelemetry.io/otel/trace"
)
const (
envTracesSampler = "OTEL_TRACES_SAMPLER"
envTracesSamplerArg = "OTEL_TRACES_SAMPLER_ARG"
)
type basicSpanProcessor struct {
flushed bool
closed bool
@@ -313,19 +317,11 @@ func TestTracerProviderSamplerConfigFromEnv(t *testing.T) {
for _, test := range tests {
t.Run(test.sampler, func(t *testing.T) {
envVars := map[string]string{
"OTEL_TRACES_SAMPLER": test.sampler,
}
t.Setenv(envTracesSampler, test.sampler)
if test.samplerArg != "" {
envVars["OTEL_TRACES_SAMPLER_ARG"] = test.samplerArg
t.Setenv(envTracesSamplerArg, test.samplerArg)
}
envStore, err := ottest.SetEnvVariables(envVars)
require.NoError(t, err)
t.Cleanup(func() {
handler.Reset()
require.NoError(t, envStore.Restore())
})
stp := NewTracerProvider(WithSyncer(NewTestExporter()))
assert.Equal(t, test.description, stp.sampler.Description())
@@ -337,15 +333,8 @@ func TestTracerProviderSamplerConfigFromEnv(t *testing.T) {
if test.argOptional {
t.Run("invalid sampler arg", func(t *testing.T) {
envStore, err := ottest.SetEnvVariables(map[string]string{
"OTEL_TRACES_SAMPLER": test.sampler,
"OTEL_TRACES_SAMPLER_ARG": "invalid-ignored-string",
})
require.NoError(t, err)
t.Cleanup(func() {
handler.Reset()
require.NoError(t, envStore.Restore())
})
t.Setenv(envTracesSampler, test.sampler)
t.Setenv(envTracesSamplerArg, "invalid-ignored-string")
stp := NewTracerProvider(WithSyncer(NewTestExporter()))
t.Cleanup(func() {