diff --git a/semconv/internal/v2/http.go b/semconv/internal/v2/http.go index 6989391cfbd..0f84a905b04 100644 --- a/semconv/internal/v2/http.go +++ b/semconv/internal/v2/http.go @@ -84,7 +84,7 @@ func (c *HTTPConv) ClientRequest(req *http.Request) []attribute.KeyValue { h = req.URL.Host } peer, p := firstHostPort(h, req.Header.Get("Host")) - port := requiredHTTPPort(req.TLS != nil, p) + port := requiredHTTPPort(req.URL != nil && req.URL.Scheme == "https", p) if port > 0 { n++ } diff --git a/semconv/internal/v2/http_test.go b/semconv/internal/v2/http_test.go index edfc7b50f27..73b9aa9b147 100644 --- a/semconv/internal/v2/http_test.go +++ b/semconv/internal/v2/http_test.go @@ -59,6 +59,31 @@ func TestHTTPClientResponse(t *testing.T) { }, got) } +func TestHTTPSClientRequest(t *testing.T) { + req := &http.Request{ + Method: http.MethodGet, + URL: &url.URL{ + Scheme: "https", + Host: "127.0.0.1:443", + Path: "/resource", + }, + Proto: "HTTP/1.0", + ProtoMajor: 1, + ProtoMinor: 0, + } + + assert.Equal( + t, + []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.url", "https://127.0.0.1:443/resource"), + attribute.String("net.peer.name", "127.0.0.1"), + }, + hc.ClientRequest(req), + ) +} + func TestHTTPClientRequest(t *testing.T) { const ( user = "alice"