mirror of
https://github.com/prometheus-community/json_exporter
synced 2024-12-24 15:33:59 +00:00
Return 503 on connection errors
Signed-off-by: rustyclock <rustyclock@protonmail.com>
This commit is contained in:
parent
4479a45118
commit
a9d3155c71
12
cmd/main.go
12
cmd/main.go
@ -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{})
|
||||
|
@ -15,6 +15,7 @@ package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user