From e98ef1bfdb4cc413a019ebdb64988e17bb331942 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 25 Nov 2024 21:02:46 +0100 Subject: [PATCH 1/2] [chore]: enable usestdlibvars linter (#6001) #### Description [usestdlibvars](https://golangci-lint.run/usage/linters/#usestdlibvars) is a linter that detect the possibility to use variables/constants from the Go standard library. Signed-off-by: Matthieu MOREL Co-authored-by: Tyler Yahn --- .golangci.yml | 1 + propagation/baggage_test.go | 8 +- propagation/trace_context_benchmark_test.go | 6 +- semconv/internal/http_test.go | 180 ++++++++++---------- semconv/internal/v2/http_test.go | 14 +- semconv/internal/v3/http_test.go | 14 +- semconv/internal/v4/http_test.go | 14 +- 7 files changed, 119 insertions(+), 118 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 43ef9aaff19..ce3f40b609c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -31,6 +31,7 @@ linters: - unconvert - unused - unparam + - usestdlibvars issues: # Maximum issues count per one linter. diff --git a/propagation/baggage_test.go b/propagation/baggage_test.go index e6a71540fa2..4559a808fe2 100644 --- a/propagation/baggage_test.go +++ b/propagation/baggage_test.go @@ -117,7 +117,7 @@ func TestExtractValidBaggageFromHTTPReq(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - req, _ := http.NewRequest("GET", "http://example.com", nil) + req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) req.Header.Set("baggage", tt.header) ctx := context.Background() @@ -173,7 +173,7 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - req, _ := http.NewRequest("GET", "http://example.com", nil) + req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) req.Header.Set("baggage", tt.header) expected := tt.has.Baggage(t) @@ -226,7 +226,7 @@ func TestInjectBaggageToHTTPReq(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - req, _ := http.NewRequest("GET", "http://example.com", nil) + req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) ctx := baggage.ContextWithBaggage(context.Background(), tt.mems.Baggage(t)) propagator.Inject(ctx, propagation.HeaderCarrier(req.Header)) @@ -273,7 +273,7 @@ func TestBaggageInjectExtractRoundtrip(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := tt.mems.Baggage(t) - req, _ := http.NewRequest("GET", "http://example.com", nil) + req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) ctx := baggage.ContextWithBaggage(context.Background(), b) propagator.Inject(ctx, propagation.HeaderCarrier(req.Header)) diff --git a/propagation/trace_context_benchmark_test.go b/propagation/trace_context_benchmark_test.go index 7b7ea5788dd..041d2f664ee 100644 --- a/propagation/trace_context_benchmark_test.go +++ b/propagation/trace_context_benchmark_test.go @@ -56,7 +56,7 @@ func BenchmarkExtract(b *testing.B) { func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) { b.Run("Sampled", func(b *testing.B) { - req, _ := http.NewRequest("GET", "http://example.com", nil) + req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01") b.ReportAllocs() @@ -64,14 +64,14 @@ func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) { }) b.Run("BogusVersion", func(b *testing.B) { - req, _ := http.NewRequest("GET", "http://example.com", nil) + req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) req.Header.Set("traceparent", "qw-00000000000000000000000000000000-0000000000000000-01") b.ReportAllocs() fn(b, req) }) b.Run("FutureAdditionalData", func(b *testing.B) { - req, _ := http.NewRequest("GET", "http://example.com", nil) + req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil) req.Header.Set("traceparent", "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-XYZxsf09") b.ReportAllocs() fn(b, req) diff --git a/semconv/internal/http_test.go b/semconv/internal/http_test.go index 98fabffa844..e2b0e09396c 100644 --- a/semconv/internal/http_test.go +++ b/semconv/internal/http_test.go @@ -74,7 +74,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "stripped, tcp", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -90,7 +90,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "stripped, udp", network: "udp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -106,7 +106,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "stripped, ip", network: "ip", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -122,7 +122,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "stripped, unix", network: "unix", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -138,7 +138,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "stripped, other", network: "nih", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -154,7 +154,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote ipv4 and port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -172,7 +172,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote ipv6 and port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "[fe80::0202:b3ff:fe1e:8329]:56", @@ -190,7 +190,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote ipv4-in-v6 and port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "[::ffff:192.168.0.1]:56", @@ -208,7 +208,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote name and port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "example.com:56", @@ -226,7 +226,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote ipv4 only", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4", @@ -243,7 +243,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote ipv6 only", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "fe80::0202:b3ff:fe1e:8329", @@ -260,7 +260,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote ipv4_in_v6 only", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "::ffff:192.168.0.1", // section 2.5.5.2 of RFC4291 @@ -277,7 +277,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote name only", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "example.com", @@ -294,7 +294,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with remote port only", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: ":56", @@ -311,7 +311,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host name only", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -330,7 +330,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ipv4 only", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -349,7 +349,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ipv6 only", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -368,7 +368,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host name and port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -388,7 +388,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ipv4 and port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -408,7 +408,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ipv6 and port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -428,7 +428,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host name and bogus port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -447,7 +447,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ipv4 and bogus port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -466,7 +466,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ipv6 and bogus port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -485,7 +485,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with empty host and port", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -504,7 +504,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ip and port in headers", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -526,7 +526,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ipv4 and port in url", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "http://4.3.2.1:78/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -547,7 +547,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { { name: "with host ipv6 and port in url", network: "tcp", - method: "GET", + method: http.MethodGet, requestURI: "http://4.3.2.1:78/user/123", proto: "HTTP/1.0", remoteAddr: "1.2.3.4:56", @@ -581,7 +581,7 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) { } func TestEndUserAttributesFromHTTPRequest(t *testing.T) { - r := testRequest("GET", "/user/123", "HTTP/1.1", "", "", nil, http.Header{}, withTLS) + r := testRequest(http.MethodGet, "/user/123", "HTTP/1.1", "", "", nil, http.Header{}, withTLS) var expected []attribute.KeyValue got := sc.EndUserAttributesFromHTTPRequest(r) assert.ElementsMatch(t, expected, got) @@ -615,7 +615,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "stripped", serverName: "", route: "", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -626,7 +626,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: noTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "http"), attribute.String("http.flavor", "1.0"), @@ -636,7 +636,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with server name", serverName: "my-server-name", route: "", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -647,7 +647,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: noTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "http"), attribute.String("http.flavor", "1.0"), @@ -658,7 +658,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with tls", serverName: "my-server-name", route: "", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -669,7 +669,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -680,7 +680,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with route", serverName: "my-server-name", route: "/user/:id", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -691,7 +691,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -703,7 +703,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with host", serverName: "my-server-name", route: "/user/:id", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -714,7 +714,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -727,7 +727,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with host fallback", serverName: "my-server-name", route: "/user/:id", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -739,7 +739,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -752,7 +752,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with user agent", serverName: "my-server-name", route: "/user/:id", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -765,7 +765,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -779,7 +779,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with proxy info", serverName: "my-server-name", route: "/user/:id", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -793,7 +793,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -808,7 +808,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with http 1.1", serverName: "my-server-name", route: "/user/:id", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.1", remoteAddr: "", @@ -822,7 +822,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.1"), @@ -837,7 +837,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { name: "with http 2", serverName: "my-server-name", route: "/user/:id", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/2.0", remoteAddr: "", @@ -851,7 +851,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "2"), @@ -864,11 +864,11 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { }, { name: "with content length", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", contentLength: 100, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.target", "/user/123"), attribute.String("http.scheme", "http"), attribute.Int64("http.request_content_length", 100), @@ -1029,7 +1029,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }{ { name: "stripped", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1040,7 +1040,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: noTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "http"), attribute.String("http.flavor", "1.0"), @@ -1048,7 +1048,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, { name: "with tls", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1059,7 +1059,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -1067,7 +1067,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, { name: "with host", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1078,7 +1078,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -1087,7 +1087,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, { name: "with host fallback", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1100,7 +1100,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "https://example.com/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -1109,7 +1109,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, { name: "with user agent", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1122,7 +1122,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), @@ -1132,7 +1132,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, { name: "with http 1.1", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.1", remoteAddr: "", @@ -1145,7 +1145,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.1"), @@ -1155,7 +1155,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, { name: "with http 2", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/2.0", remoteAddr: "", @@ -1168,7 +1168,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "2"), @@ -1178,13 +1178,13 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { }, { name: "with content length", - method: "GET", + method: http.MethodGet, url: &url.URL{ Path: "/user/123", }, contentLength: 100, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "http"), attribute.Int64("http.request_content_length", 100), @@ -1197,7 +1197,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { Path: "/user/123", }, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "http"), }, @@ -1210,7 +1210,7 @@ func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { User: url.UserPassword("foo", "bar"), }, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.url", "/user/123"), attribute.String("http.scheme", "http"), }, @@ -1246,7 +1246,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "stripped", serverName: "", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1257,7 +1257,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: noTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("http.flavor", "1.0"), }, @@ -1265,7 +1265,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with server name", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1276,7 +1276,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: noTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("http.flavor", "1.0"), attribute.String("http.server_name", "my-server-name"), @@ -1285,7 +1285,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with tls", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1296,7 +1296,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), attribute.String("http.server_name", "my-server-name"), @@ -1305,7 +1305,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with route", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1316,7 +1316,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), attribute.String("http.server_name", "my-server-name"), @@ -1325,7 +1325,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with host", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1336,7 +1336,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), attribute.String("http.server_name", "my-server-name"), @@ -1346,7 +1346,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with host fallback", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1358,7 +1358,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), attribute.String("http.server_name", "my-server-name"), @@ -1368,7 +1368,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with user agent", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1381,7 +1381,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), attribute.String("http.server_name", "my-server-name"), @@ -1391,7 +1391,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with proxy info", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1405,7 +1405,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.0"), attribute.String("http.server_name", "my-server-name"), @@ -1415,7 +1415,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with http 1.1", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.1", remoteAddr: "", @@ -1429,7 +1429,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "1.1"), attribute.String("http.server_name", "my-server-name"), @@ -1439,7 +1439,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { { name: "with http 2", serverName: "my-server-name", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/2.0", remoteAddr: "", @@ -1453,7 +1453,7 @@ func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { }, tls: withTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "https"), attribute.String("http.flavor", "2"), attribute.String("http.server_name", "my-server-name"), @@ -1486,7 +1486,7 @@ func TestHttpBasicAttributesFromHTTPRequest(t *testing.T) { testcases := []testcase{ { name: "stripped", - method: "GET", + method: http.MethodGet, requestURI: "/user/123", proto: "HTTP/1.0", remoteAddr: "", @@ -1497,7 +1497,7 @@ func TestHttpBasicAttributesFromHTTPRequest(t *testing.T) { header: nil, tls: noTLS, expected: []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("http.flavor", "1.0"), attribute.String("http.host", "example.com"), diff --git a/semconv/internal/v2/http_test.go b/semconv/internal/v2/http_test.go index 988b3454e23..598f46d2d14 100644 --- a/semconv/internal/v2/http_test.go +++ b/semconv/internal/v2/http_test.go @@ -65,7 +65,7 @@ func TestHTTPSClientRequest(t *testing.T) { assert.Equal( t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.flavor", "1.0"), attribute.String("http.url", "https://127.0.0.1:443/resource"), attribute.String("net.peer.name", "127.0.0.1"), @@ -100,7 +100,7 @@ func TestHTTPClientRequest(t *testing.T) { assert.Equal( t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.flavor", "1.0"), attribute.String("http.url", "http://127.0.0.1:8080/resource"), attribute.String("net.peer.name", "127.0.0.1"), @@ -118,7 +118,7 @@ func TestHTTPClientRequestRequired(t *testing.T) { var got []attribute.KeyValue assert.NotPanics(t, func() { got = hc.ClientRequest(req) }) want := []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.flavor", ""), attribute.String("http.url", ""), attribute.String("net.peer.name", ""), @@ -156,7 +156,7 @@ func TestHTTPServerRequest(t *testing.T) { assert.ElementsMatch(t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("http.flavor", "1.1"), attribute.String("net.host.name", srvURL.Hostname()), @@ -196,7 +196,7 @@ func TestHTTPServerRequestFailsGracefully(t *testing.T) { var got []attribute.KeyValue assert.NotPanics(t, func() { got = hc.ServerRequest("", req) }) want := []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("http.flavor", ""), attribute.String("net.host.name", ""), @@ -205,8 +205,8 @@ func TestHTTPServerRequestFailsGracefully(t *testing.T) { } func TestMethod(t *testing.T) { - assert.Equal(t, attribute.String("http.method", "POST"), hc.method("POST")) - assert.Equal(t, attribute.String("http.method", "GET"), hc.method("")) + assert.Equal(t, attribute.String("http.method", http.MethodPost), hc.method(http.MethodPost)) + assert.Equal(t, attribute.String("http.method", http.MethodGet), hc.method("")) assert.Equal(t, attribute.String("http.method", "garbage"), hc.method("garbage")) } diff --git a/semconv/internal/v3/http_test.go b/semconv/internal/v3/http_test.go index 71ed97575d9..193ad96f514 100644 --- a/semconv/internal/v3/http_test.go +++ b/semconv/internal/v3/http_test.go @@ -65,7 +65,7 @@ func TestHTTPSClientRequest(t *testing.T) { assert.Equal( t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.flavor", "1.0"), attribute.String("http.url", "https://127.0.0.1:443/resource"), attribute.String("net.peer.name", "127.0.0.1"), @@ -100,7 +100,7 @@ func TestHTTPClientRequest(t *testing.T) { assert.Equal( t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.flavor", "1.0"), attribute.String("http.url", "http://127.0.0.1:8080/resource"), attribute.String("net.peer.name", "127.0.0.1"), @@ -118,7 +118,7 @@ func TestHTTPClientRequestRequired(t *testing.T) { var got []attribute.KeyValue assert.NotPanics(t, func() { got = hc.ClientRequest(req) }) want := []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.flavor", ""), attribute.String("http.url", ""), attribute.String("net.peer.name", ""), @@ -156,7 +156,7 @@ func TestHTTPServerRequest(t *testing.T) { assert.ElementsMatch(t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("http.flavor", "1.1"), attribute.String("net.host.name", srvURL.Hostname()), @@ -196,7 +196,7 @@ func TestHTTPServerRequestFailsGracefully(t *testing.T) { var got []attribute.KeyValue assert.NotPanics(t, func() { got = hc.ServerRequest("", req) }) want := []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("http.flavor", ""), attribute.String("net.host.name", ""), @@ -205,8 +205,8 @@ func TestHTTPServerRequestFailsGracefully(t *testing.T) { } func TestMethod(t *testing.T) { - assert.Equal(t, attribute.String("http.method", "POST"), hc.method("POST")) - assert.Equal(t, attribute.String("http.method", "GET"), hc.method("")) + assert.Equal(t, attribute.String("http.method", http.MethodPost), hc.method(http.MethodPost)) + assert.Equal(t, attribute.String("http.method", http.MethodGet), hc.method("")) assert.Equal(t, attribute.String("http.method", "garbage"), hc.method("garbage")) } diff --git a/semconv/internal/v4/http_test.go b/semconv/internal/v4/http_test.go index 3cee7125cbe..7318efd405c 100644 --- a/semconv/internal/v4/http_test.go +++ b/semconv/internal/v4/http_test.go @@ -66,7 +66,7 @@ func TestHTTPSClientRequest(t *testing.T) { assert.Equal( t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("net.protocol.version", "1.0"), attribute.String("http.url", "https://127.0.0.1:443/resource"), attribute.String("net.peer.name", "127.0.0.1"), @@ -101,7 +101,7 @@ func TestHTTPClientRequest(t *testing.T) { assert.Equal( t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("net.protocol.version", "1.0"), attribute.String("http.url", "http://127.0.0.1:8080/resource"), attribute.String("net.peer.name", "127.0.0.1"), @@ -119,7 +119,7 @@ func TestHTTPClientRequestRequired(t *testing.T) { var got []attribute.KeyValue assert.NotPanics(t, func() { got = hc.ClientRequest(req) }) want := []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("net.protocol.name", ""), attribute.String("http.url", ""), attribute.String("net.peer.name", ""), @@ -157,7 +157,7 @@ func TestHTTPServerRequest(t *testing.T) { assert.ElementsMatch(t, []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("net.protocol.version", "1.1"), attribute.String("net.host.name", srvURL.Hostname()), @@ -197,7 +197,7 @@ func TestHTTPServerRequestFailsGracefully(t *testing.T) { var got []attribute.KeyValue assert.NotPanics(t, func() { got = hc.ServerRequest("", req) }) want := []attribute.KeyValue{ - attribute.String("http.method", "GET"), + attribute.String("http.method", http.MethodGet), attribute.String("http.scheme", "http"), attribute.String("net.protocol.name", ""), attribute.String("net.host.name", ""), @@ -206,8 +206,8 @@ func TestHTTPServerRequestFailsGracefully(t *testing.T) { } func TestMethod(t *testing.T) { - assert.Equal(t, attribute.String("http.method", "POST"), hc.method("POST")) - assert.Equal(t, attribute.String("http.method", "GET"), hc.method("")) + assert.Equal(t, attribute.String("http.method", http.MethodPost), hc.method(http.MethodPost)) + assert.Equal(t, attribute.String("http.method", http.MethodGet), hc.method("")) assert.Equal(t, attribute.String("http.method", "garbage"), hc.method("garbage")) } From 814a41388b9792d7389d9b631418bb28d3e0f2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Tue, 26 Nov 2024 04:20:46 +0800 Subject: [PATCH 2/2] feat(trace): add concurrent-safe Reset method to SpanRecorder (#5994) Add Reset method to reuse it for testing. Just like in InMemoryExporter. --- CHANGELOG.md | 4 ++++ sdk/trace/tracetest/recorder.go | 13 +++++++++++++ sdk/trace/tracetest/recorder_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index db0b9f625eb..e573f61e55e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Added + +- Add `Reset` method to `SpanRecorder` in `go.opentelemetry.io/otel/sdk/trace/tracetest`. (#5994) + ### Changed - The default global API now supports full auto-instrumentation from the `go.opentelemetry.io/auto` package. diff --git a/sdk/trace/tracetest/recorder.go b/sdk/trace/tracetest/recorder.go index 7aababbbf2f..732669a17ad 100644 --- a/sdk/trace/tracetest/recorder.go +++ b/sdk/trace/tracetest/recorder.go @@ -69,6 +69,19 @@ func (sr *SpanRecorder) Started() []sdktrace.ReadWriteSpan { return dst } +// Reset clears the recorded spans. +// +// This method is safe to be called concurrently. +func (sr *SpanRecorder) Reset() { + sr.startedMu.Lock() + sr.endedMu.Lock() + defer sr.startedMu.Unlock() + defer sr.endedMu.Unlock() + + sr.started = nil + sr.ended = nil +} + // Ended returns a copy of all ended spans that have been recorded. // // This method is safe to be called concurrently. diff --git a/sdk/trace/tracetest/recorder_test.go b/sdk/trace/tracetest/recorder_test.go index 5fd2eecd11a..1d7c7723111 100644 --- a/sdk/trace/tracetest/recorder_test.go +++ b/sdk/trace/tracetest/recorder_test.go @@ -112,3 +112,27 @@ func TestStartingConcurrentSafe(t *testing.T) { assert.Len(t, sr.Started(), 2) } + +func TestResetConcurrentSafe(t *testing.T) { + sr := NewSpanRecorder() + ctx := context.Background() + + runConcurrently( + func() { sr.OnStart(ctx, new(rwSpan)) }, + func() { sr.OnStart(ctx, new(rwSpan)) }, + func() { sr.OnEnd(new(roSpan)) }, + func() { sr.OnEnd(new(roSpan)) }, + ) + + assert.Len(t, sr.Started(), 2) + assert.Len(t, sr.Ended(), 2) + + runConcurrently( + func() { sr.Reset() }, + func() { sr.Reset() }, + func() { sr.Reset() }, + ) + + assert.Empty(t, sr.Started()) + assert.Empty(t, sr.Ended()) +}