diff --git a/instrumentation/net/http/otelhttp/internal/semconv/httpconv.go b/instrumentation/net/http/otelhttp/internal/semconv/httpconv.go index 10b597bff1b..3c6c6a9b93b 100644 --- a/instrumentation/net/http/otelhttp/internal/semconv/httpconv.go +++ b/instrumentation/net/http/otelhttp/internal/semconv/httpconv.go @@ -262,7 +262,7 @@ func (n CurrentHTTPServer) MetricAttributes(server string, req *http.Request, st attributes := slices.Grow(additionalAttributes, num) attributes = append(attributes, - standardizeNewHTTPMethodMetric(req.Method), + semconvNew.HTTPRequestMethodKey.String(standardizeHTTPMethod(req.Method)), n.scheme(req.TLS != nil), semconvNew.ServerAddress(host)) @@ -429,13 +429,3 @@ func (n CurrentHTTPClient) method(method string) (attribute.KeyValue, attribute. func isErrorStatusCode(code int) bool { return code >= 400 || code < 100 } - -func standardizeNewHTTPMethodMetric(method string) attribute.KeyValue { - method = strings.ToUpper(method) - switch method { - case http.MethodConnect, http.MethodDelete, http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodPatch, http.MethodPost, http.MethodPut, http.MethodTrace: - default: - method = "_OTHER" - } - return semconvNew.HTTPRequestMethodKey.String(method) -} diff --git a/instrumentation/net/http/otelhttp/internal/semconv/util.go b/instrumentation/net/http/otelhttp/internal/semconv/util.go index 93e8d0f94c1..2ce8f0d9591 100644 --- a/instrumentation/net/http/otelhttp/internal/semconv/util.go +++ b/instrumentation/net/http/otelhttp/internal/semconv/util.go @@ -96,3 +96,13 @@ func handleErr(err error) { otel.Handle(err) } } + +func standardizeHTTPMethod(method string) string { + method = strings.ToUpper(method) + switch method { + case http.MethodConnect, http.MethodDelete, http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodPatch, http.MethodPost, http.MethodPut, http.MethodTrace: + default: + method = "_OTHER" + } + return method +} diff --git a/instrumentation/net/http/otelhttp/internal/semconv/util_test.go b/instrumentation/net/http/otelhttp/internal/semconv/util_test.go index b0fe5439883..ccc3619b5c0 100644 --- a/instrumentation/net/http/otelhttp/internal/semconv/util_test.go +++ b/instrumentation/net/http/otelhttp/internal/semconv/util_test.go @@ -39,3 +39,34 @@ func TestSplitHostPort(t *testing.T) { assert.Equal(t, test.port, p, test.hostport) } } + +func TestStandardizeHTTPMethod(t *testing.T) { + tests := []struct { + method string + want string + }{ + {"GET", "GET"}, + {"get", "GET"}, + {"POST", "POST"}, + {"post", "POST"}, + {"PUT", "PUT"}, + {"put", "PUT"}, + {"DELETE", "DELETE"}, + {"delete", "DELETE"}, + {"HEAD", "HEAD"}, + {"head", "HEAD"}, + {"OPTIONS", "OPTIONS"}, + {"options", "OPTIONS"}, + {"CONNECT", "CONNECT"}, + {"connect", "CONNECT"}, + {"TRACE", "TRACE"}, + {"trace", "TRACE"}, + {"PATCH", "PATCH"}, + {"patch", "PATCH"}, + {"unknown", "_OTHER"}, + {"", "_OTHER"}, + } + for _, test := range tests { + assert.Equal(t, test.want, standardizeHTTPMethod(test.method)) + } +} diff --git a/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go b/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go index c042249dd72..8899cdb725f 100644 --- a/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go +++ b/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "slices" - "strings" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/semconvutil" "go.opentelemetry.io/otel/attribute" @@ -144,7 +143,7 @@ func (o OldHTTPServer) MetricAttributes(server string, req *http.Request, status attributes := slices.Grow(additionalAttributes, n) attributes = append(attributes, - standardizeHTTPMethodMetric(req.Method), + semconv.HTTPMethod(standardizeHTTPMethod(req.Method)), o.scheme(req.TLS != nil), semconv.NetHostName(host)) @@ -214,7 +213,7 @@ func (o OldHTTPClient) MetricAttributes(req *http.Request, statusCode int, addit attributes := slices.Grow(additionalAttributes, n) attributes = append(attributes, - standardizeHTTPMethodMetric(req.Method), + semconv.HTTPMethod(standardizeHTTPMethod(req.Method)), semconv.NetPeerName(requestHost), ) @@ -262,13 +261,3 @@ func (o OldHTTPClient) createMeasures(meter metric.Meter) (metric.Int64Counter, return requestBytesCounter, responseBytesCounter, latencyMeasure } - -func standardizeHTTPMethodMetric(method string) attribute.KeyValue { - method = strings.ToUpper(method) - switch method { - case http.MethodConnect, http.MethodDelete, http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodPatch, http.MethodPost, http.MethodPut, http.MethodTrace: - default: - method = "_OTHER" - } - return semconv.HTTPMethod(method) -} diff --git a/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0_test.go b/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0_test.go deleted file mode 100644 index a3731492729..00000000000 --- a/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0_test.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package semconv - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "go.opentelemetry.io/otel/attribute" -) - -func TestStandardizeHTTPMethodMetric(t *testing.T) { - testCases := []struct { - method string - want attribute.KeyValue - }{ - { - method: "GET", - want: attribute.String("http.method", "GET"), - }, - { - method: "POST", - want: attribute.String("http.method", "POST"), - }, - { - method: "PUT", - want: attribute.String("http.method", "PUT"), - }, - { - method: "DELETE", - want: attribute.String("http.method", "DELETE"), - }, - { - method: "HEAD", - want: attribute.String("http.method", "HEAD"), - }, - { - method: "OPTIONS", - want: attribute.String("http.method", "OPTIONS"), - }, - { - method: "CONNECT", - want: attribute.String("http.method", "CONNECT"), - }, - { - method: "TRACE", - want: attribute.String("http.method", "TRACE"), - }, - { - method: "PATCH", - want: attribute.String("http.method", "PATCH"), - }, - { - method: "test", - want: attribute.String("http.method", "_OTHER"), - }, - } - for _, tt := range testCases { - t.Run(tt.method, func(t *testing.T) { - got := standardizeHTTPMethodMetric(tt.method) - assert.Equal(t, tt.want, got) - }) - } -}