1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-23 22:34:47 +02:00

Update ClientRequest HTTPS determination (#3577)

* Update ClientRequest HTTPS determination

The ClientRequest function will only report a peer port attribute if
that peer port differs from the standard 80 for HTTP and 443 for HTTPS.
In determining if the request is for HTTPS use the request URL scheme.
This is not perfect. If a user doesn't provide a scheme this will not be
correctly detected. However, the current approach of checking if the
`TLS` field is non-nil will always be wrong, requests made by client
ignore this field and it is always nil. Therefore, switching to using
the URL field is the best we can do without having already made the
request.

* Test HTTPS detection for ClientRequest
This commit is contained in:
Tyler Yahn
2023-01-12 16:01:51 -08:00
committed by GitHub
parent 640a0cd8bc
commit 42863522e5
2 changed files with 26 additions and 1 deletions

View File

@@ -84,7 +84,7 @@ func (c *HTTPConv) ClientRequest(req *http.Request) []attribute.KeyValue {
h = req.URL.Host h = req.URL.Host
} }
peer, p := firstHostPort(h, req.Header.Get("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 { if port > 0 {
n++ n++
} }

View File

@@ -59,6 +59,31 @@ func TestHTTPClientResponse(t *testing.T) {
}, got) }, 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) { func TestHTTPClientRequest(t *testing.T) {
const ( const (
user = "alice" user = "alice"