diff --git a/.golangci.yml b/.golangci.yml index 42410aebf..f350aed6d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,6 +26,7 @@ linters: - testifylint - unconvert - unused + - usestdlibvars issues: max-same-issues: 0 diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 47bf02c10..a62ae4fbf 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -482,7 +482,7 @@ func CheckServerStatus(serverURL *url.URL, checkEndpoint string, roundTripper ht return err } - request, err := http.NewRequest("GET", config.Address, nil) + request, err := http.NewRequest(http.MethodGet, config.Address, nil) if err != nil { return err } diff --git a/discovery/eureka/client.go b/discovery/eureka/client.go index a833415a5..52e8ce7b4 100644 --- a/discovery/eureka/client.go +++ b/discovery/eureka/client.go @@ -81,7 +81,7 @@ const appListPath string = "/apps" func fetchApps(ctx context.Context, server string, client *http.Client) (*Applications, error) { url := fmt.Sprintf("%s%s", server, appListPath) - request, err := http.NewRequest("GET", url, nil) + request, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, err } diff --git a/discovery/hetzner/robot.go b/discovery/hetzner/robot.go index 1d8aa9302..2cf6006f8 100644 --- a/discovery/hetzner/robot.go +++ b/discovery/hetzner/robot.go @@ -70,7 +70,7 @@ func newRobotDiscovery(conf *SDConfig, _ log.Logger) (*robotDiscovery, error) { } func (d *robotDiscovery) refresh(context.Context) ([]*targetgroup.Group, error) { - req, err := http.NewRequest("GET", d.endpoint+"/server", nil) + req, err := http.NewRequest(http.MethodGet, d.endpoint+"/server", nil) if err != nil { return nil, err } diff --git a/discovery/http/http.go b/discovery/http/http.go index 8dd21ec9e..ff76fd762 100644 --- a/discovery/http/http.go +++ b/discovery/http/http.go @@ -150,7 +150,7 @@ func NewDiscovery(conf *SDConfig, logger log.Logger, clientOpts []config.HTTPCli } func (d *Discovery) Refresh(ctx context.Context) ([]*targetgroup.Group, error) { - req, err := http.NewRequest("GET", d.url, nil) + req, err := http.NewRequest(http.MethodGet, d.url, nil) if err != nil { return nil, err } diff --git a/discovery/marathon/marathon.go b/discovery/marathon/marathon.go index ecad108e4..f833af47e 100644 --- a/discovery/marathon/marathon.go +++ b/discovery/marathon/marathon.go @@ -339,7 +339,7 @@ type appListClient func(ctx context.Context, client *http.Client, url string) (* // fetchApps requests a list of applications from a marathon server. func fetchApps(ctx context.Context, client *http.Client, url string) (*appList, error) { - request, err := http.NewRequest("GET", url, nil) + request, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, err } diff --git a/discovery/openstack/mock_test.go b/discovery/openstack/mock_test.go index 4aa871e11..b1267db90 100644 --- a/discovery/openstack/mock_test.go +++ b/discovery/openstack/mock_test.go @@ -239,7 +239,7 @@ const hypervisorListBody = ` // HandleHypervisorListSuccessfully mocks os-hypervisors detail call. func (m *SDMock) HandleHypervisorListSuccessfully() { m.Mux.HandleFunc("/os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) { - testMethod(m.t, r, "GET") + testMethod(m.t, r, http.MethodGet) testHeader(m.t, r, "X-Auth-Token", tokenID) w.Header().Add("Content-Type", "application/json") @@ -536,7 +536,7 @@ const serverListBody = ` // HandleServerListSuccessfully mocks server detail call. func (m *SDMock) HandleServerListSuccessfully() { m.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { - testMethod(m.t, r, "GET") + testMethod(m.t, r, http.MethodGet) testHeader(m.t, r, "X-Auth-Token", tokenID) w.Header().Add("Content-Type", "application/json") @@ -575,7 +575,7 @@ const listOutput = ` // HandleFloatingIPListSuccessfully mocks floating ips call. func (m *SDMock) HandleFloatingIPListSuccessfully() { m.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) { - testMethod(m.t, r, "GET") + testMethod(m.t, r, http.MethodGet) testHeader(m.t, r, "X-Auth-Token", tokenID) w.Header().Add("Content-Type", "application/json") diff --git a/discovery/puppetdb/puppetdb.go b/discovery/puppetdb/puppetdb.go index 3f9ad1f11..8c9ccde0a 100644 --- a/discovery/puppetdb/puppetdb.go +++ b/discovery/puppetdb/puppetdb.go @@ -189,7 +189,7 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) { return nil, err } - req, err := http.NewRequest("POST", d.url, bytes.NewBuffer(bodyBytes)) + req, err := http.NewRequest(http.MethodPost, d.url, bytes.NewBuffer(bodyBytes)) if err != nil { return nil, err } diff --git a/discovery/triton/triton.go b/discovery/triton/triton.go index e56b7951b..675149f2a 100644 --- a/discovery/triton/triton.go +++ b/discovery/triton/triton.go @@ -211,7 +211,7 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) { endpoint = fmt.Sprintf("%s?groups=%s", endpoint, groups) } - req, err := http.NewRequest("GET", endpoint, nil) + req, err := http.NewRequest(http.MethodGet, endpoint, nil) if err != nil { return nil, err } diff --git a/discovery/xds/client.go b/discovery/xds/client.go index 9844c6d7e..027ceb271 100644 --- a/discovery/xds/client.go +++ b/discovery/xds/client.go @@ -179,7 +179,7 @@ func (rc *HTTPResourceClient) Fetch(ctx context.Context) (*v3.DiscoveryResponse, return nil, err } - request, err := http.NewRequest("POST", rc.endpoint, bytes.NewBuffer(reqBody)) + request, err := http.NewRequest(http.MethodPost, rc.endpoint, bytes.NewBuffer(reqBody)) if err != nil { return nil, err } diff --git a/documentation/examples/remote_storage/remote_storage_adapter/influxdb/client_test.go b/documentation/examples/remote_storage/remote_storage_adapter/influxdb/client_test.go index cb56514e4..a738c01dc 100644 --- a/documentation/examples/remote_storage/remote_storage_adapter/influxdb/client_test.go +++ b/documentation/examples/remote_storage/remote_storage_adapter/influxdb/client_test.go @@ -74,7 +74,7 @@ testmetric,test_label=test_label_value2 value=5.1234 123456789123 server := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { - require.Equal(t, "POST", r.Method, "Unexpected method.") + require.Equal(t, http.MethodPost, r.Method, "Unexpected method.") require.Equal(t, "/write", r.URL.Path, "Unexpected path.") b, err := io.ReadAll(r.Body) require.NoError(t, err, "Error reading body.") diff --git a/documentation/examples/remote_storage/remote_storage_adapter/opentsdb/client.go b/documentation/examples/remote_storage/remote_storage_adapter/opentsdb/client.go index 0fa7c5a4b..abb1d0b7d 100644 --- a/documentation/examples/remote_storage/remote_storage_adapter/opentsdb/client.go +++ b/documentation/examples/remote_storage/remote_storage_adapter/opentsdb/client.go @@ -105,7 +105,7 @@ func (c *Client) Write(samples model.Samples) error { ctx, cancel := context.WithTimeout(context.Background(), c.timeout) defer cancel() - req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(buf)) + req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(buf)) if err != nil { return err } diff --git a/model/labels/labels_test.go b/model/labels/labels_test.go index 5ec7764ca..3d6e7659f 100644 --- a/model/labels/labels_test.go +++ b/model/labels/labels_test.go @@ -16,6 +16,7 @@ package labels import ( "encoding/json" "fmt" + "net/http" "strings" "testing" @@ -810,7 +811,7 @@ var benchmarkLabels = []Label{ {"job", "node"}, {"instance", "123.123.1.211:9090"}, {"path", "/api/v1/namespaces//deployments/"}, - {"method", "GET"}, + {"method", http.MethodGet}, {"namespace", "system"}, {"status", "500"}, {"prometheus", "prometheus-core-1"}, diff --git a/notifier/notifier.go b/notifier/notifier.go index d1832402f..53d3c0419 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -590,7 +590,7 @@ func labelsToOpenAPILabelSet(modelLabelSet labels.Labels) models.LabelSet { } func (n *Manager) sendOne(ctx context.Context, c *http.Client, url string, b []byte) error { - req, err := http.NewRequest("POST", url, bytes.NewReader(b)) + req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b)) if err != nil { return err } diff --git a/scrape/scrape.go b/scrape/scrape.go index 734c24813..4bbeab57a 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -726,7 +726,7 @@ var UserAgent = fmt.Sprintf("Prometheus/%s", version.Version) func (s *targetScraper) scrape(ctx context.Context) (*http.Response, error) { if s.req == nil { - req, err := http.NewRequest("GET", s.URL().String(), nil) + req, err := http.NewRequest(http.MethodGet, s.URL().String(), nil) if err != nil { return nil, err } diff --git a/storage/remote/client.go b/storage/remote/client.go index 5ba0f7117..140194ec7 100644 --- a/storage/remote/client.go +++ b/storage/remote/client.go @@ -199,7 +199,7 @@ type RecoverableError struct { // Store sends a batch of samples to the HTTP endpoint, the request is the proto marshalled // and encoded bytes from codec.go. func (c *Client) Store(ctx context.Context, req []byte, attempt int) error { - httpReq, err := http.NewRequest("POST", c.urlString, bytes.NewReader(req)) + httpReq, err := http.NewRequest(http.MethodPost, c.urlString, bytes.NewReader(req)) if err != nil { // Errors from NewRequest are from unparsable URLs, so are not // recoverable. @@ -290,7 +290,7 @@ func (c *Client) Read(ctx context.Context, query *prompb.Query) (*prompb.QueryRe } compressed := snappy.Encode(nil, data) - httpReq, err := http.NewRequest("POST", c.urlString, bytes.NewReader(compressed)) + httpReq, err := http.NewRequest(http.MethodPost, c.urlString, bytes.NewReader(compressed)) if err != nil { return nil, fmt.Errorf("unable to create request: %w", err) } diff --git a/storage/remote/read_handler_test.go b/storage/remote/read_handler_test.go index e83a0cb21..e8e0ecb8d 100644 --- a/storage/remote/read_handler_test.go +++ b/storage/remote/read_handler_test.go @@ -75,7 +75,7 @@ func TestSampledReadEndpoint(t *testing.T) { require.NoError(t, err) compressed := snappy.Encode(nil, data) - request, err := http.NewRequest("POST", "", bytes.NewBuffer(compressed)) + request, err := http.NewRequest(http.MethodPost, "", bytes.NewBuffer(compressed)) require.NoError(t, err) recorder := httptest.NewRecorder() @@ -170,7 +170,7 @@ func BenchmarkStreamReadEndpoint(b *testing.B) { for i := 0; i < b.N; i++ { compressed := snappy.Encode(nil, data) - request, err := http.NewRequest("POST", "", bytes.NewBuffer(compressed)) + request, err := http.NewRequest(http.MethodPost, "", bytes.NewBuffer(compressed)) require.NoError(b, err) recorder := httptest.NewRecorder() @@ -268,7 +268,7 @@ func TestStreamReadEndpoint(t *testing.T) { require.NoError(t, err) compressed := snappy.Encode(nil, data) - request, err := http.NewRequest("POST", "", bytes.NewBuffer(compressed)) + request, err := http.NewRequest(http.MethodPost, "", bytes.NewBuffer(compressed)) require.NoError(t, err) recorder := httptest.NewRecorder() diff --git a/util/httputil/compression_test.go b/util/httputil/compression_test.go index 2db6810bd..e166c7de7 100644 --- a/util/httputil/compression_test.go +++ b/util/httputil/compression_test.go @@ -85,7 +85,7 @@ func TestCompressionHandler_Gzip(t *testing.T) { }, } - req, _ := http.NewRequest("GET", server.URL+"/foo_endpoint", nil) + req, _ := http.NewRequest(http.MethodGet, server.URL+"/foo_endpoint", nil) req.Header.Set(acceptEncodingHeader, gzipEncoding) resp, err := client.Do(req) @@ -120,7 +120,7 @@ func TestCompressionHandler_Deflate(t *testing.T) { }, } - req, _ := http.NewRequest("GET", server.URL+"/foo_endpoint", nil) + req, _ := http.NewRequest(http.MethodGet, server.URL+"/foo_endpoint", nil) req.Header.Set(acceptEncodingHeader, deflateEncoding) resp, err := client.Do(req) diff --git a/util/httputil/cors_test.go b/util/httputil/cors_test.go index cfa240040..657443ece 100644 --- a/util/httputil/cors_test.go +++ b/util/httputil/cors_test.go @@ -41,7 +41,7 @@ func TestCORSHandler(t *testing.T) { dummyOrigin := "https://foo.com" // OPTIONS with legit origin - req, err := http.NewRequest("OPTIONS", server.URL+"/any_path", nil) + req, err := http.NewRequest(http.MethodOptions, server.URL+"/any_path", nil) require.NoError(t, err, "could not create request") req.Header.Set("Origin", dummyOrigin) @@ -53,7 +53,7 @@ func TestCORSHandler(t *testing.T) { require.Equal(t, dummyOrigin, AccessControlAllowOrigin, "expected Access-Control-Allow-Origin header") // OPTIONS with bad origin - req, err = http.NewRequest("OPTIONS", server.URL+"/any_path", nil) + req, err = http.NewRequest(http.MethodOptions, server.URL+"/any_path", nil) require.NoError(t, err, "could not create request") req.Header.Set("Origin", "https://not-foo.com") diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index 4158e544e..c38399381 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -3354,7 +3354,7 @@ func TestParseTimeParam(t *testing.T) { } for _, test := range tests { - req, err := http.NewRequest("GET", "localhost:42/foo?"+test.paramName+"="+test.paramValue, nil) + req, err := http.NewRequest(http.MethodGet, "localhost:42/foo?"+test.paramName+"="+test.paramValue, nil) require.NoError(t, err) result := test.result @@ -3491,7 +3491,7 @@ func TestOptionsMethod(t *testing.T) { s := httptest.NewServer(r) defer s.Close() - req, err := http.NewRequest("OPTIONS", s.URL+"/any_path", nil) + req, err := http.NewRequest(http.MethodOptions, s.URL+"/any_path", nil) require.NoError(t, err, "Error creating OPTIONS request") client := &http.Client{} resp, err := client.Do(req) diff --git a/web/api/v1/errors_test.go b/web/api/v1/errors_test.go index b6ec7d4e1..e76a1a3d3 100644 --- a/web/api/v1/errors_test.go +++ b/web/api/v1/errors_test.go @@ -89,7 +89,7 @@ func TestApiStatusCodes(t *testing.T) { r := createPrometheusAPI(q) rec := httptest.NewRecorder() - req := httptest.NewRequest("GET", "/api/v1/query?query=up", nil) + req := httptest.NewRequest(http.MethodGet, "/api/v1/query?query=up", nil) r.ServeHTTP(rec, req) diff --git a/web/federate_test.go b/web/federate_test.go index 16637f60a..f201210ec 100644 --- a/web/federate_test.go +++ b/web/federate_test.go @@ -224,7 +224,7 @@ func TestFederation(t *testing.T) { for name, scenario := range scenarios { t.Run(name, func(t *testing.T) { h.config.GlobalConfig.ExternalLabels = scenario.externalLabels - req := httptest.NewRequest("GET", "http://example.org/federate?"+scenario.params, nil) + req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?"+scenario.params, nil) res := httptest.NewRecorder() h.federation(res, req) @@ -265,7 +265,7 @@ func TestFederation_NotReady(t *testing.T) { }, } - req := httptest.NewRequest("GET", "http://example.org/federate?"+scenario.params, nil) + req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?"+scenario.params, nil) res := httptest.NewRecorder() h.federation(res, req) @@ -381,7 +381,7 @@ func TestFederationWithNativeHistograms(t *testing.T) { }, } - req := httptest.NewRequest("GET", "http://example.org/federate?match[]=test_metric", nil) + req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?match[]=test_metric", nil) req.Header.Add("Accept", `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited,application/openmetrics-text;version=1.0.0;q=0.8,application/openmetrics-text;version=0.0.1;q=0.75,text/plain;version=0.0.4;q=0.5,*/*;q=0.1`) res := httptest.NewRecorder() diff --git a/web/web_test.go b/web/web_test.go index 62bdb2ae3..e1fa66fa8 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -311,7 +311,7 @@ func TestDebugHandler(t *testing.T) { w := httptest.NewRecorder() - req, err := http.NewRequest("GET", tc.url, nil) + req, err := http.NewRequest(http.MethodGet, tc.url, nil) require.NoError(t, err) @@ -335,7 +335,7 @@ func TestHTTPMetrics(t *testing.T) { t.Helper() w := httptest.NewRecorder() - req, err := http.NewRequest("GET", "/-/ready", nil) + req, err := http.NewRequest(http.MethodGet, "/-/ready", nil) require.NoError(t, err) handler.router.ServeHTTP(w, req)