You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-08-10 22:31:50 +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:
@@ -16,7 +16,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.opentelemetry.io/otel/sdk/internal/env"
|
||||
ottest "go.opentelemetry.io/otel/sdk/internal/internaltest"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
@@ -247,16 +246,6 @@ func TestNewBatchSpanProcessorWithEnvOptions(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
envStore := ottest.NewEnvStore()
|
||||
envStore.Record(env.BatchSpanProcessorScheduleDelayKey)
|
||||
envStore.Record(env.BatchSpanProcessorExportTimeoutKey)
|
||||
envStore.Record(env.BatchSpanProcessorMaxQueueSizeKey)
|
||||
envStore.Record(env.BatchSpanProcessorMaxExportBatchSizeKey)
|
||||
|
||||
defer func() {
|
||||
require.NoError(t, envStore.Restore())
|
||||
}()
|
||||
|
||||
for _, option := range options {
|
||||
t.Run(option.name, func(t *testing.T) {
|
||||
for k, v := range option.envs {
|
||||
|
@@ -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() {
|
||||
|
@@ -12,7 +12,6 @@ import (
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/internal/env"
|
||||
ottest "go.opentelemetry.io/otel/sdk/internal/internaltest"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
@@ -110,10 +109,7 @@ func TestSettingSpanLimits(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if test.env != nil {
|
||||
es := ottest.NewEnvStore()
|
||||
t.Cleanup(func() { require.NoError(t, es.Restore()) })
|
||||
for k, v := range test.env {
|
||||
es.Record(k)
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
}
|
||||
|
@@ -29,7 +29,7 @@ import (
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
const envVar = "OTEL_RESOURCE_ATTRIBUTES"
|
||||
const envVarResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"
|
||||
|
||||
type storingHandler struct {
|
||||
errs []error
|
||||
@@ -1379,12 +1379,7 @@ func mergeResource(t *testing.T, r1, r2 *resource.Resource) *resource.Resource {
|
||||
}
|
||||
|
||||
func TestWithResource(t *testing.T) {
|
||||
store, err := ottest.SetEnvVariables(map[string]string{
|
||||
envVar: "key=value,rk5=7",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, store.Restore()) }()
|
||||
|
||||
t.Setenv(envVarResourceAttributes, "key=value,rk5=7")
|
||||
cases := []struct {
|
||||
name string
|
||||
options []TracerProviderOption
|
||||
|
Reference in New Issue
Block a user