|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +// TestResolveOTLPEndpoint verifies endpoint precedence and handling of empty values. |
| 10 | +func TestResolveOTLPEndpoint(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + commonEndpoint string |
| 14 | + signalSpecificEndpoint string |
| 15 | + expectedEndpoint string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "empty values", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "common endpoint", |
| 22 | + commonEndpoint: "common:4317", |
| 23 | + expectedEndpoint: "common:4317", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "signal-specific endpoint", |
| 27 | + signalSpecificEndpoint: "signal:4317", |
| 28 | + expectedEndpoint: "signal:4317", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "signal-specific endpoint takes precedence", |
| 32 | + commonEndpoint: "common:4317", |
| 33 | + signalSpecificEndpoint: "signal:4317", |
| 34 | + expectedEndpoint: "signal:4317", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "whitespace-only values", |
| 38 | + commonEndpoint: " ", |
| 39 | + signalSpecificEndpoint: "\t", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "endpoints with surrounding whitespace", |
| 43 | + commonEndpoint: " common:4317 ", |
| 44 | + signalSpecificEndpoint: " signal:4317 ", |
| 45 | + expectedEndpoint: "signal:4317", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "whitespace-only signal-specific endpoint falls back to common endpoint", |
| 49 | + commonEndpoint: "common:4317", |
| 50 | + signalSpecificEndpoint: " ", |
| 51 | + expectedEndpoint: "common:4317", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, signalEndpointEnv := range []string{otlpMetricsEndpointEnv, otlpTracesEndpointEnv} { |
| 56 | + t.Run(signalEndpointEnv, func(t *testing.T) { |
| 57 | + for _, test := range tests { |
| 58 | + t.Run(test.name, func(t *testing.T) { |
| 59 | + t.Setenv(otlpEndpointEnv, test.commonEndpoint) |
| 60 | + t.Setenv(signalEndpointEnv, test.signalSpecificEndpoint) |
| 61 | + |
| 62 | + require.Equal(t, test.expectedEndpoint, resolveOTLPEndpoint(signalEndpointEnv)) |
| 63 | + }) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
0 commit comments