mirror of
https://github.com/prometheus-community/json_exporter
synced 2024-12-24 23:42:41 +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
|
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")
|
target := r.URL.Query().Get("target")
|
||||||
if target == "" {
|
if target == "" {
|
||||||
http.Error(w, "Target parameter is missing", http.StatusBadRequest)
|
http.Error(w, "Target parameter is missing", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
registry.MustRegister(probeSuccessGauge)
|
|
||||||
|
|
||||||
data, err := internal.FetchJson(ctx, logger, target, config)
|
data, err := internal.FetchJson(ctx, logger, target, config)
|
||||||
if err != nil {
|
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 {
|
} else {
|
||||||
internal.Scrape(logger, metrics, data)
|
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{})
|
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
|
||||||
|
@ -15,6 +15,7 @@ package internal
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
@ -129,13 +130,17 @@ func FetchJson(ctx context.Context, logger log.Logger, endpoint string, config c
|
|||||||
}
|
}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
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()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return nil, errors.New(resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to read response body;err:<%s>", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user