-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathhttprequest.go
56 lines (48 loc) · 1.69 KB
/
httprequest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package splunk // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
import (
"fmt"
"net/http"
"net/http/httputil"
"strconv"
"time"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/multierr"
)
const HeaderRetryAfter = "Retry-After"
// HandleHTTPCode handles an http response and returns the right type of error in case of a failure.
func HandleHTTPCode(resp *http.Response) error {
// Splunk accepts all 2XX codes.
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
return nil
}
err := fmt.Errorf(
"HTTP %d %q",
resp.StatusCode,
http.StatusText(resp.StatusCode))
switch resp.StatusCode {
// Check for responses that may include "Retry-After" header.
case http.StatusTooManyRequests, http.StatusServiceUnavailable:
// Fallback to 0 if the Retry-After header is not present. This will trigger the
// default backoff policy by our caller (retry handler).
retryAfter := 0
if val := resp.Header.Get(HeaderRetryAfter); val != "" {
if seconds, err2 := strconv.Atoi(val); err2 == nil {
retryAfter = seconds
}
}
// Indicate to our caller to pause for the specified number of seconds.
err = exporterhelper.NewThrottleRetry(err, time.Duration(retryAfter)*time.Second)
// Check for permanent errors.
case http.StatusBadRequest, http.StatusUnauthorized:
dump, err2 := httputil.DumpResponse(resp, true)
if err2 == nil {
err = consumererror.NewPermanent(fmt.Errorf("%w", fmt.Errorf("%q", dump)))
} else {
err = multierr.Append(err, err2)
}
}
return err
}