From 68ff433868aae4b5f996512f6f86cdf089316b12 Mon Sep 17 00:00:00 2001 From: Nelz Date: Mon, 7 Mar 2022 16:04:47 -0800 Subject: [PATCH 1/3] fallback to URL.Host if Request.Host is empty --- semconv/v1.7.0/http.go | 2 ++ semconv/v1.7.0/http_test.go | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/semconv/v1.7.0/http.go b/semconv/v1.7.0/http.go index 9b430fac0c6..fafee88a953 100644 --- a/semconv/v1.7.0/http.go +++ b/semconv/v1.7.0/http.go @@ -167,6 +167,8 @@ func httpBasicAttributesFromHTTPRequest(request *http.Request) []attribute.KeyVa if request.Host != "" { attrs = append(attrs, HTTPHostKey.String(request.Host)) + } else if request.URL != nil && request.URL.Host != "" { + attrs = append(attrs, HTTPHostKey.String(request.URL.Host)) } flavor := "" diff --git a/semconv/v1.7.0/http_test.go b/semconv/v1.7.0/http_test.go index 5d707be5d61..2a76bd68714 100644 --- a/semconv/v1.7.0/http_test.go +++ b/semconv/v1.7.0/http_test.go @@ -705,6 +705,31 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { attribute.String("http.host", "example.com"), }, }, + { + name: "with host fallback", + serverName: "my-server-name", + route: "/user/:id", + method: "GET", + requestURI: "/user/123", + proto: "HTTP/1.0", + remoteAddr: "", + host: "", + url: &url.URL{ + Host: "example.com", + Path: "/user/123", + }, + header: nil, + tls: withTLS, + expected: []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.target", "/user/123"), + attribute.String("http.scheme", "https"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.server_name", "my-server-name"), + attribute.String("http.route", "/user/:id"), + attribute.String("http.host", "example.com"), + }, + }, { name: "with user agent", serverName: "my-server-name", @@ -1042,6 +1067,28 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { attribute.String("http.host", "example.com"), }, }, + { + name: "with host fallback", + method: "GET", + requestURI: "/user/123", + proto: "HTTP/1.0", + remoteAddr: "", + host: "", + url: &url.URL{ + Scheme: "https", + Host: "example.com", + Path: "/user/123", + }, + header: nil, + tls: withTLS, + expected: []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.url", "https://example.com/user/123"), + attribute.String("http.scheme", "https"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.host", "example.com"), + }, + }, { name: "with user agent", method: "GET", From 7447e100c90d795fc45369aeb64bbebfc4cf6bbe Mon Sep 17 00:00:00 2001 From: Nelz Date: Mon, 7 Mar 2022 16:33:14 -0800 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52cc0c413e7..b83debc78bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ This update is a breaking change of the unstable Metrics API. Code instrumented - Unify path cleaning functionally in the `otlpmetric` and `otlptrace` config. (#2639) - Change the debug message from the `sdk/trace.BatchSpanProcessor` to reflect the count is cumulative. (#2640) - Introduce new internal envconfig package for OTLP exporters (#2608) +- If `http.Request.Host` is empty, fall back to use `URL.Host` when populating `http.host` in the `semconv` packages. (#2661) ### Fixed From 08f165660ec4f92bda66edced1cd7ca12be85a6f Mon Sep 17 00:00:00 2001 From: Nelz Date: Mon, 7 Mar 2022 16:44:18 -0800 Subject: [PATCH 3/3] previous versions --- semconv/v1.4.0/http.go | 2 ++ semconv/v1.4.0/http_test.go | 47 +++++++++++++++++++++++++++++++++++++ semconv/v1.5.0/http.go | 2 ++ semconv/v1.5.0/http_test.go | 47 +++++++++++++++++++++++++++++++++++++ semconv/v1.6.1/http.go | 2 ++ semconv/v1.6.1/http_test.go | 47 +++++++++++++++++++++++++++++++++++++ 6 files changed, 147 insertions(+) diff --git a/semconv/v1.4.0/http.go b/semconv/v1.4.0/http.go index d9537ff3903..a9032ccc3c2 100644 --- a/semconv/v1.4.0/http.go +++ b/semconv/v1.4.0/http.go @@ -166,6 +166,8 @@ func httpBasicAttributesFromHTTPRequest(request *http.Request) []attribute.KeyVa if request.Host != "" { attrs = append(attrs, HTTPHostKey.String(request.Host)) + } else if request.URL != nil && request.URL.Host != "" { + attrs = append(attrs, HTTPHostKey.String(request.URL.Host)) } flavor := "" diff --git a/semconv/v1.4.0/http_test.go b/semconv/v1.4.0/http_test.go index 5d707be5d61..2a76bd68714 100644 --- a/semconv/v1.4.0/http_test.go +++ b/semconv/v1.4.0/http_test.go @@ -705,6 +705,31 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { attribute.String("http.host", "example.com"), }, }, + { + name: "with host fallback", + serverName: "my-server-name", + route: "/user/:id", + method: "GET", + requestURI: "/user/123", + proto: "HTTP/1.0", + remoteAddr: "", + host: "", + url: &url.URL{ + Host: "example.com", + Path: "/user/123", + }, + header: nil, + tls: withTLS, + expected: []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.target", "/user/123"), + attribute.String("http.scheme", "https"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.server_name", "my-server-name"), + attribute.String("http.route", "/user/:id"), + attribute.String("http.host", "example.com"), + }, + }, { name: "with user agent", serverName: "my-server-name", @@ -1042,6 +1067,28 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { attribute.String("http.host", "example.com"), }, }, + { + name: "with host fallback", + method: "GET", + requestURI: "/user/123", + proto: "HTTP/1.0", + remoteAddr: "", + host: "", + url: &url.URL{ + Scheme: "https", + Host: "example.com", + Path: "/user/123", + }, + header: nil, + tls: withTLS, + expected: []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.url", "https://example.com/user/123"), + attribute.String("http.scheme", "https"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.host", "example.com"), + }, + }, { name: "with user agent", method: "GET", diff --git a/semconv/v1.5.0/http.go b/semconv/v1.5.0/http.go index 1ab89a93aa8..114e24b0512 100644 --- a/semconv/v1.5.0/http.go +++ b/semconv/v1.5.0/http.go @@ -166,6 +166,8 @@ func httpBasicAttributesFromHTTPRequest(request *http.Request) []attribute.KeyVa if request.Host != "" { attrs = append(attrs, HTTPHostKey.String(request.Host)) + } else if request.URL != nil && request.URL.Host != "" { + attrs = append(attrs, HTTPHostKey.String(request.URL.Host)) } flavor := "" diff --git a/semconv/v1.5.0/http_test.go b/semconv/v1.5.0/http_test.go index af8567e1d18..c2043860bf7 100644 --- a/semconv/v1.5.0/http_test.go +++ b/semconv/v1.5.0/http_test.go @@ -705,6 +705,31 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { attribute.String("http.host", "example.com"), }, }, + { + name: "with host fallback", + serverName: "my-server-name", + route: "/user/:id", + method: "GET", + requestURI: "/user/123", + proto: "HTTP/1.0", + remoteAddr: "", + host: "", + url: &url.URL{ + Host: "example.com", + Path: "/user/123", + }, + header: nil, + tls: withTLS, + expected: []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.target", "/user/123"), + attribute.String("http.scheme", "https"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.server_name", "my-server-name"), + attribute.String("http.route", "/user/:id"), + attribute.String("http.host", "example.com"), + }, + }, { name: "with user agent", serverName: "my-server-name", @@ -1042,6 +1067,28 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { attribute.String("http.host", "example.com"), }, }, + { + name: "with host fallback", + method: "GET", + requestURI: "/user/123", + proto: "HTTP/1.0", + remoteAddr: "", + host: "", + url: &url.URL{ + Scheme: "https", + Host: "example.com", + Path: "/user/123", + }, + header: nil, + tls: withTLS, + expected: []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.url", "https://example.com/user/123"), + attribute.String("http.scheme", "https"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.host", "example.com"), + }, + }, { name: "with user agent", method: "GET", diff --git a/semconv/v1.6.1/http.go b/semconv/v1.6.1/http.go index 838e5135b00..d2194b3e47b 100644 --- a/semconv/v1.6.1/http.go +++ b/semconv/v1.6.1/http.go @@ -166,6 +166,8 @@ func httpBasicAttributesFromHTTPRequest(request *http.Request) []attribute.KeyVa if request.Host != "" { attrs = append(attrs, HTTPHostKey.String(request.Host)) + } else if request.URL != nil && request.URL.Host != "" { + attrs = append(attrs, HTTPHostKey.String(request.URL.Host)) } flavor := "" diff --git a/semconv/v1.6.1/http_test.go b/semconv/v1.6.1/http_test.go index 017bd13502d..4b4a652c74d 100644 --- a/semconv/v1.6.1/http_test.go +++ b/semconv/v1.6.1/http_test.go @@ -704,6 +704,31 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { attribute.String("http.host", "example.com"), }, }, + { + name: "with host fallback", + serverName: "my-server-name", + route: "/user/:id", + method: "GET", + requestURI: "/user/123", + proto: "HTTP/1.0", + remoteAddr: "", + host: "", + url: &url.URL{ + Host: "example.com", + Path: "/user/123", + }, + header: nil, + tls: withTLS, + expected: []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.target", "/user/123"), + attribute.String("http.scheme", "https"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.server_name", "my-server-name"), + attribute.String("http.route", "/user/:id"), + attribute.String("http.host", "example.com"), + }, + }, { name: "with user agent", serverName: "my-server-name", @@ -1041,6 +1066,28 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { attribute.String("http.host", "example.com"), }, }, + { + name: "with host fallback", + method: "GET", + requestURI: "/user/123", + proto: "HTTP/1.0", + remoteAddr: "", + host: "", + url: &url.URL{ + Scheme: "https", + Host: "example.com", + Path: "/user/123", + }, + header: nil, + tls: withTLS, + expected: []attribute.KeyValue{ + attribute.String("http.method", "GET"), + attribute.String("http.url", "https://example.com/user/123"), + attribute.String("http.scheme", "https"), + attribute.String("http.flavor", "1.0"), + attribute.String("http.host", "example.com"), + }, + }, { name: "with user agent", method: "GET",