Skip to content

Commit

Permalink
test(otelhttp): optimize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Dec 13, 2024
1 parent b750320 commit 6a94cce
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 90 deletions.
12 changes: 1 addition & 11 deletions instrumentation/net/http/otelhttp/internal/semconv/httpconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
}
10 changes: 10 additions & 0 deletions instrumentation/net/http/otelhttp/internal/semconv/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions instrumentation/net/http/otelhttp/internal/semconv/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
15 changes: 2 additions & 13 deletions instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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),
)

Expand Down Expand Up @@ -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)
}
66 changes: 0 additions & 66 deletions instrumentation/net/http/otelhttp/internal/semconv/v1.20.0_test.go

This file was deleted.

0 comments on commit 6a94cce

Please sign in to comment.