-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
dpclient.go
268 lines (221 loc) · 6.86 KB
/
dpclient.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package signalfxexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter"
import (
"bytes"
"compress/gzip"
"context"
"io"
"net/http"
"net/url"
"path"
"strings"
"sync"
sfxpb "github.com/signalfx/com_signalfx_metrics_protobuf/model"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/utils"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)
const (
contentEncodingHeader = "Content-Encoding"
contentTypeHeader = "Content-Type"
otlpProtobufContentType = "application/x-protobuf;format=otlp"
)
type sfxClientBase struct {
ingestURL *url.URL
headers map[string]string
client *http.Client
zippers sync.Pool
}
var metricsMarshaler = &pmetric.JSONMarshaler{}
// avoid attempting to compress things that fit into a single ethernet frame
func (s *sfxClientBase) getReader(b []byte) (io.Reader, bool, error) {
var err error
if len(b) > 1500 {
buf := new(bytes.Buffer)
w := s.zippers.Get().(*gzip.Writer)
defer s.zippers.Put(w)
w.Reset(buf)
_, err = w.Write(b)
if err == nil {
err = w.Close()
if err == nil {
return buf, true, nil
}
}
}
return bytes.NewReader(b), false, err
}
// sfxDPClient sends the data to the SignalFx backend.
type sfxDPClient struct {
sfxClientBase
logDataPoints bool
logger *zap.Logger
accessTokenPassthrough bool
converter *translation.MetricsConverter
sendOTLPHistograms bool
}
func (s *sfxDPClient) pushMetricsData(
ctx context.Context,
md pmetric.Metrics,
) (droppedDataPoints int, err error) {
rms := md.ResourceMetrics()
if rms.Len() == 0 {
return 0, nil
}
if s.logDataPoints {
buf, err := metricsMarshaler.MarshalMetrics(md)
if err != nil {
s.logger.Error("Failed to marshal metrics for logging", zap.Error(err))
} else {
s.logger.Debug("received metrics", zap.String("pdata", string(buf)))
}
}
// All metrics in the pmetric.Metrics will have the same access token because of the BatchPerResourceMetrics.
metricToken := s.retrieveAccessToken(rms.At(0))
// export SFx format
sfxDataPoints := s.converter.MetricsToSignalFxV2(md)
if len(sfxDataPoints) > 0 {
droppedCount, err := s.pushMetricsDataForToken(ctx, sfxDataPoints, metricToken)
if err != nil {
return droppedCount, err
}
}
// export any histograms in otlp if sendOTLPHistograms is true
if s.sendOTLPHistograms {
histogramData, metricCount := utils.GetHistograms(md)
if metricCount > 0 {
droppedCount, err := s.pushOTLPMetricsDataForToken(ctx, histogramData, metricToken)
if err != nil {
return droppedCount, err
}
}
}
return 0, nil
}
func (s *sfxDPClient) postData(ctx context.Context, body io.Reader, headers map[string]string) error {
datapointURL := *s.ingestURL
if !strings.HasSuffix(datapointURL.Path, "v2/datapoint") {
datapointURL.Path = path.Join(datapointURL.Path, "v2/datapoint")
}
req, err := http.NewRequestWithContext(ctx, "POST", datapointURL.String(), body)
if err != nil {
return consumererror.NewPermanent(err)
}
// Set the headers configured in sfxDPClient
for k, v := range s.headers {
req.Header.Set(k, v)
}
// Set any extra headers passed by the caller
for k, v := range headers {
req.Header.Set(k, v)
}
// TODO: Mark errors as partial errors wherever applicable when, partial
// error for metrics is available.
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
err = splunk.HandleHTTPCode(resp)
if err != nil {
return err
}
return nil
}
func (s *sfxDPClient) pushMetricsDataForToken(ctx context.Context, sfxDataPoints []*sfxpb.DataPoint, accessToken string) (int, error) {
if s.logDataPoints {
for _, dp := range sfxDataPoints {
s.logger.Debug("Dispatching SFx datapoint", zap.Stringer("dp", dp))
}
}
body, compressed, err := s.encodeBody(sfxDataPoints)
dataPointCount := len(sfxDataPoints)
if err != nil {
return dataPointCount, consumererror.NewPermanent(err)
}
headers := make(map[string]string)
// Override access token in headers map if it's non empty.
if accessToken != "" {
headers[splunk.SFxAccessTokenHeader] = accessToken
}
if compressed {
headers[contentEncodingHeader] = "gzip"
}
err = s.postData(ctx, body, headers)
if err != nil {
return dataPointCount, err
}
return 0, nil
}
func (s *sfxDPClient) encodeBody(dps []*sfxpb.DataPoint) (bodyReader io.Reader, compressed bool, err error) {
msg := sfxpb.DataPointUploadMessage{
Datapoints: dps,
}
body, err := msg.Marshal()
if err != nil {
return nil, false, err
}
return s.getReader(body)
}
func (s *sfxDPClient) retrieveAccessToken(md pmetric.ResourceMetrics) string {
if !s.accessTokenPassthrough {
// Nothing to do if token is pass through not configured or resource is nil.
return ""
}
attrs := md.Resource().Attributes()
if accessToken, ok := attrs.Get(splunk.SFxAccessTokenLabel); ok {
return accessToken.Str()
}
return ""
}
func (s *sfxDPClient) pushOTLPMetricsDataForToken(ctx context.Context, mh pmetric.Metrics, accessToken string) (int, error) {
dataPointCount := mh.DataPointCount()
if s.logDataPoints {
s.logger.Debug("Count of metrics to send in OTLP format",
zap.Int("resource metrics", mh.ResourceMetrics().Len()),
zap.Int("metrics", mh.MetricCount()),
zap.Int("data points", dataPointCount))
buf, err := metricsMarshaler.MarshalMetrics(mh)
if err != nil {
s.logger.Error("Failed to marshal metrics for logging otlp histograms", zap.Error(err))
} else {
s.logger.Debug("Dispatching OTLP metrics", zap.String("pmetrics", string(buf)))
}
}
body, compressed, err := s.encodeOTLPBody(mh)
if err != nil {
return dataPointCount, consumererror.NewPermanent(err)
}
headers := make(map[string]string)
// Set otlp content-type header
headers[contentTypeHeader] = otlpProtobufContentType
// Override access token in headers map if it's non-empty.
if accessToken != "" {
headers[splunk.SFxAccessTokenHeader] = accessToken
}
if compressed {
headers[contentEncodingHeader] = "gzip"
}
s.logger.Debug("Sending metrics in OTLP format")
err = s.postData(ctx, body, headers)
if err != nil {
return dataPointCount, consumererror.NewMetrics(err, mh)
}
return 0, nil
}
func (s *sfxDPClient) encodeOTLPBody(md pmetric.Metrics) (bodyReader io.Reader, compressed bool, err error) {
tr := pmetricotlp.NewExportRequestFromMetrics(md)
body, err := tr.MarshalProto()
if err != nil {
return nil, false, err
}
return s.getReader(body)
}