-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathotlp.go
319 lines (274 loc) · 9.9 KB
/
otlp.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package push
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/storage/remote/otlptranslator/prometheusremotewrite"
"github.com/prometheus/prometheus/util/annotations"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/middleware"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/distributor"
"github.com/cortexproject/cortex/pkg/tenant"
"github.com/cortexproject/cortex/pkg/util"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/cortexproject/cortex/pkg/util/validation"
)
const (
pbContentType = "application/x-protobuf"
jsonContentType = "application/json"
)
// OTLPHandler is a http.Handler which accepts OTLP metrics.
func OTLPHandler(maxRecvMsgSize int, overrides *validation.Overrides, cfg distributor.OTLPConfig, sourceIPs *middleware.SourceIPExtractor, push Func, metrics *distributor.PushHandlerMetrics) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := util_log.WithContext(ctx, util_log.Logger)
if sourceIPs != nil {
source := sourceIPs.Get(r)
if source != "" {
ctx = util.AddSourceIPsToOutgoingContext(ctx, source)
logger = util_log.WithSourceIPs(source, logger)
}
}
userID, err := tenant.TenantID(ctx)
if err != nil {
return
}
req, err := decodeOTLPWriteRequest(ctx, r, maxRecvMsgSize, userID, metrics)
if err != nil {
level.Error(logger).Log("err", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
prwReq := cortexpb.WriteRequest{
Source: cortexpb.API,
Metadata: nil,
SkipLabelNameValidation: false,
}
// otlp to prompb TimeSeries
promTsList, err := convertToPromTS(r.Context(), req.Metrics(), cfg, overrides, userID, logger)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// convert prompb to cortexpb TimeSeries
tsList := []cortexpb.PreallocTimeseries(nil)
for _, v := range promTsList {
tsList = append(tsList, cortexpb.PreallocTimeseries{TimeSeries: &cortexpb.TimeSeries{
Labels: makeLabels(v.Labels),
Samples: makeSamples(v.Samples),
Exemplars: makeExemplars(v.Exemplars),
Histograms: makeHistograms(v.Histograms),
}})
}
prwReq.Timeseries = tsList
if _, err := push(ctx, &prwReq); err != nil {
resp, ok := httpgrpc.HTTPResponseFromError(err)
if !ok {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if resp.GetCode()/100 == 5 {
level.Error(logger).Log("msg", "push error", "err", err)
} else if resp.GetCode() != http.StatusAccepted && resp.GetCode() != http.StatusTooManyRequests {
level.Warn(logger).Log("msg", "push refused", "err", err)
}
http.Error(w, string(resp.Body), int(resp.Code))
}
})
}
func decodeOTLPWriteRequest(ctx context.Context, r *http.Request, maxSize int, userID string, metrics *distributor.PushHandlerMetrics) (pmetricotlp.ExportRequest, error) {
expectedSize := int(r.ContentLength)
if expectedSize > maxSize {
return pmetricotlp.NewExportRequest(), fmt.Errorf("received message larger than max (%d vs %d)", expectedSize, maxSize)
}
contentType := r.Header.Get("Content-Type")
contentEncoding := r.Header.Get("Content-Encoding")
var compressionType util.CompressionType
switch contentEncoding {
case "gzip":
compressionType = util.Gzip
case "":
compressionType = util.NoCompression
default:
return pmetricotlp.NewExportRequest(), fmt.Errorf("unsupported compression: %s, Supported compression types are \"gzip\" or '' (no compression)", contentEncoding)
}
var decoderFunc func(reader io.Reader) (pmetricotlp.ExportRequest, error)
switch contentType {
case pbContentType:
decoderFunc = func(reader io.Reader) (pmetricotlp.ExportRequest, error) {
req := pmetricotlp.NewExportRequest()
otlpReqProto := otlpProtoMessage{req: &req}
bodySize, err := util.ParseProtoReader(ctx, reader, expectedSize, maxSize, otlpReqProto, compressionType)
if err != nil {
return req, err
}
if metrics != nil {
metrics.ObservePushRequestSize(userID, formatOTLP, float64(bodySize))
}
return req, nil
}
case jsonContentType:
decoderFunc = func(reader io.Reader) (pmetricotlp.ExportRequest, error) {
req := pmetricotlp.NewExportRequest()
reader = io.LimitReader(reader, int64(maxSize)+1)
if compressionType == util.Gzip {
var err error
reader, err = gzip.NewReader(reader)
if err != nil {
return req, err
}
}
var buf bytes.Buffer
if expectedSize > 0 {
buf.Grow(expectedSize + bytes.MinRead) // extra space guarantees no reallocation
}
bodySize, err := buf.ReadFrom(reader)
if err != nil {
return req, err
}
if metrics != nil {
metrics.ObservePushRequestSize(userID, formatOTLP, float64(bodySize))
}
return req, req.UnmarshalJSON(buf.Bytes())
}
default:
return pmetricotlp.NewExportRequest(), fmt.Errorf("unsupported content type: %s, supported: [%s, %s]", contentType, jsonContentType, pbContentType)
}
return decoderFunc(r.Body)
}
func convertToPromTS(ctx context.Context, pmetrics pmetric.Metrics, cfg distributor.OTLPConfig, overrides *validation.Overrides, userID string, logger log.Logger) ([]prompb.TimeSeries, error) {
promConverter := prometheusremotewrite.NewPrometheusConverter()
settings := prometheusremotewrite.Settings{
AddMetricSuffixes: true,
DisableTargetInfo: cfg.DisableTargetInfo,
}
var annots annotations.Annotations
var err error
if cfg.ConvertAllAttributes {
annots, err = promConverter.FromMetrics(ctx, convertToMetricsAttributes(pmetrics), settings)
} else {
settings.PromoteResourceAttributes = overrides.PromoteResourceAttributes(userID)
annots, err = promConverter.FromMetrics(ctx, pmetrics, settings)
}
ws, _ := annots.AsStrings("", 0, 0)
if len(ws) > 0 {
level.Warn(logger).Log("msg", "Warnings translating OTLP metrics to Prometheus write request", "warnings", ws)
}
if err != nil {
level.Error(logger).Log("msg", "Error translating OTLP metrics to Prometheus write request", "err", err)
return nil, err
}
return promConverter.TimeSeries(), nil
}
func makeLabels(in []prompb.Label) []cortexpb.LabelAdapter {
out := make(labels.Labels, 0, len(in))
for _, l := range in {
out = append(out, labels.Label{Name: l.Name, Value: l.Value})
}
return cortexpb.FromLabelsToLabelAdapters(out)
}
func makeSamples(in []prompb.Sample) []cortexpb.Sample {
out := make([]cortexpb.Sample, 0, len(in))
for _, s := range in {
out = append(out, cortexpb.Sample{
Value: s.Value,
TimestampMs: s.Timestamp,
})
}
return out
}
func makeExemplars(in []prompb.Exemplar) []cortexpb.Exemplar {
out := make([]cortexpb.Exemplar, 0, len(in))
for _, e := range in {
out = append(out, cortexpb.Exemplar{
Labels: makeLabels(e.Labels),
Value: e.Value,
TimestampMs: e.Timestamp,
})
}
return out
}
func makeHistograms(in []prompb.Histogram) []cortexpb.Histogram {
out := make([]cortexpb.Histogram, 0, len(in))
for _, h := range in {
out = append(out, cortexpb.HistogramPromProtoToHistogramProto(h))
}
return out
}
func convertToMetricsAttributes(md pmetric.Metrics) pmetric.Metrics {
cloneMd := pmetric.NewMetrics()
md.CopyTo(cloneMd)
rms := cloneMd.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
resource := rms.At(i).Resource()
ilms := rms.At(i).ScopeMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
metricSlice := ilm.Metrics()
for k := 0; k < metricSlice.Len(); k++ {
addAttributesToMetric(metricSlice.At(k), resource.Attributes())
}
}
}
return cloneMd
}
// addAttributesToMetric adds additional labels to the given metric
func addAttributesToMetric(metric pmetric.Metric, labelMap pcommon.Map) {
switch metric.Type() {
case pmetric.MetricTypeGauge:
addAttributesToNumberDataPoints(metric.Gauge().DataPoints(), labelMap)
case pmetric.MetricTypeSum:
addAttributesToNumberDataPoints(metric.Sum().DataPoints(), labelMap)
case pmetric.MetricTypeHistogram:
addAttributesToHistogramDataPoints(metric.Histogram().DataPoints(), labelMap)
case pmetric.MetricTypeSummary:
addAttributesToSummaryDataPoints(metric.Summary().DataPoints(), labelMap)
case pmetric.MetricTypeExponentialHistogram:
addAttributesToExponentialHistogramDataPoints(metric.ExponentialHistogram().DataPoints(), labelMap)
}
}
func addAttributesToNumberDataPoints(ps pmetric.NumberDataPointSlice, newAttributeMap pcommon.Map) {
for i := 0; i < ps.Len(); i++ {
joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
}
}
func addAttributesToHistogramDataPoints(ps pmetric.HistogramDataPointSlice, newAttributeMap pcommon.Map) {
for i := 0; i < ps.Len(); i++ {
joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
}
}
func addAttributesToSummaryDataPoints(ps pmetric.SummaryDataPointSlice, newAttributeMap pcommon.Map) {
for i := 0; i < ps.Len(); i++ {
joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
}
}
func addAttributesToExponentialHistogramDataPoints(ps pmetric.ExponentialHistogramDataPointSlice, newAttributeMap pcommon.Map) {
for i := 0; i < ps.Len(); i++ {
joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
}
}
func joinAttributeMaps(from, to pcommon.Map) {
from.Range(func(k string, v pcommon.Value) bool {
v.CopyTo(to.PutEmpty(k))
return true
})
}
// otlpProtoMessage Implements proto.Meesage, proto.Unmarshaler
type otlpProtoMessage struct {
req *pmetricotlp.ExportRequest
}
func (otlpProtoMessage) ProtoMessage() {}
func (otlpProtoMessage) Reset() {}
func (otlpProtoMessage) String() string { return "" }
func (o otlpProtoMessage) Unmarshal(data []byte) error { return o.req.UnmarshalProto(data) }