-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathhec_worker.go
81 lines (66 loc) · 2.32 KB
/
hec_worker.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package splunkhecexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter"
import (
"bytes"
"context"
"io"
"net/http"
"net/url"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)
type hecWorker interface {
send(context.Context, buffer, map[string]string) error
}
type defaultHecWorker struct {
url *url.URL
client *http.Client
headers map[string]string
logger *zap.Logger
}
func (hec *defaultHecWorker) send(ctx context.Context, buf buffer, headers map[string]string) error {
// We copy the bytes to a new buffer to avoid corruption. This is a workaround to avoid hitting https://github.com/golang/go/issues/51907.
nb := make([]byte, buf.Len())
copy(nb, buf.Bytes())
bodyBuf := bytes.NewReader(nb)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, hec.url.String(), bodyBuf)
if err != nil {
return consumererror.NewPermanent(err)
}
req.ContentLength = int64(buf.Len())
// Set the headers configured for the client
for k, v := range hec.headers {
req.Header.Set(k, v)
}
// Set extra headers passed by the caller
for k, v := range headers {
req.Header.Set(k, v)
}
if _, ok := buf.(*cancellableGzipWriter); ok {
req.Header.Set("Content-Encoding", "gzip")
}
resp, err := hec.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode == http.StatusServiceUnavailable {
hec.logger.Error("Splunk is unable to receive data. Please investigate the health of the cluster", zap.Int("status", resp.StatusCode), zap.String("host", hec.url.String()))
}
err = splunk.HandleHTTPCode(resp)
if err != nil {
return err
}
// Do not drain the response when 429 or 502 status code is returned.
// HTTP client will not reuse the same connection unless it is drained.
// See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/18281 for more details.
if resp.StatusCode != http.StatusTooManyRequests && resp.StatusCode != http.StatusBadGateway {
if _, errCopy := io.Copy(io.Discard, resp.Body); errCopy != nil {
return errCopy
}
}
return nil
}
var _ hecWorker = &defaultHecWorker{}