| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelhttptrace_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"context" |
| 9 |
"net/http" |
| 10 |
"net/http/httptest" |
| 11 |
"net/http/httptrace" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
"go.opentelemetry.io/otel" |
| 18 |
"go.opentelemetry.io/otel/attribute" |
| 19 |
"go.opentelemetry.io/otel/sdk/trace" |
| 20 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 21 |
|
| 22 |
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace" |
| 23 |
) |
| 24 |
|
| 25 |
func getSpanFromRecorder(sr *tracetest.SpanRecorder, name string) (trace.ReadOnlySpan, bool) { |
| 26 |
for _, s := range sr.Ended() { |
| 27 |
if s.Name() == name { |
| 28 |
return s, true |
| 29 |
} |
| 30 |
} |
| 31 |
return nil, false |
| 32 |
} |
| 33 |
|
| 34 |
func getSpansFromRecorder(sr *tracetest.SpanRecorder, name string) []trace.ReadOnlySpan { |
| 35 |
var ret []trace.ReadOnlySpan |
| 36 |
for _, s := range sr.Ended() { |
| 37 |
if s.Name() == name { |
| 38 |
ret = append(ret, s) |
| 39 |
} |
| 40 |
} |
| 41 |
return ret |
| 42 |
} |
| 43 |
|
| 44 |
func TestHTTPRequestWithClientTrace(t *testing.T) { |
| 45 |
sr := tracetest.NewSpanRecorder() |
| 46 |
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 47 |
otel.SetTracerProvider(tp) |
| 48 |
tr := tp.Tracer("httptrace/client") |
| 49 |
|
| 50 |
|
| 51 |
ts := httptest.NewServer( |
| 52 |
http.HandlerFunc(func(http.ResponseWriter, *http.Request) { |
| 53 |
}), |
| 54 |
) |
| 55 |
defer ts.Close() |
| 56 |
address := ts.Listener.Addr() |
| 57 |
|
| 58 |
client := ts.Client() |
| 59 |
err := func(ctx context.Context) error { |
| 60 |
ctx, span := tr.Start(ctx, "test") |
| 61 |
defer span.End() |
| 62 |
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, http.NoBody) |
| 63 |
_, req = otelhttptrace.W3C(ctx, req) |
| 64 |
|
| 65 |
res, err := client.Do(req) |
| 66 |
if err != nil { |
| 67 |
t.Fatalf("Request failed: %s", err.Error()) |
| 68 |
} |
| 69 |
_ = res.Body.Close() |
| 70 |
|
| 71 |
return nil |
| 72 |
}(t.Context()) |
| 73 |
if err != nil { |
| 74 |
panic("unexpected error in http request: " + err.Error()) |
| 75 |
} |
| 76 |
|
| 77 |
testLen := []struct { |
| 78 |
name string |
| 79 |
attributes []attribute.KeyValue |
| 80 |
parent string |
| 81 |
}{ |
| 82 |
{ |
| 83 |
name: "http.connect", |
| 84 |
attributes: []attribute.KeyValue{ |
| 85 |
attribute.Key("http.conn.done.addr").String(address.String()), |
| 86 |
attribute.Key("http.conn.done.network").String("tcp"), |
| 87 |
attribute.Key("http.conn.start.network").String("tcp"), |
| 88 |
attribute.Key("http.remote").String(address.String()), |
| 89 |
}, |
| 90 |
parent: "http.getconn", |
| 91 |
}, |
| 92 |
{ |
| 93 |
name: "http.getconn", |
| 94 |
attributes: []attribute.KeyValue{ |
| 95 |
attribute.Key("http.remote").String(address.String()), |
| 96 |
attribute.Key("server.address").String(address.String()), |
| 97 |
attribute.Key("http.conn.reused").Bool(false), |
| 98 |
attribute.Key("http.conn.wasidle").Bool(false), |
| 99 |
}, |
| 100 |
parent: "test", |
| 101 |
}, |
| 102 |
{ |
| 103 |
name: "http.receive", |
| 104 |
parent: "test", |
| 105 |
}, |
| 106 |
{ |
| 107 |
name: "http.headers", |
| 108 |
parent: "test", |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "http.send", |
| 112 |
parent: "test", |
| 113 |
}, |
| 114 |
{ |
| 115 |
name: "test", |
| 116 |
}, |
| 117 |
} |
| 118 |
for _, tl := range testLen { |
| 119 |
span, ok := getSpanFromRecorder(sr, tl.name) |
| 120 |
if !assert.True(t, ok) { |
| 121 |
continue |
| 122 |
} |
| 123 |
|
| 124 |
if tl.parent != "" { |
| 125 |
parent, ok := getSpanFromRecorder(sr, tl.parent) |
| 126 |
if assert.True(t, ok) { |
| 127 |
assert.Equal(t, span.Parent().SpanID(), parent.SpanContext().SpanID()) |
| 128 |
} |
| 129 |
} |
| 130 |
if len(tl.attributes) > 0 { |
| 131 |
attrs := span.Attributes() |
| 132 |
if tl.name == "http.getconn" { |
| 133 |
|
| 134 |
local := attribute.Key("http.local") |
| 135 |
var contains bool |
| 136 |
for i, a := range attrs { |
| 137 |
if a.Key == local { |
| 138 |
attrs = append(attrs[:i], attrs[i+1:]...) |
| 139 |
contains = true |
| 140 |
break |
| 141 |
} |
| 142 |
} |
| 143 |
assert.True(t, contains, "missing http.local attribute") |
| 144 |
} |
| 145 |
assert.ElementsMatch(t, tl.attributes, attrs) |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func TestConcurrentConnectionStart(t *testing.T) { |
| 151 |
tts := []struct { |
| 152 |
name string |
| 153 |
run func(*httptrace.ClientTrace) |
| 154 |
}{ |
| 155 |
{ |
| 156 |
name: "Open1Close1Open2Close2", |
| 157 |
run: func(ct *httptrace.ClientTrace) { |
| 158 |
ct.ConnectStart("tcp", "127.0.0.1:3000") |
| 159 |
ct.ConnectDone("tcp", "127.0.0.1:3000", nil) |
| 160 |
ct.ConnectStart("tcp", "[::1]:3000") |
| 161 |
ct.ConnectDone("tcp", "[::1]:3000", nil) |
| 162 |
}, |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "Open2Close2Open1Close1", |
| 166 |
run: func(ct *httptrace.ClientTrace) { |
| 167 |
ct.ConnectStart("tcp", "[::1]:3000") |
| 168 |
ct.ConnectDone("tcp", "[::1]:3000", nil) |
| 169 |
ct.ConnectStart("tcp", "127.0.0.1:3000") |
| 170 |
ct.ConnectDone("tcp", "127.0.0.1:3000", nil) |
| 171 |
}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
name: "Open1Open2Close1Close2", |
| 175 |
run: func(ct *httptrace.ClientTrace) { |
| 176 |
ct.ConnectStart("tcp", "127.0.0.1:3000") |
| 177 |
ct.ConnectStart("tcp", "[::1]:3000") |
| 178 |
ct.ConnectDone("tcp", "127.0.0.1:3000", nil) |
| 179 |
ct.ConnectDone("tcp", "[::1]:3000", nil) |
| 180 |
}, |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "Open1Open2Close2Close1", |
| 184 |
run: func(ct *httptrace.ClientTrace) { |
| 185 |
ct.ConnectStart("tcp", "127.0.0.1:3000") |
| 186 |
ct.ConnectStart("tcp", "[::1]:3000") |
| 187 |
ct.ConnectDone("tcp", "[::1]:3000", nil) |
| 188 |
ct.ConnectDone("tcp", "127.0.0.1:3000", nil) |
| 189 |
}, |
| 190 |
}, |
| 191 |
{ |
| 192 |
name: "Open2Open1Close1Close2", |
| 193 |
run: func(ct *httptrace.ClientTrace) { |
| 194 |
ct.ConnectStart("tcp", "[::1]:3000") |
| 195 |
ct.ConnectStart("tcp", "127.0.0.1:3000") |
| 196 |
ct.ConnectDone("tcp", "127.0.0.1:3000", nil) |
| 197 |
ct.ConnectDone("tcp", "[::1]:3000", nil) |
| 198 |
}, |
| 199 |
}, |
| 200 |
{ |
| 201 |
name: "Open2Open1Close2Close1", |
| 202 |
run: func(ct *httptrace.ClientTrace) { |
| 203 |
ct.ConnectStart("tcp", "[::1]:3000") |
| 204 |
ct.ConnectStart("tcp", "127.0.0.1:3000") |
| 205 |
ct.ConnectDone("tcp", "[::1]:3000", nil) |
| 206 |
ct.ConnectDone("tcp", "127.0.0.1:3000", nil) |
| 207 |
}, |
| 208 |
}, |
| 209 |
} |
| 210 |
|
| 211 |
expectedRemotes := []attribute.KeyValue{ |
| 212 |
attribute.String("http.remote", "127.0.0.1:3000"), |
| 213 |
attribute.String("http.conn.start.network", "tcp"), |
| 214 |
attribute.String("http.conn.done.addr", "127.0.0.1:3000"), |
| 215 |
attribute.String("http.conn.done.network", "tcp"), |
| 216 |
attribute.String("http.remote", "[::1]:3000"), |
| 217 |
attribute.String("http.conn.start.network", "tcp"), |
| 218 |
attribute.String("http.conn.done.addr", "[::1]:3000"), |
| 219 |
attribute.String("http.conn.done.network", "tcp"), |
| 220 |
} |
| 221 |
for _, tt := range tts { |
| 222 |
t.Run(tt.name, func(t *testing.T) { |
| 223 |
sr := tracetest.NewSpanRecorder() |
| 224 |
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 225 |
otel.SetTracerProvider(tp) |
| 226 |
tt.run(otelhttptrace.NewClientTrace(t.Context())) |
| 227 |
spans := getSpansFromRecorder(sr, "http.connect") |
| 228 |
require.Len(t, spans, 2) |
| 229 |
|
| 230 |
var gotRemotes []attribute.KeyValue |
| 231 |
for _, span := range spans { |
| 232 |
gotRemotes = append(gotRemotes, span.Attributes()...) |
| 233 |
} |
| 234 |
assert.ElementsMatch(t, expectedRemotes, gotRemotes) |
| 235 |
}) |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func TestEndBeforeStartCreatesSpan(t *testing.T) { |
| 240 |
sr := tracetest.NewSpanRecorder() |
| 241 |
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 242 |
otel.SetTracerProvider(tp) |
| 243 |
|
| 244 |
ct := otelhttptrace.NewClientTrace(t.Context()) |
| 245 |
ct.DNSDone(httptrace.DNSDoneInfo{}) |
| 246 |
ct.DNSStart(httptrace.DNSStartInfo{Host: "example.com"}) |
| 247 |
|
| 248 |
name := "http.dns" |
| 249 |
spans := getSpansFromRecorder(sr, name) |
| 250 |
require.Len(t, spans, 1) |
| 251 |
} |
| 252 |
|
| 253 |
func TestEndBeforeStartWithoutSubSpansDoesNotPanic(t *testing.T) { |
| 254 |
sr := tracetest.NewSpanRecorder() |
| 255 |
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 256 |
otel.SetTracerProvider(tp) |
| 257 |
|
| 258 |
ct := otelhttptrace.NewClientTrace(t.Context(), otelhttptrace.WithoutSubSpans()) |
| 259 |
|
| 260 |
require.NotPanics(t, func() { |
| 261 |
ct.DNSDone(httptrace.DNSDoneInfo{}) |
| 262 |
}) |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
require.Empty(t, sr.Ended()) |
| 267 |
} |
| 268 |
|
| 269 |
func TestNoClientTraceCallGuarantee(t *testing.T) { |
| 270 |
t.Run("Got100Continue", func(t *testing.T) { |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
assert.NotPanics(t, func() { |
| 275 |
clientTrace := otelhttptrace.NewClientTrace(t.Context()) |
| 276 |
clientTrace.Got100Continue() |
| 277 |
}) |
| 278 |
}) |
| 279 |
t.Run("Got1xxResponse", func(t *testing.T) { |
| 280 |
clientTrace := otelhttptrace.NewClientTrace(t.Context()) |
| 281 |
err := clientTrace.Got1xxResponse(http.StatusNoContent, nil) |
| 282 |
assert.NoError(t, err) |
| 283 |
}) |
| 284 |
t.Run("Wait100Continue", func(t *testing.T) { |
| 285 |
assert.NotPanics(t, func() { |
| 286 |
clientTrace := otelhttptrace.NewClientTrace(t.Context()) |
| 287 |
clientTrace.Wait100Continue() |
| 288 |
}) |
| 289 |
}) |
| 290 |
} |
| 291 |
|
| 292 |
type clientTraceTestFixture struct { |
| 293 |
Address string |
| 294 |
URL string |
| 295 |
Client *http.Client |
| 296 |
SpanRecorder *tracetest.SpanRecorder |
| 297 |
} |
| 298 |
|
| 299 |
func prepareClientTraceTest(t *testing.T) clientTraceTestFixture { |
| 300 |
fixture := clientTraceTestFixture{} |
| 301 |
fixture.SpanRecorder = tracetest.NewSpanRecorder() |
| 302 |
otel.SetTracerProvider( |
| 303 |
trace.NewTracerProvider(trace.WithSpanProcessor(fixture.SpanRecorder)), |
| 304 |
) |
| 305 |
|
| 306 |
ts := httptest.NewServer( |
| 307 |
http.HandlerFunc(func(http.ResponseWriter, *http.Request) { |
| 308 |
}), |
| 309 |
) |
| 310 |
t.Cleanup(ts.Close) |
| 311 |
fixture.Client = ts.Client() |
| 312 |
fixture.URL = ts.URL |
| 313 |
fixture.Address = ts.Listener.Addr().String() |
| 314 |
return fixture |
| 315 |
} |
| 316 |
|
| 317 |
func TestWithoutSubSpans(t *testing.T) { |
| 318 |
fixture := prepareClientTraceTest(t) |
| 319 |
|
| 320 |
ctx := t.Context() |
| 321 |
ctx = httptrace.WithClientTrace(ctx, |
| 322 |
otelhttptrace.NewClientTrace(ctx, |
| 323 |
otelhttptrace.WithoutSubSpans(), |
| 324 |
), |
| 325 |
) |
| 326 |
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, http.NoBody) |
| 327 |
require.NoError(t, err) |
| 328 |
resp, err := fixture.Client.Do(req) |
| 329 |
require.NoError(t, err) |
| 330 |
require.NoError(t, resp.Body.Close()) |
| 331 |
|
| 332 |
require.Empty(t, fixture.SpanRecorder.Ended()) |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
ctx, span := otel.Tracer("oteltest").Start(t.Context(), "root") |
| 337 |
ctx = httptrace.WithClientTrace(ctx, |
| 338 |
otelhttptrace.NewClientTrace(ctx, |
| 339 |
otelhttptrace.WithoutSubSpans(), |
| 340 |
), |
| 341 |
) |
| 342 |
req, err = http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, http.NoBody) |
| 343 |
req.Header.Set("User-Agent", "oteltest/1.1") |
| 344 |
req.Header.Set("Authorization", "Bearer token123") |
| 345 |
require.NoError(t, err) |
| 346 |
resp, err = fixture.Client.Do(req) |
| 347 |
require.NoError(t, err) |
| 348 |
require.NoError(t, resp.Body.Close()) |
| 349 |
span.End() |
| 350 |
|
| 351 |
require.Len(t, fixture.SpanRecorder.Ended(), 1) |
| 352 |
recSpan := fixture.SpanRecorder.Ended()[0] |
| 353 |
|
| 354 |
gotAttributes := recSpan.Attributes() |
| 355 |
require.Len(t, gotAttributes, 4) |
| 356 |
assert.Equal(t, |
| 357 |
[]attribute.KeyValue{ |
| 358 |
attribute.Key("http.request.header.host").String(fixture.Address), |
| 359 |
attribute.Key("http.request.header.user-agent").String("oteltest/1.1"), |
| 360 |
attribute.Key("http.request.header.authorization").String("****"), |
| 361 |
attribute.Key("http.request.header.accept-encoding").String("gzip"), |
| 362 |
}, |
| 363 |
gotAttributes, |
| 364 |
) |
| 365 |
|
| 366 |
type attrMap = map[attribute.Key]attribute.Value |
| 367 |
expectedEvents := []struct { |
| 368 |
Event string |
| 369 |
VerifyAttrs func(t *testing.T, got attrMap) |
| 370 |
}{ |
| 371 |
{"http.getconn.start", func(t *testing.T, got attrMap) { |
| 372 |
assert.Equal(t, |
| 373 |
attribute.StringValue(fixture.Address), |
| 374 |
got[attribute.Key("server.address")], |
| 375 |
) |
| 376 |
}}, |
| 377 |
{"http.getconn.done", func(t *testing.T, got attrMap) { |
| 378 |
|
| 379 |
assert.Contains(t, got, attribute.Key("http.conn.idletime")) |
| 380 |
assert.Equal(t, |
| 381 |
attribute.BoolValue(true), |
| 382 |
got[attribute.Key("http.conn.reused")], |
| 383 |
) |
| 384 |
assert.Equal(t, |
| 385 |
attribute.BoolValue(true), |
| 386 |
got[attribute.Key("http.conn.wasidle")], |
| 387 |
) |
| 388 |
assert.Equal(t, |
| 389 |
attribute.StringValue(fixture.Address), |
| 390 |
got[attribute.Key("http.remote")], |
| 391 |
) |
| 392 |
|
| 393 |
assert.Contains(t, got, attribute.Key("http.local")) |
| 394 |
}}, |
| 395 |
{"http.send.start", nil}, |
| 396 |
{"http.send.done", nil}, |
| 397 |
{"http.receive.start", nil}, |
| 398 |
{"http.receive.done", nil}, |
| 399 |
} |
| 400 |
require.Len(t, recSpan.Events(), len(expectedEvents)) |
| 401 |
for i, e := range recSpan.Events() { |
| 402 |
attrs := attrMap{} |
| 403 |
for _, a := range e.Attributes { |
| 404 |
attrs[a.Key] = a.Value |
| 405 |
} |
| 406 |
expected := expectedEvents[i] |
| 407 |
assert.Equal(t, expected.Event, e.Name) |
| 408 |
if expected.VerifyAttrs == nil { |
| 409 |
assert.Nil(t, e.Attributes, "Event %q has no attributes", e.Name) |
| 410 |
} else { |
| 411 |
e := e |
| 412 |
t.Run(e.Name, func(t *testing.T) { |
| 413 |
expected.VerifyAttrs(t, attrs) |
| 414 |
}) |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
func TestWithRedactedHeaders(t *testing.T) { |
| 420 |
fixture := prepareClientTraceTest(t) |
| 421 |
|
| 422 |
ctx, span := otel.Tracer("oteltest").Start(t.Context(), "root") |
| 423 |
ctx = httptrace.WithClientTrace(ctx, |
| 424 |
otelhttptrace.NewClientTrace(ctx, |
| 425 |
otelhttptrace.WithoutSubSpans(), |
| 426 |
otelhttptrace.WithRedactedHeaders("user-agent"), |
| 427 |
), |
| 428 |
) |
| 429 |
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, http.NoBody) |
| 430 |
require.NoError(t, err) |
| 431 |
resp, err := fixture.Client.Do(req) |
| 432 |
require.NoError(t, err) |
| 433 |
require.NoError(t, resp.Body.Close()) |
| 434 |
span.End() |
| 435 |
require.Len(t, fixture.SpanRecorder.Ended(), 1) |
| 436 |
recSpan := fixture.SpanRecorder.Ended()[0] |
| 437 |
|
| 438 |
gotAttributes := recSpan.Attributes() |
| 439 |
assert.Equal(t, |
| 440 |
[]attribute.KeyValue{ |
| 441 |
attribute.Key("http.request.header.host").String(fixture.Address), |
| 442 |
attribute.Key("http.request.header.user-agent").String("****"), |
| 443 |
attribute.Key("http.request.header.accept-encoding").String("gzip"), |
| 444 |
}, |
| 445 |
gotAttributes, |
| 446 |
) |
| 447 |
} |
| 448 |
|
| 449 |
func TestWithoutHeaders(t *testing.T) { |
| 450 |
fixture := prepareClientTraceTest(t) |
| 451 |
|
| 452 |
ctx, span := otel.Tracer("oteltest").Start(t.Context(), "root") |
| 453 |
ctx = httptrace.WithClientTrace(ctx, |
| 454 |
otelhttptrace.NewClientTrace(ctx, |
| 455 |
otelhttptrace.WithoutSubSpans(), |
| 456 |
otelhttptrace.WithoutHeaders(), |
| 457 |
), |
| 458 |
) |
| 459 |
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, http.NoBody) |
| 460 |
require.NoError(t, err) |
| 461 |
resp, err := fixture.Client.Do(req) |
| 462 |
require.NoError(t, err) |
| 463 |
require.NoError(t, resp.Body.Close()) |
| 464 |
span.End() |
| 465 |
require.Len(t, fixture.SpanRecorder.Ended(), 1) |
| 466 |
recSpan := fixture.SpanRecorder.Ended()[0] |
| 467 |
|
| 468 |
gotAttributes := recSpan.Attributes() |
| 469 |
require.Empty(t, gotAttributes) |
| 470 |
} |
| 471 |
|
| 472 |
func TestWithInsecureHeaders(t *testing.T) { |
| 473 |
fixture := prepareClientTraceTest(t) |
| 474 |
|
| 475 |
ctx, span := otel.Tracer("oteltest").Start(t.Context(), "root") |
| 476 |
ctx = httptrace.WithClientTrace(ctx, |
| 477 |
otelhttptrace.NewClientTrace(ctx, |
| 478 |
otelhttptrace.WithoutSubSpans(), |
| 479 |
otelhttptrace.WithInsecureHeaders(), |
| 480 |
), |
| 481 |
) |
| 482 |
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, http.NoBody) |
| 483 |
req.Header.Set("User-Agent", "oteltest/1.1") |
| 484 |
req.Header.Set("Authorization", "Bearer token123") |
| 485 |
require.NoError(t, err) |
| 486 |
resp, err := fixture.Client.Do(req) |
| 487 |
require.NoError(t, err) |
| 488 |
require.NoError(t, resp.Body.Close()) |
| 489 |
span.End() |
| 490 |
require.Len(t, fixture.SpanRecorder.Ended(), 1) |
| 491 |
recSpan := fixture.SpanRecorder.Ended()[0] |
| 492 |
|
| 493 |
gotAttributes := recSpan.Attributes() |
| 494 |
assert.Equal(t, |
| 495 |
[]attribute.KeyValue{ |
| 496 |
attribute.Key("http.request.header.host").String(fixture.Address), |
| 497 |
attribute.Key("http.request.header.user-agent").String("oteltest/1.1"), |
| 498 |
attribute.Key("http.request.header.authorization").String("Bearer token123"), |
| 499 |
attribute.Key("http.request.header.accept-encoding").String("gzip"), |
| 500 |
}, |
| 501 |
gotAttributes, |
| 502 |
) |
| 503 |
} |
| 504 |
|
| 505 |
func TestHTTPRequestWithTraceContext(t *testing.T) { |
| 506 |
sr := tracetest.NewSpanRecorder() |
| 507 |
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 508 |
|
| 509 |
|
| 510 |
ts := httptest.NewServer( |
| 511 |
http.HandlerFunc(func(http.ResponseWriter, *http.Request) { |
| 512 |
}), |
| 513 |
) |
| 514 |
defer ts.Close() |
| 515 |
|
| 516 |
ctx, span := tp.Tracer("").Start(t.Context(), "parent_span") |
| 517 |
|
| 518 |
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, ts.URL, http.NoBody) |
| 519 |
req = req.WithContext(httptrace.WithClientTrace(req.Context(), otelhttptrace.NewClientTrace(ctx))) |
| 520 |
|
| 521 |
client := ts.Client() |
| 522 |
res, err := client.Do(req) |
| 523 |
require.NoError(t, err) |
| 524 |
_ = res.Body.Close() |
| 525 |
|
| 526 |
span.End() |
| 527 |
|
| 528 |
parent, ok := getSpanFromRecorder(sr, "parent_span") |
| 529 |
require.True(t, ok) |
| 530 |
|
| 531 |
getconn, ok := getSpanFromRecorder(sr, "http.getconn") |
| 532 |
require.True(t, ok) |
| 533 |
|
| 534 |
require.Equal(t, parent.SpanContext().TraceID(), getconn.SpanContext().TraceID()) |
| 535 |
require.Equal(t, parent.SpanContext().SpanID(), getconn.Parent().SpanID()) |
| 536 |
} |
| 537 |
|
| 538 |
func TestHTTPRequestWithExpect100Continue(t *testing.T) { |
| 539 |
fixture := prepareClientTraceTest(t) |
| 540 |
|
| 541 |
ctx, span := otel.Tracer("oteltest").Start(t.Context(), "root") |
| 542 |
ctx = httptrace.WithClientTrace(ctx, otelhttptrace.NewClientTrace(ctx)) |
| 543 |
|
| 544 |
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fixture.URL, bytes.NewReader([]byte("test"))) |
| 545 |
require.NoError(t, err) |
| 546 |
|
| 547 |
|
| 548 |
req.Header.Set("Expect", "100-continue") |
| 549 |
resp, err := fixture.Client.Do(req) |
| 550 |
require.NoError(t, err) |
| 551 |
require.NoError(t, resp.Body.Close()) |
| 552 |
span.End() |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
var httpSendSpan trace.ReadOnlySpan |
| 558 |
require.Eventually(t, func() bool { |
| 559 |
var ok bool |
| 560 |
httpSendSpan, ok = getSpanFromRecorder(fixture.SpanRecorder, "http.send") |
| 561 |
return ok |
| 562 |
}, 5*time.Second, 10*time.Millisecond) |
| 563 |
|
| 564 |
|
| 565 |
found := false |
| 566 |
for _, v := range httpSendSpan.Events() { |
| 567 |
if v.Name == "GOT 100 - Wait" { |
| 568 |
found = true |
| 569 |
break |
| 570 |
} |
| 571 |
} |
| 572 |
require.True(t, found) |
| 573 |
} |