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