Skip to content

Commit 21b379f

Browse files
committed
fix otelhttp tests
1 parent ca41630 commit 21b379f

File tree

3 files changed

+6
-358
lines changed

3 files changed

+6
-358
lines changed

instrumentation/net/http/otelhttp/handler_test.go

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -795,160 +795,6 @@ func TestHandlerWithMetricAttributesFn(t *testing.T) {
795795
}
796796
}
797797

798-
func TestHandlerWithSemConvStabilityOptIn(t *testing.T) {
799-
newSpanAttrs := []attribute.KeyValue{
800-
attribute.String("http.request.method", "GET"),
801-
attribute.String("url.scheme", "http"),
802-
attribute.String("server.address", "localhost"),
803-
attribute.String("network.protocol.version", "1.1"),
804-
attribute.String("url.path", "/"),
805-
attribute.Int("http.response.status_code", 200),
806-
}
807-
newMetricAttrs := attribute.NewSet(
808-
attribute.String("http.request.method", "GET"),
809-
attribute.Int("http.response.status_code", 200),
810-
attribute.String("network.protocol.name", "http"),
811-
attribute.String("network.protocol.version", "1.1"),
812-
attribute.String("server.address", "localhost"),
813-
attribute.String("url.scheme", "http"),
814-
)
815-
newMetrics := []metricdata.Metrics{
816-
{
817-
Name: "http.server.request.body.size",
818-
Description: "Size of HTTP server request bodies.",
819-
Unit: "By",
820-
Data: metricdata.Histogram[int64]{
821-
Temporality: metricdata.CumulativeTemporality,
822-
DataPoints: []metricdata.HistogramDataPoint[int64]{{Attributes: newMetricAttrs}},
823-
},
824-
},
825-
{
826-
Name: "http.server.response.body.size",
827-
Description: "Size of HTTP server response bodies.",
828-
Unit: "By",
829-
Data: metricdata.Histogram[int64]{
830-
Temporality: metricdata.CumulativeTemporality,
831-
DataPoints: []metricdata.HistogramDataPoint[int64]{{Attributes: newMetricAttrs}},
832-
},
833-
},
834-
{
835-
Name: "http.server.request.duration",
836-
Description: "Duration of HTTP server requests.",
837-
Unit: "s",
838-
Data: metricdata.Histogram[float64]{
839-
Temporality: metricdata.CumulativeTemporality,
840-
DataPoints: []metricdata.HistogramDataPoint[float64]{{Attributes: newMetricAttrs}},
841-
},
842-
},
843-
}
844-
oldSpanAttrs := []attribute.KeyValue{
845-
attribute.String("http.method", "GET"),
846-
attribute.String("http.scheme", "http"),
847-
attribute.String("net.host.name", "localhost"),
848-
attribute.String("net.protocol.version", "1.1"),
849-
attribute.String("http.target", "/"),
850-
attribute.Int("http.status_code", 200),
851-
}
852-
oldMetricAttrs := attribute.NewSet(
853-
attribute.String("http.method", "GET"),
854-
attribute.String("http.scheme", "http"),
855-
attribute.Int("http.status_code", 200),
856-
attribute.String("net.host.name", "localhost"),
857-
attribute.String("net.protocol.name", "http"),
858-
attribute.String("net.protocol.version", "1.1"),
859-
)
860-
oldMetrics := []metricdata.Metrics{
861-
{
862-
Name: "http.server.request.size",
863-
Description: "Measures the size of HTTP request messages.",
864-
Unit: "By",
865-
Data: metricdata.Sum[int64]{
866-
Temporality: metricdata.CumulativeTemporality,
867-
IsMonotonic: true,
868-
DataPoints: []metricdata.DataPoint[int64]{{Attributes: oldMetricAttrs}},
869-
},
870-
},
871-
{
872-
Name: "http.server.response.size",
873-
Description: "Measures the size of HTTP response messages.",
874-
Unit: "By",
875-
Data: metricdata.Sum[int64]{
876-
Temporality: metricdata.CumulativeTemporality,
877-
IsMonotonic: true,
878-
DataPoints: []metricdata.DataPoint[int64]{{Attributes: oldMetricAttrs}},
879-
},
880-
},
881-
{
882-
Name: "http.server.duration",
883-
Description: "Measures the duration of inbound HTTP requests.",
884-
Unit: "ms",
885-
Data: metricdata.Histogram[float64]{
886-
Temporality: metricdata.CumulativeTemporality,
887-
DataPoints: []metricdata.HistogramDataPoint[float64]{{Attributes: oldMetricAttrs}},
888-
},
889-
},
890-
}
891-
tests := []struct {
892-
name string
893-
semConvStabilityOptInValue string
894-
wantSpanAttributes []attribute.KeyValue
895-
wantMetrics metricdata.ScopeMetrics
896-
}{
897-
{
898-
name: "without opt-in",
899-
semConvStabilityOptInValue: "",
900-
wantSpanAttributes: newSpanAttrs,
901-
wantMetrics: metricdata.ScopeMetrics{
902-
Scope: instrumentation.Scope{
903-
Name: ScopeName,
904-
Version: Version(),
905-
},
906-
Metrics: newMetrics,
907-
},
908-
},
909-
{
910-
name: "with http/dup opt-in",
911-
semConvStabilityOptInValue: "http/dup",
912-
wantSpanAttributes: append(newSpanAttrs, oldSpanAttrs...),
913-
wantMetrics: metricdata.ScopeMetrics{
914-
Scope: instrumentation.Scope{
915-
Name: ScopeName,
916-
Version: Version(),
917-
},
918-
Metrics: append(newMetrics, oldMetrics...),
919-
},
920-
},
921-
}
922-
for _, tt := range tests {
923-
t.Run(tt.name, func(t *testing.T) {
924-
t.Setenv("OTEL_SEMCONV_STABILITY_OPT_IN", tt.semConvStabilityOptInValue)
925-
metricRecorder := sdkmetric.NewManualReader()
926-
spanRecorder := tracetest.NewSpanRecorder()
927-
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(metricRecorder))
928-
traceProvider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder))
929-
h := NewHandler(
930-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
931-
w.WriteHeader(http.StatusOK)
932-
}),
933-
"test_handler",
934-
WithTracerProvider(traceProvider),
935-
WithMeterProvider(meterProvider),
936-
)
937-
r, err := http.NewRequest(http.MethodGet, "http://localhost/", nil)
938-
require.NoError(t, err)
939-
h.ServeHTTP(httptest.NewRecorder(), r)
940-
spans := spanRecorder.Ended()
941-
require.Len(t, spans, 1)
942-
assert.ElementsMatch(t, spans[0].Attributes(), tt.wantSpanAttributes)
943-
rm := metricdata.ResourceMetrics{}
944-
err = metricRecorder.Collect(context.Background(), &rm)
945-
require.NoError(t, err)
946-
require.Len(t, rm.ScopeMetrics, 1)
947-
metricdatatest.AssertEqual(t, tt.wantMetrics, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue(), metricdatatest.IgnoreExemplars())
948-
})
949-
}
950-
}
951-
952798
func BenchmarkHandlerServeHTTP(b *testing.B) {
953799
tp := sdktrace.NewTracerProvider()
954800
mp := sdkmetric.NewMeterProvider()

instrumentation/net/http/otelhttp/transport.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
147147
RequestSize: bw.BytesRead(),
148148
}
149149

