Skip to content

Commit

Permalink
fixup! fixup! fix: revisit errors package, match the error
Browse files Browse the repository at this point in the history
  • Loading branch information
burningalchemist committed Oct 7, 2023
1 parent 88bdcac commit d890439
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions cmd/sql_exporter/promhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const (
contentLengthHeader string = "Content-Length"
contentEncodingHeader string = "Content-Encoding"
acceptEncodingHeader string = "Accept-Encoding"
scrapeTimeoutHeader string = "X-Prometheus-Scrape-Timeout-Seconds"
)

const (
prometheusHeaderErr = "Failed to parse timeout from Prometheus header"
noMetricsGathered = "No metrics gathered"
noMetricsEncoded = "No metrics encoded"
)

// ExporterHandlerFor returns an http.Handler for the provided Exporter.
Expand All @@ -36,14 +43,14 @@ func ExporterHandlerFor(exporter sql_exporter.Exporter) http.Handler {
mfs, err := gatherer.Gather()
if err != nil {
if len(mfs) == 0 {
klog.Errorf("No metrics gathered: %s", err)
http.Error(w, "No metrics gathered, "+err.Error(), http.StatusInternalServerError)
klog.Errorf("%s: %s", noMetricsGathered, err)
http.Error(w, noMetricsGathered+", "+err.Error(), http.StatusInternalServerError)
return
}
multierr := err.(prometheus.MultiError)
for _, err := range multierr {
if errors.Is(err, context.DeadlineExceeded) {
klog.Errorf("%s. Timeout collecting metrics", err)
klog.Errorf("%s: timeout collecting metrics", err)
} else {
klog.Errorf("Error gathering metrics: %s", err)
}
Expand All @@ -66,8 +73,8 @@ func ExporterHandlerFor(exporter sql_exporter.Exporter) http.Handler {
closer.Close()
}
if errs.MaybeUnwrap() != nil && buf.Len() == 0 {
klog.Errorf("No metrics encoded: %s", errs)
http.Error(w, "No metrics encoded, "+errs.Error(), http.StatusInternalServerError)
klog.Errorf("%s: %s", noMetricsEncoded, errs)
http.Error(w, noMetricsEncoded+", "+errs.Error(), http.StatusInternalServerError)
return
}
header := w.Header()
Expand All @@ -84,19 +91,14 @@ func contextFor(req *http.Request, exporter sql_exporter.Exporter) (context.Cont
timeout := time.Duration(0)
configTimeout := time.Duration(exporter.Config().Globals.ScrapeTimeout)
// If a timeout is provided in the Prometheus header, use it.
if v := req.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
if v := req.Header.Get(scrapeTimeoutHeader); v != "" {
timeoutSeconds, err := strconv.ParseFloat(v, 64)
if err != nil {
parseError := errors.Unwrap(err)
switch {
case errors.Is(parseError, strconv.ErrSyntax):
{
klog.Errorf("Failed to parse timeout from Prometheus header: unsupported value")
}
case errors.Is(parseError, strconv.ErrRange):
{
klog.Errorf("Failed to parse timeout from Prometheus header: value is out of range")
}
case errors.Is(err, strconv.ErrSyntax):
klog.Errorf("%s: unsupported value", prometheusHeaderErr)
case errors.Is(err, strconv.ErrRange):
klog.Errorf("%s: value out of range", prometheusHeaderErr)
}
} else {
timeout = time.Duration(timeoutSeconds * float64(time.Second))
Expand Down

0 comments on commit d890439

Please sign in to comment.