Skip to content

Commit

Permalink
Merge pull request #745 from prometheus/fix_738
Browse files Browse the repository at this point in the history
Update golangci-lint to 1.36.0
  • Loading branch information
mem authored Mar 16, 2021
2 parents e948256 + cee8181 commit 0617320
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_

GOLANGCI_LINT :=
GOLANGCI_LINT_OPTS ?=
GOLANGCI_LINT_VERSION ?= v1.18.0
GOLANGCI_LINT_VERSION ?= v1.36.0
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64.
# windows isn't included here because of the path separator being different.
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
Expand Down
19 changes: 11 additions & 8 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,16 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
request = request.WithContext(httptrace.WithClientTrace(request.Context(), trace))

resp, err := client.Do(request)
// Err won't be nil if redirects were turned off. See https://github.com/golang/go/issues/3795
if err != nil && resp == nil {
level.Error(logger).Log("msg", "Error for HTTP request", "err", err)
// This is different from the usual err != nil you'd expect here because err won't be nil if redirects were
// turned off. See https://github.com/golang/go/issues/3795
//
// If err == nil there should never be a case where resp is also nil, but better be safe than sorry, so check if
// resp == nil first, and then check if there was an error.
if resp == nil {
resp = &http.Response{}
if err != nil {
level.Error(logger).Log("msg", "Error for HTTP request", "err", err)
}
} else {
requestErrored := (err != nil)

Expand Down Expand Up @@ -459,7 +466,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
}
}

if resp != nil && !requestErrored {
if !requestErrored {
_, err = io.Copy(ioutil.Discard, byteCounter)
if err != nil {
level.Info(logger).Log("msg", "Failed to read HTTP response body", "err", err)
Expand Down Expand Up @@ -504,12 +511,8 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
success = false
}
}

}

if resp == nil {
resp = &http.Response{}
}
tt.mu.Lock()
defer tt.mu.Unlock()
for i, trace := range tt.traces {
Expand Down
63 changes: 63 additions & 0 deletions prober/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -241,6 +242,68 @@ func TestRedirectNotFollowed(t *testing.T) {

}

// TestRedirectionLimit verifies that the probe stops following
// redirects after some limit
func TestRedirectionLimit(t *testing.T) {
const redirectLimit = 11

tooManyRedirects := false

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == fmt.Sprintf("/redirect-%d", redirectLimit+1):
// the client should never hit this path
// because they should stop at the previous one.
w.WriteHeader(http.StatusTooManyRequests)
tooManyRedirects = true
return

case strings.HasPrefix(r.URL.Path, "/redirect-"):
n, err := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/redirect-"))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "failed to extract redirect number from %s", r.URL.Path)
return
}
http.Redirect(w, r, fmt.Sprintf("/redirect-%d", n+1), http.StatusFound)

default:
http.Redirect(w, r, "/redirect-1", http.StatusFound)
}
}))
defer ts.Close()

// Follow redirect, should eventually fail with 302
registry := prometheus.NewRegistry()
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

result := ProbeHTTP(
testCTX,
ts.URL,
config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true}},
registry,
log.NewNopLogger())
if result {
t.Fatalf("Probe suceeded unexpectedly")
}

if tooManyRedirects {
t.Fatalf("Probe followed too many redirects")
}

mfs, err := registry.Gather()
if err != nil {
t.Fatal(err)
}

expectedResults := map[string]float64{
"probe_http_redirects": redirectLimit, // should stop here
"probe_http_status_code": http.StatusFound, // final code should be Found
}
checkRegistryResults(expectedResults, mfs, t)
}

func TestPost(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Expand Down
2 changes: 1 addition & 1 deletion prober/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func checkMetrics(expected map[string]map[string]map[string]struct{}, mfs []*dto
var lv labelValidation
if values != nil {
lv.values = map[string]valueValidation{}
for vname, _ := range values {
for vname := range values {
lv.values[vname] = valueValidation{}
}
}
Expand Down

0 comments on commit 0617320

Please sign in to comment.