150+
if err == nil {
151+
readRecordFunc := func(n int64) {}
152+
res.Body = newWrappedBody(span, readRecordFunc, res.Body)
153+
}
154+
150155
// Use floating point division here for higher precision (instead of Millisecond method).
151156
elapsedTime := float64(time.Since(requestStartTime)) / float64(time.Millisecond)
152157

instrumentation/net/http/otelhttp/transport_test.go

Lines changed: 1 addition & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -479,89 +479,14 @@ func TestTransportUsesFormatter(t *testing.T) {
479479
require.NoError(t, res.Body.Close())
480480

481481
spans := spanRecorder.Ended()
482+
require.NotEmpty(t, spans)
482483
spanName := spans[0].Name()
483484
expectedName := "HTTP GET"
484485
if spanName != expectedName {
485486
t.Fatalf("unexpected name: got %s, expected %s", spanName, expectedName)
486487
}
487488
}
488489

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-
565490
func TestTransportErrorStatus(t *testing.T) {
566491
// Prepare tracing stuff.
567492
spanRecorder := tracetest.NewSpanRecorder()
@@ -931,134 +856,6 @@ func TestTransportMetrics(t *testing.T) {
931856
)
932857
assertClientScopeMetrics(t, rm.ScopeMetrics[0], attrs)
933858
})
934-
935-
t.Run("make http request with http/dup opt-in and check both new and old metrics", func(t *testing.T) {
936-
t.Setenv("OTEL_SEMCONV_STABILITY_OPT_IN", "http/dup")
937-
reader := sdkmetric.NewManualReader()
938-
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
939-
940-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
941-
w.WriteHeader(http.StatusOK)
942-
_, err := w.Write(responseBody)
943-
assert.NoError(t, err)
944-
}))
945-
defer ts.Close()
946-
947-
tr := NewTransport(
948-
http.DefaultTransport,
949-
WithMeterProvider(meterProvider),
950-
)
951-
c := http.Client{Transport: tr}
952-
r, err := http.NewRequest(http.MethodGet, ts.URL, bytes.NewReader(requestBody))
953-
require.NoError(t, err)
954-
res, err := c.Do(r)
955-
require.NoError(t, err)
956-
_, err = io.ReadAll(res.Body)
957-
require.NoError(t, err)
958-
require.NoError(t, res.Body.Close())
959-
host, portStr, err := net.SplitHostPort(r.Host)
960-
require.NoError(t, err)
961-
port, err := strconv.Atoi(portStr)
962-
require.NoError(t, err)
963-
964-
attrsNew := attribute.NewSet(
965-
attribute.String("http.request.method", "GET"),
966-
attribute.Int("http.response.status_code", 200),
967-
attribute.String("server.address", host),
968-
attribute.Int("server.port", port),
969-
attribute.String("url.scheme", "http"),
970-
attribute.String("network.protocol.name", "http"),
971-
attribute.String("network.protocol.version", "1.1"),
972-
)
973-
attrsOld := attribute.NewSet(
974-
attribute.String("http.method", "GET"),
975-
attribute.Int("http.status_code", 200),
976-
attribute.String("net.peer.name", host),
977-
attribute.Int("net.peer.port", port),
978-
)
979-
980-
rm := metricdata.ResourceMetrics{}
981-
err = reader.Collect(context.Background(), &rm)
982-
require.NoError(t, err)
983-
require.Len(t, rm.ScopeMetrics, 1)
984-
985-
expected := metricdata.ScopeMetrics{
986-
Scope: instrumentation.Scope{
987-
Name: ScopeName,
988-
Version: Version(),
989-
},
990-
Metrics: []metricdata.Metrics{
991-
{
992-
Name: "http.client.request.body.size",
993-
Description: "Size of HTTP client request bodies.",
994-
Unit: "By",
995-
Data: metricdata.Histogram[int64]{
996-
Temporality: metricdata.CumulativeTemporality,
997-
DataPoints: []metricdata.HistogramDataPoint[int64]{
998-
{
999-
Attributes: attrsNew,
1000-
},
1001-
},
1002-
},
1003-
},
1004-
{
1005-
Name: "http.client.request.duration",
1006-
Description: "Duration of HTTP client requests.",
1007-
Unit: "s",
1008-
Data: metricdata.Histogram[float64]{
1009-
Temporality: metricdata.CumulativeTemporality,
1010-
DataPoints: []metricdata.HistogramDataPoint[float64]{
1011-
{
1012-
Attributes: attrsNew,
1013-
},
1014-
},
1015-
},
1016-
},
1017-
{
1018-
Name: "http.client.request.size",
1019-
Description: "Measures the size of HTTP request messages.",
1020-
Unit: "By",
1021-
Data: metricdata.Sum[int64]{
1022-
Temporality: metricdata.CumulativeTemporality,
1023-
IsMonotonic: true,
1024-
DataPoints: []metricdata.DataPoint[int64]{
1025-
{
1026-
Attributes: attrsOld,
1027-
},
1028-
},
1029-
},
1030-
},
1031-
{
1032-
Name: "http.client.response.size",
1033-
Description: "Measures the size of HTTP response messages.",
1034-
Unit: "By",
1035-
Data: metricdata.Sum[int64]{
1036-
Temporality: metricdata.CumulativeTemporality,
1037-
IsMonotonic: true,
1038-
DataPoints: []metricdata.DataPoint[int64]{
1039-
{
1040-
Attributes: attrsOld,
1041-
},
1042-
},
1043-
},
1044-
},
1045-
{
1046-
Name: "http.client.duration",
1047-
Description: "Measures the duration of outbound HTTP requests.",
1048-
Unit: "ms",
1049-
Data: metricdata.Histogram[float64]{
1050-
Temporality: metricdata.CumulativeTemporality,
1051-
DataPoints: []metricdata.HistogramDataPoint[float64]{
1052-
{
1053-
Attributes: attrsOld,
1054-
},
1055-
},
1056-
},
1057-
},
1058-
},
1059-
}
1060-
metricdatatest.AssertEqual(t, expected, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue())
1061-
})
1062859
}
1063860

1064861
func assertClientScopeMetrics(t *testing.T, sm metricdata.ScopeMetrics, attrs attribute.Set) {

0 commit comments

Comments
 (0)