diff --git a/instrumentation/net/http/otelhttp/internal/semconv/env_test.go b/instrumentation/net/http/otelhttp/internal/semconv/env_test.go index 9fda8fe80e4..874966b3e15 100644 --- a/instrumentation/net/http/otelhttp/internal/semconv/env_test.go +++ b/instrumentation/net/http/otelhttp/internal/semconv/env_test.go @@ -125,14 +125,14 @@ func (t *testRecorder[T]) Record(_ context.Context, value T, options ...metric.R } func NewTestHTTPServer() HTTPServer { - return HTTPServer{ - requestBytesCounter: &testRecorder[int64]{}, - responseBytesCounter: &testRecorder[int64]{}, - serverLatencyMeasure: &testRecorder[float64]{}, - requestBodySizeHistogram: &testRecorder[int64]{}, - responseBodySizeHistogram: &testRecorder[int64]{}, - requestDurationHistogram: &testRecorder[float64]{}, - } + httpServer := NewHTTPServer(nil) + httpServer.requestBytesCounter = &testRecorder[int64]{} + httpServer.responseBytesCounter = &testRecorder[int64]{} + httpServer.serverLatencyMeasure = &testRecorder[float64]{} + httpServer.requestBodySizeHistogram = &testRecorder[int64]{} + httpServer.responseBodySizeHistogram = &testRecorder[int64]{} + httpServer.requestDurationHistogram = &testRecorder[float64]{} + return httpServer } func NewTestHTTPClient() HTTPClient { diff --git a/instrumentation/net/http/otelhttp/internal/semconv/httpconv_test.go b/instrumentation/net/http/otelhttp/internal/semconv/httpconv_test.go index 5394251cbc4..a9da5584827 100644 --- a/instrumentation/net/http/otelhttp/internal/semconv/httpconv_test.go +++ b/instrumentation/net/http/otelhttp/internal/semconv/httpconv_test.go @@ -97,46 +97,78 @@ func TestNewTraceResponse(t *testing.T) { } func TestNewRecordMetrics(t *testing.T) { - t.Setenv(OTelSemConvStabilityOptIn, "http/dup") - server := NewTestHTTPServer() - server.duplicate = true - req, err := http.NewRequest("POST", "http://example.com", nil) - assert.NoError(t, err) + tests := []struct { + name string + setEnv bool + expectedFunc func(server HTTPServer, t *testing.T) + }{ + { + name: "set env", + setEnv: true, + expectedFunc: func(server HTTPServer, t *testing.T) { + assert.Equal(t, int64(100), server.requestBodySizeHistogram.(*testRecorder[int64]).value) + assert.Equal(t, int64(200), server.responseBodySizeHistogram.(*testRecorder[int64]).value) + assert.Equal(t, float64(300), server.requestDurationHistogram.(*testRecorder[float64]).value) + + want := []attribute.KeyValue{ + attribute.String("url.scheme", "http"), + attribute.String("http.request.method", "POST"), + attribute.Int64("http.response.status_code", 301), + attribute.String("key", "value"), + attribute.String("server.address", "stuff"), + attribute.String("network.protocol.name", "http"), + attribute.String("network.protocol.version", "1.1"), + } - server.RecordMetrics(context.Background(), ServerMetricData{ - ServerName: "stuff", - ResponseSize: 200, - MetricAttributes: MetricAttributes{ - Req: req, - StatusCode: 301, - AdditionalAttributes: []attribute.KeyValue{ - attribute.String("key", "value"), + assert.ElementsMatch(t, want, server.requestBodySizeHistogram.(*testRecorder[int64]).attributes) + assert.ElementsMatch(t, want, server.responseBodySizeHistogram.(*testRecorder[int64]).attributes) + assert.ElementsMatch(t, want, server.requestDurationHistogram.(*testRecorder[float64]).attributes) }, }, - MetricData: MetricData{ - RequestSize: 100, - ElapsedTime: 300, + { + name: "not set env", + setEnv: false, + expectedFunc: func(server HTTPServer, t *testing.T) { + assert.Equal(t, int64(0), server.requestBodySizeHistogram.(*testRecorder[int64]).value) + assert.Equal(t, int64(0), server.responseBodySizeHistogram.(*testRecorder[int64]).value) + assert.Equal(t, float64(0), server.requestDurationHistogram.(*testRecorder[float64]).value) + + assert.Len(t, server.requestBodySizeHistogram.(*testRecorder[int64]).attributes, 0) + assert.Len(t, server.responseBodySizeHistogram.(*testRecorder[int64]).attributes, 0) + assert.Len(t, server.requestDurationHistogram.(*testRecorder[float64]).attributes, 0) + }, }, - }) + } - assert.Equal(t, int64(100), server.requestBodySizeHistogram.(*testRecorder[int64]).value) - assert.Equal(t, int64(200), server.responseBodySizeHistogram.(*testRecorder[int64]).value) - assert.Equal(t, float64(300), server.requestDurationHistogram.(*testRecorder[float64]).value) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setEnv { + t.Setenv(OTelSemConvStabilityOptIn, "http/dup") + } - want := []attribute.KeyValue{ - attribute.String("http.scheme", "http"), - attribute.String("http.method", "POST"), - attribute.Int64("http.status_code", 301), - attribute.String("key", "value"), - attribute.String("net.host.name", "stuff"), - attribute.String("net.protocol.name", "http"), - attribute.String("net.protocol.version", "1.1"), - } - _ = want + server := NewTestHTTPServer() + req, err := http.NewRequest("POST", "http://example.com", nil) + assert.NoError(t, err) - // assert.ElementsMatch(t, want, server.requestBodySizeHistogram.(*testRecorder[int64]).attributes) - // assert.ElementsMatch(t, want, server.responseBodySizeHistogram.(*testRecorder[int64]).attributes) - // assert.ElementsMatch(t, want, server.requestDurationHistogram.(*testRecorder[float64]).attributes) + server.RecordMetrics(context.Background(), ServerMetricData{ + ServerName: "stuff", + ResponseSize: 200, + MetricAttributes: MetricAttributes{ + Req: req, + StatusCode: 301, + AdditionalAttributes: []attribute.KeyValue{ + attribute.String("key", "value"), + }, + }, + MetricData: MetricData{ + RequestSize: 100, + ElapsedTime: 300, + }, + }) + + tt.expectedFunc(server, t) + }) + } } func TestNewMethod(t *testing.T) {