Skip to content

Commit

Permalink
Return 503 on connection errors
Browse files Browse the repository at this point in the history
Signed-off-by: rustyclock <[email protected]>
  • Loading branch information
rustycl0ck committed Aug 18, 2020
1 parent 4479a45 commit a9d3155
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
12 changes: 1 addition & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,17 @@ func probeHandler(w http.ResponseWriter, r *http.Request, logger log.Logger, con
level.Error(logger).Log("msg", "Failed to create metrics list from config", "err", err) //nolint:errcheck
}

probeSuccessGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_success",
Help: "Displays whether or not the probe was a success",
})

target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "Target parameter is missing", http.StatusBadRequest)
return
}

registry.MustRegister(probeSuccessGauge)

data, err := internal.FetchJson(ctx, logger, target, config)
if err != nil {
level.Error(logger).Log("msg", "Failed to fetch JSON response", "err", err) //nolint:errcheck
http.Error(w, "Failed to fetch JSON response. TARGET: "+target+", ERROR: "+err.Error(), http.StatusServiceUnavailable)
} else {
internal.Scrape(logger, metrics, data)

probeSuccessGauge.Set(1)
//level.Info(logger).Log("msg", "Probe succeeded", "duration_seconds", duration) // Too noisy
}

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
Expand Down
9 changes: 7 additions & 2 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package internal

import (
"context"
"errors"
"fmt"
"io/ioutil"
"math"
Expand Down Expand Up @@ -129,13 +130,17 @@ func FetchJson(ctx context.Context, logger log.Logger, endpoint string, config c
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch json from endpoint;endpoint:<%s>,err:<%s>", endpoint, err)
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body;err:<%s>", err)
return nil, err
}

return data, nil
Expand Down

0 comments on commit a9d3155

Please sign in to comment.