@@ -188,6 +188,13 @@ func SetupOTelSDKWithConfig(ctx context.Context, cfg OTelConfig) (shutdown func(
188188 err = errors .Join (inErr , shutdown (ctx ))
189189 }
190190
191+ // Normalize endpoint to include a URL scheme so WithEndpointURL can
192+ // parse it. Bare IP:port values like "127.0.0.1:4317" cause url.Parse
193+ // to fail with "first path segment in URL cannot contain colon".
194+ if cfg .Endpoint != "" {
195+ cfg .Endpoint = endpointURL (cfg .Endpoint , cfg .Insecure )
196+ }
197+
191198 // Create resource with service information.
192199 res , err := newResource (cfg )
193200 if err != nil {
@@ -276,6 +283,21 @@ func newPropagator(cfg OTelConfig) propagation.TextMapPropagator {
276283 return propagation .NewCompositeTextMapPropagator (propagators ... )
277284}
278285
286+ // endpointURL ensures the endpoint has a URL scheme. The OTel SDK internally
287+ // reads OTEL_EXPORTER_OTLP_ENDPOINT and parses it with url.Parse, which fails
288+ // for bare IP:port values like "127.0.0.1:4317" with "first path segment in
289+ // URL cannot contain colon". Prepending a scheme based on the insecure flag
290+ // produces a valid URL the SDK can parse.
291+ func endpointURL (raw string , insecure bool ) string {
292+ if strings .Contains (raw , "://" ) {
293+ return raw
294+ }
295+ if insecure {
296+ return "http://" + raw
297+ }
298+ return "https://" + raw
299+ }
300+
279301// newTraceProvider creates a TracerProvider with an OTLP exporter configured based on the protocol setting.
280302func newTraceProvider (ctx context.Context , cfg OTelConfig , res * resource.Resource ) (* sdktrace.TracerProvider , error ) {
281303 var exporter sdktrace.SpanExporter
@@ -284,7 +306,7 @@ func newTraceProvider(ctx context.Context, cfg OTelConfig, res *resource.Resourc
284306 if cfg .Protocol == OTelProtocolHTTP {
285307 opts := []otlptracehttp.Option {}
286308 if cfg .Endpoint != "" {
287- opts = append (opts , otlptracehttp .WithEndpoint (cfg .Endpoint ))
309+ opts = append (opts , otlptracehttp .WithEndpointURL (cfg .Endpoint ))
288310 }
289311 if cfg .Insecure {
290312 opts = append (opts , otlptracehttp .WithInsecure ())
@@ -293,7 +315,7 @@ func newTraceProvider(ctx context.Context, cfg OTelConfig, res *resource.Resourc
293315 } else {
294316 opts := []otlptracegrpc.Option {}
295317 if cfg .Endpoint != "" {
296- opts = append (opts , otlptracegrpc .WithEndpoint (cfg .Endpoint ))
318+ opts = append (opts , otlptracegrpc .WithEndpointURL (cfg .Endpoint ))
297319 }
298320 if cfg .Insecure {
299321 opts = append (opts , otlptracegrpc .WithInsecure ())
@@ -323,7 +345,7 @@ func newMetricsProvider(ctx context.Context, cfg OTelConfig, res *resource.Resou
323345 if cfg .Protocol == OTelProtocolHTTP {
324346 opts := []otlpmetrichttp.Option {}
325347 if cfg .Endpoint != "" {
326- opts = append (opts , otlpmetrichttp .WithEndpoint (cfg .Endpoint ))
348+ opts = append (opts , otlpmetrichttp .WithEndpointURL (cfg .Endpoint ))
327349 }
328350 if cfg .Insecure {
329351 opts = append (opts , otlpmetrichttp .WithInsecure ())
@@ -332,7 +354,7 @@ func newMetricsProvider(ctx context.Context, cfg OTelConfig, res *resource.Resou
332354 } else {
333355 opts := []otlpmetricgrpc.Option {}
334356 if cfg .Endpoint != "" {
335- opts = append (opts , otlpmetricgrpc .WithEndpoint (cfg .Endpoint ))
357+ opts = append (opts , otlpmetricgrpc .WithEndpointURL (cfg .Endpoint ))
336358 }
337359 if cfg .Insecure {
338360 opts = append (opts , otlpmetricgrpc .WithInsecure ())
@@ -361,7 +383,7 @@ func newLoggerProvider(ctx context.Context, cfg OTelConfig, res *resource.Resour
361383 if cfg .Protocol == OTelProtocolHTTP {
362384 opts := []otlploghttp.Option {}
363385 if cfg .Endpoint != "" {
364- opts = append (opts , otlploghttp .WithEndpoint (cfg .Endpoint ))
386+ opts = append (opts , otlploghttp .WithEndpointURL (cfg .Endpoint ))
365387 }
366388 if cfg .Insecure {
367389 opts = append (opts , otlploghttp .WithInsecure ())
@@ -370,7 +392,7 @@ func newLoggerProvider(ctx context.Context, cfg OTelConfig, res *resource.Resour
370392 } else {
371393 opts := []otlploggrpc.Option {}
372394 if cfg .Endpoint != "" {
373- opts = append (opts , otlploggrpc .WithEndpoint (cfg .Endpoint ))
395+ opts = append (opts , otlploggrpc .WithEndpointURL (cfg .Endpoint ))
374396 }
375397 if cfg .Insecure {
376398 opts = append (opts , otlploggrpc .WithInsecure ())
0 commit comments