-
Notifications
You must be signed in to change notification settings - Fork 576
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
otelhttptrace: add TLS info to the "http.tls" span #5563
base: main
Are you sure you want to change the base?
Changes from all commits
5884ee8
444054a
7945bc6
e30784e
3469bf8
aa211d3
427c5c4
f7116c0
de7bef7
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 |
---|---|---|
|
@@ -6,9 +6,14 @@ package test | |
import ( | ||
"bytes" | ||
"context" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httptrace" | ||
"net/url" | ||
"slices" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -42,107 +47,153 @@ func getSpansFromRecorder(sr *tracetest.SpanRecorder, name string) []trace.ReadO | |
} | ||
|
||
func TestHTTPRequestWithClientTrace(t *testing.T) { | ||
sr := tracetest.NewSpanRecorder() | ||
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) | ||
otel.SetTracerProvider(tp) | ||
tr := tp.Tracer("httptrace/client") | ||
|
||
// Mock http server | ||
ts := httptest.NewServer( | ||
// Mock http server, one without TLS and another with TLS. | ||
tsHTTP := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
}), | ||
) | ||
defer ts.Close() | ||
address := ts.Listener.Addr() | ||
defer tsHTTP.Close() | ||
|
||
client := ts.Client() | ||
err := func(ctx context.Context) error { | ||
ctx, span := tr.Start(ctx, "test") | ||
defer span.End() | ||
req, _ := http.NewRequest("GET", ts.URL, nil) | ||
_, req = otelhttptrace.W3C(ctx, req) | ||
tsHTTPS := httptest.NewTLSServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
}), | ||
) | ||
defer tsHTTPS.Close() | ||
|
||
for _, ts := range []*httptest.Server{tsHTTP, tsHTTPS} { | ||
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. If we're taking the approach of subtests, I think it would be better to loop through anonymous structs with a |
||
sr := tracetest.NewSpanRecorder() | ||
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) | ||
otel.SetTracerProvider(tp) | ||
tr := tp.Tracer("httptrace/client") | ||
|
||
err := func(ctx context.Context) error { | ||
ctx, span := tr.Start(ctx, "test") | ||
defer span.End() | ||
req, _ := http.NewRequest("GET", ts.URL, nil) | ||
_, req = otelhttptrace.W3C(ctx, req) | ||
|
||
res, err := ts.Client().Do(req) | ||
if err != nil { | ||
t.Fatalf("Request failed: %s", err.Error()) | ||
} | ||
_ = res.Body.Close() | ||
|
||
res, err := client.Do(req) | ||
return nil | ||
}(context.Background()) | ||
if err != nil { | ||
t.Fatalf("Request failed: %s", err.Error()) | ||
panic("unexpected error in http request: " + err.Error()) | ||
} | ||
_ = res.Body.Close() | ||
|
||
return nil | ||
}(context.Background()) | ||
if err != nil { | ||
panic("unexpected error in http request: " + err.Error()) | ||
} | ||
type tc struct { | ||
name string | ||
attributes []attribute.KeyValue | ||
parent string | ||
} | ||
|
||
testLen := []struct { | ||
name string | ||
attributes []attribute.KeyValue | ||
parent string | ||
}{ | ||
{ | ||
name: "http.connect", | ||
attributes: []attribute.KeyValue{ | ||
attribute.Key("http.conn.done.addr").String(address.String()), | ||
attribute.Key("http.conn.done.network").String("tcp"), | ||
attribute.Key("http.conn.start.network").String("tcp"), | ||
attribute.Key("http.remote").String(address.String()), | ||
testLen := []tc{ | ||
{ | ||
name: "http.connect", | ||
attributes: []attribute.KeyValue{ | ||
attribute.Key("http.conn.done.addr").String(ts.Listener.Addr().String()), | ||
attribute.Key("http.conn.done.network").String("tcp"), | ||
attribute.Key("http.conn.start.network").String("tcp"), | ||
attribute.Key("http.remote").String(ts.Listener.Addr().String()), | ||
}, | ||
parent: "http.getconn", | ||
}, | ||
parent: "http.getconn", | ||
}, | ||
{ | ||
name: "http.getconn", | ||
attributes: []attribute.KeyValue{ | ||
attribute.Key("http.remote").String(address.String()), | ||
attribute.Key("net.host.name").String(address.String()), | ||
attribute.Key("http.conn.reused").Bool(false), | ||
attribute.Key("http.conn.wasidle").Bool(false), | ||
{ | ||
name: "http.getconn", | ||
attributes: []attribute.KeyValue{ | ||
attribute.Key("http.remote").String(ts.Listener.Addr().String()), | ||
attribute.Key("net.host.name").String(ts.Listener.Addr().String()), | ||
attribute.Key("http.conn.reused").Bool(false), | ||
attribute.Key("http.conn.wasidle").Bool(false), | ||
}, | ||
parent: "test", | ||
}, | ||
{ | ||
name: "http.receive", | ||
parent: "test", | ||
}, | ||
{ | ||
name: "http.headers", | ||
parent: "test", | ||
}, | ||
{ | ||
name: "http.send", | ||
parent: "test", | ||
}, | ||
{ | ||
name: "test", | ||
}, | ||
parent: "test", | ||
}, | ||
{ | ||
name: "http.receive", | ||
parent: "test", | ||
}, | ||
{ | ||
name: "http.headers", | ||
parent: "test", | ||
}, | ||
{ | ||
name: "http.send", | ||
parent: "test", | ||
}, | ||
{ | ||
name: "test", | ||
}, | ||
} | ||
for _, tl := range testLen { | ||
span, ok := getSpanFromRecorder(sr, tl.name) | ||
if !assert.True(t, ok) { | ||
continue | ||
} | ||
|
||
if tl.parent != "" { | ||
parent, ok := getSpanFromRecorder(sr, tl.parent) | ||
if assert.True(t, ok) { | ||
assert.Equal(t, span.Parent().SpanID(), parent.SpanContext().SpanID()) | ||
} | ||
u, err := url.Parse(ts.URL) | ||
if err != nil { | ||
panic("unexpected error in parsing httptest server URL: " + err.Error()) | ||
} | ||
// http.tls only exists on HTTPS connections. | ||
if u.Scheme == "https" { | ||
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. There shouldn't be conditions like this in a subtest loop. These should be part of the attributes defined in the loop definition. |
||
testLen = append([]tc{{ | ||
name: "http.tls", | ||
attributes: []attribute.KeyValue{ | ||
attribute.Key("tls.server.certificate_chain").StringSlice( | ||
[]string{base64.StdEncoding.EncodeToString(ts.Certificate().Raw)}, | ||
), | ||
attribute.Key("tls.server.hash.sha256"). | ||
String(fmt.Sprintf("%X", sha256.Sum256(ts.Certificate().Raw))), | ||
attribute.Key("tls.server.not_after"). | ||
String(ts.Certificate().NotAfter.UTC().Format(time.RFC3339)), | ||
attribute.Key("tls.server.not_before"). | ||
String(ts.Certificate().NotBefore.UTC().Format(time.RFC3339)), | ||
}, | ||
parent: "http.getconn", | ||
}}, testLen...) | ||
} | ||
if len(tl.attributes) > 0 { | ||
attrs := span.Attributes() | ||
if tl.name == "http.getconn" { | ||
// http.local attribute uses a non-deterministic port. | ||
local := attribute.Key("http.local") | ||
var contains bool | ||
for i, a := range attrs { | ||
if a.Key == local { | ||
attrs = append(attrs[:i], attrs[i+1:]...) | ||
contains = true | ||
break | ||
|
||
for i, tl := range testLen { | ||
span, ok := getSpanFromRecorder(sr, tl.name) | ||
if !assert.True(t, ok) { | ||
continue | ||
} | ||
|
||
if tl.parent != "" { | ||
parent, ok := getSpanFromRecorder(sr, tl.parent) | ||
if assert.True(t, ok) { | ||
assert.Equal(t, span.Parent().SpanID(), parent.SpanContext().SpanID()) | ||
} | ||
} | ||
if len(tl.attributes) > 0 { | ||
attrs := span.Attributes() | ||
if tl.name == "http.getconn" { | ||
// http.local attribute uses a non-deterministic port. | ||
local := attribute.Key("http.local") | ||
var contains bool | ||
for i, a := range attrs { | ||
if a.Key == local { | ||
attrs = append(attrs[:i], attrs[i+1:]...) | ||
contains = true | ||
break | ||
} | ||
} | ||
assert.True(t, contains, "missing http.local attribute") | ||
} | ||
if tl.name == "http.tls" { | ||
if i == 0 { | ||
tl.attributes = append(tl.attributes, attribute.Key("tls.resumed").Bool(false)) | ||
} else { | ||
tl.attributes = append(tl.attributes, attribute.Key("tls.resumed").Bool(true)) | ||
} | ||
attrs = slices.DeleteFunc(attrs, func(a attribute.KeyValue) bool { | ||
// Skip keys that are unable to be detected beforehand. | ||
if a.Key == otelhttptrace.TLSCipherKey || a.Key == otelhttptrace.TLSProtocolVersionKey { | ||
return true | ||
} | ||
return false | ||
}) | ||
} | ||
assert.True(t, contains, "missing http.local attribute") | ||
assert.ElementsMatch(t, tl.attributes, attrs) | ||
} | ||
assert.ElementsMatch(t, tl.attributes, attrs) | ||
} | ||
} | ||
} | ||
|
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.