-
Notifications
You must be signed in to change notification settings - Fork 5
fix(otel): normalize endpoint URL and infer insecure mode from scheme #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,75 @@ func TestNewTelemetry_DefaultPropagatorAndRedactor(t *testing.T) { | |
| assert.NotNil(t, tl.Redactor, "default redactor should be set") | ||
| } | ||
|
|
||
| // =========================================================================== | ||
| // 1b. Endpoint normalization | ||
| // =========================================================================== | ||
|
|
||
| func TestNewTelemetry_EndpointNormalization(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| endpoint string | ||
| wantEndpoint string | ||
| wantInsecure bool | ||
| insecureOverride bool // initial InsecureExporter value | ||
| }{ | ||
| { | ||
| name: "http scheme stripped and insecure inferred", | ||
| endpoint: "http://otel-collector:4317", | ||
| wantEndpoint: "otel-collector:4317", | ||
| wantInsecure: true, | ||
| }, | ||
| { | ||
| name: "https scheme stripped and insecure stays false", | ||
| endpoint: "https://otel-collector:4317", | ||
| wantEndpoint: "otel-collector:4317", | ||
| wantInsecure: false, | ||
| }, | ||
| { | ||
| name: "no scheme defaults to insecure", | ||
| endpoint: "otel-collector:4317", | ||
| wantEndpoint: "otel-collector:4317", | ||
| wantInsecure: true, | ||
| }, | ||
| { | ||
| name: "https with explicit insecure override preserved", | ||
| endpoint: "https://otel-collector:4317", | ||
| insecureOverride: true, | ||
| wantEndpoint: "otel-collector:4317", | ||
| wantInsecure: true, | ||
| }, | ||
| { | ||
| name: "http with trailing slash", | ||
| endpoint: "http://otel-collector:4317/", | ||
| wantEndpoint: "otel-collector:4317/", | ||
| wantInsecure: true, | ||
| }, | ||
| } | ||
|
Comment on lines
+102
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add regression cases for schemeless secure intent and whitespace normalization. The table misses two high-impact scenarios in this change path: preserving secure intent for schemeless endpoints and trimming schemeless endpoints with surrounding whitespace. Proposed test additions {
name: "no scheme defaults to insecure",
endpoint: "otel-collector:4317",
wantEndpoint: "otel-collector:4317",
wantInsecure: true,
},
+ {
+ name: "no scheme with explicit secure intent is preserved",
+ endpoint: "otel-collector:4317",
+ insecureOverride: false,
+ wantEndpoint: "otel-collector:4317",
+ wantInsecure: false,
+ },
+ {
+ name: "no scheme trims surrounding whitespace",
+ endpoint: " otel-collector:4317 ",
+ insecureOverride: true,
+ wantEndpoint: "otel-collector:4317",
+ wantInsecure: true,
+ },🤖 Prompt for AI Agents |
||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Use telemetry disabled so we don't need a real collector. | ||
| tl, err := NewTelemetry(TelemetryConfig{ | ||
| LibraryName: "test-lib", | ||
| EnableTelemetry: false, | ||
| CollectorExporterEndpoint: tt.endpoint, | ||
| InsecureExporter: tt.insecureOverride, | ||
| Logger: log.NewNop(), | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, tl) | ||
| assert.Equal(t, tt.wantEndpoint, tl.CollectorExporterEndpoint, | ||
| "endpoint should be normalized") | ||
| assert.Equal(t, tt.wantInsecure, tl.InsecureExporter, | ||
| "InsecureExporter should be inferred from scheme") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // =========================================================================== | ||
| // 2. Telemetry methods on nil receiver | ||
| // =========================================================================== | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent silent TLS downgrade and persist normalized endpoint.
Line [112]-[113] forces
InsecureExporter = truefor all schemeless endpoints, which can unintentionally downgrade secure setups. Also, Line [104] trims intoep, but the no-scheme branch never writes the trimmed value back tocfg.CollectorExporterEndpoint.Proposed fix
if ep := strings.TrimSpace(cfg.CollectorExporterEndpoint); ep != "" { + cfg.CollectorExporterEndpoint = ep switch { case strings.HasPrefix(ep, "http://"): cfg.CollectorExporterEndpoint = strings.TrimPrefix(ep, "http://") cfg.InsecureExporter = true case strings.HasPrefix(ep, "https://"): cfg.CollectorExporterEndpoint = strings.TrimPrefix(ep, "https://") default: - // No scheme — assume insecure (common in k8s internal comms). - cfg.InsecureExporter = true + // No scheme: keep caller-provided InsecureExporter to avoid silent transport downgrade. } }📝 Committable suggestion
🤖 Prompt for AI Agents