forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httptrace_test.go
165 lines (137 loc) · 4.58 KB
/
httptrace_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelhttptrace_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
)
func TestRoundtrip(t *testing.T) {
tr := noop.NewTracerProvider().Tracer("httptrace/client")
var expectedAttrs map[attribute.Key]string
expectedCorrs := map[string]string{"foo": "bar"}
props := otelhttptrace.WithPropagators(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
// Mock http server
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attrs, corrs, span := otelhttptrace.Extract(r.Context(), r, props)
actualAttrs := make(map[attribute.Key]string)
for _, attr := range attrs {
if attr.Key == semconv.NetSockPeerPortKey {
// Peer port will be non-deterministic
continue
}
actualAttrs[attr.Key] = attr.Value.Emit()
}
if diff := cmp.Diff(actualAttrs, expectedAttrs); diff != "" {
t.Fatalf("[TestRoundtrip] Attributes are different: %v", diff)
}
actualCorrs := make(map[string]string)
for _, corr := range corrs.Members() {
actualCorrs[corr.Key()] = corr.Value()
}
if diff := cmp.Diff(actualCorrs, expectedCorrs); diff != "" {
t.Fatalf("[TestRoundtrip] Correlations are different: %v", diff)
}
if !span.IsValid() {
t.Fatalf("[TestRoundtrip] Invalid span extracted: %v", span)
}
_, err := w.Write([]byte("OK"))
if err != nil {
t.Fatal(err)
}
}),
)
defer ts.Close()
address := ts.Listener.Addr()
hp := strings.Split(address.String(), ":")
expectedAttrs = map[attribute.Key]string{
semconv.NetHostNameKey: hp[0],
semconv.NetHostPortKey: hp[1],
semconv.NetProtocolVersionKey: "1.1",
semconv.HTTPMethodKey: "GET",
semconv.HTTPSchemeKey: "http",
semconv.HTTPTargetKey: "/",
semconv.HTTPRequestContentLengthKey: "3",
semconv.NetSockPeerAddrKey: hp[0],
semconv.NetTransportKey: "ip_tcp",
semconv.UserAgentOriginalKey: "Go-http-client/1.1",
}
client := ts.Client()
ctx := context.Background()
sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{0x01},
SpanID: trace.SpanID{0x01},
})
ctx = trace.ContextWithRemoteSpanContext(ctx, sc)
err := func(ctx context.Context) error {
ctx, span := tr.Start(ctx, "test")
defer span.End()
bag, _ := baggage.Parse("foo=bar")
ctx = baggage.ContextWithBaggage(ctx, bag)
req, _ := http.NewRequest("GET", ts.URL, strings.NewReader("foo"))
otelhttptrace.Inject(ctx, req, props)
res, err := client.Do(req)
if err != nil {
t.Fatalf("Request failed: %s", err.Error())
}
_ = res.Body.Close()
return nil
}(ctx)
if err != nil {
panic("unexpected error in http request: " + err.Error())
}
}
func TestSpecifyPropagators(t *testing.T) {
tr := noop.NewTracerProvider().Tracer("httptrace/client")
expectedCorrs := map[string]string{"foo": "bar"}
// Mock http server
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, corrs, span := otelhttptrace.Extract(r.Context(), r, otelhttptrace.WithPropagators(propagation.Baggage{}))
actualCorrs := make(map[string]string)
for _, corr := range corrs.Members() {
actualCorrs[corr.Key()] = corr.Value()
}
if diff := cmp.Diff(actualCorrs, expectedCorrs); diff != "" {
t.Fatalf("[TestRoundtrip] Correlations are different: %v", diff)
}
if span.IsValid() {
t.Fatalf("[TestRoundtrip] valid span extracted, expected none: %v", span)
}
_, err := w.Write([]byte("OK"))
if err != nil {
t.Fatal(err)
}
}),
)
defer ts.Close()
client := ts.Client()
err := func(ctx context.Context) error {
ctx, span := tr.Start(ctx, "test")
defer span.End()
bag, _ := baggage.Parse("foo=bar")
ctx = baggage.ContextWithBaggage(ctx, bag)
req, _ := http.NewRequest("GET", ts.URL, nil)
otelhttptrace.Inject(ctx, req, otelhttptrace.WithPropagators(propagation.Baggage{}))
res, err := client.Do(req)
if err != nil {
t.Fatalf("Request failed: %s", err.Error())
}
_ = res.Body.Close()
return nil
}(context.Background())
if err != nil {
panic("unexpected error in http request: " + err.Error())
}
}