Skip to content

Commit 878b3a7

Browse files
authored
otelhttp: Add client-side tests for OTEL_SEMCONV_STABILITY_OPT_IN (#7335)
1 parent e5688a8 commit 878b3a7

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

instrumentation/net/http/otelhttp/transport_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,82 @@ func TestTransportUsesFormatter(t *testing.T) {
486486
}
487487
}
488488

489+
func TestTransportWithSemConvStabilityOptIn(t *testing.T) {
490+
tests := []struct {
491+
name string
492+
semConvStabilityOptInValue string
493+
expected func(host string, port int) []attribute.KeyValue
494+
}{
495+
{
496+
name: "without opt-in",
497+
semConvStabilityOptInValue: "",
498+
expected: func(host string, port int) []attribute.KeyValue {
499+
return []attribute.KeyValue{
500+
attribute.String("http.request.method", "GET"),
501+
attribute.String("url.full", fmt.Sprintf("http://%s:%d", host, port)),
502+
attribute.String("server.address", host),
503+
attribute.Int("server.port", port),
504+
attribute.String("network.protocol.version", "1.1"),
505+
attribute.Int("http.response.status_code", 200),
506+
}
507+
},
508+
},
509+
{
510+
name: "with http/dup opt-in",
511+
semConvStabilityOptInValue: "http/dup",
512+
expected: func(host string, port int) []attribute.KeyValue {
513+
return []attribute.KeyValue{
514+
// New semantic conventions
515+
attribute.String("http.request.method", "GET"),
516+
attribute.String("url.full", fmt.Sprintf("http://%s:%d", host, port)),
517+
attribute.String("server.address", host),
518+
attribute.Int("server.port", port),
519+
attribute.String("network.protocol.version", "1.1"),
520+
// Old semantic conventions
521+
attribute.String("http.method", "GET"),
522+
attribute.String("http.url", fmt.Sprintf("http://%s:%d", host, port)),
523+
attribute.String("net.peer.name", host),
524+
attribute.Int("net.peer.port", port),
525+
attribute.Int("http.response.status_code", 200),
526+
attribute.Int("http.status_code", 200),
527+
}
528+
},
529+
},
530+
}
531+
for _, tt := range tests {
532+
t.Run(tt.name, func(t *testing.T) {
533+
t.Setenv("OTEL_SEMCONV_STABILITY_OPT_IN", tt.semConvStabilityOptInValue)
534+
spanRecorder := tracetest.NewSpanRecorder()
535+
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
536+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
537+
w.WriteHeader(http.StatusOK)
538+
}))
539+
defer ts.Close()
540+
541+
r, err := http.NewRequest(http.MethodGet, ts.URL, nil)
542+
require.NoError(t, err)
543+
544+
host, portStr, err := net.SplitHostPort(strings.TrimPrefix(ts.URL, "http://"))
545+
require.NoError(t, err)
546+
port, err := strconv.Atoi(portStr)
547+
require.NoError(t, err)
548+
549+
c := http.Client{Transport: NewTransport(
550+
http.DefaultTransport,
551+
WithTracerProvider(provider),
552+
)}
553+
resp, err := c.Do(r)
554+
require.NoError(t, err)
555+
_ = resp.Body.Close()
556+
557+
spans := spanRecorder.Ended()
558+
require.Len(t, spans, 1)
559+
attrs := spans[0].Attributes()
560+
assert.ElementsMatch(t, attrs, tt.expected(host, port))
561+
})
562+
}
563+
}
564+
489565
func TestTransportErrorStatus(t *testing.T) {
490566
// Prepare tracing stuff.
491567
spanRecorder := tracetest.NewSpanRecorder()

0 commit comments

Comments
 (0)