|
| 1 | +package push |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/go-kit/log/level" |
| 7 | + "github.com/prometheus/prometheus/model/labels" |
| 8 | + "github.com/prometheus/prometheus/prompb" |
| 9 | + "github.com/prometheus/prometheus/storage/remote" |
| 10 | + "github.com/prometheus/prometheus/storage/remote/otlptranslator/prometheusremotewrite" |
| 11 | + "github.com/weaveworks/common/httpgrpc" |
| 12 | + "github.com/weaveworks/common/middleware" |
| 13 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 14 | + "go.opentelemetry.io/collector/pdata/pmetric" |
| 15 | + |
| 16 | + "github.com/cortexproject/cortex/pkg/cortexpb" |
| 17 | + "github.com/cortexproject/cortex/pkg/util" |
| 18 | + "github.com/cortexproject/cortex/pkg/util/log" |
| 19 | +) |
| 20 | + |
| 21 | +// OTLPHandler is a http.Handler which accepts OTLP metrics. |
| 22 | +func OTLPHandler(sourceIPs *middleware.SourceIPExtractor, push Func) http.Handler { |
| 23 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | + ctx := r.Context() |
| 25 | + logger := log.WithContext(ctx, log.Logger) |
| 26 | + if sourceIPs != nil { |
| 27 | + source := sourceIPs.Get(r) |
| 28 | + if source != "" { |
| 29 | + ctx = util.AddSourceIPsToOutgoingContext(ctx, source) |
| 30 | + logger = log.WithSourceIPs(source, logger) |
| 31 | + } |
| 32 | + } |
| 33 | + req, err := remote.DecodeOTLPWriteRequest(r) |
| 34 | + if err != nil { |
| 35 | + level.Error(logger).Log("err", err.Error()) |
| 36 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + tsMap, err := prometheusremotewrite.FromMetrics(convertToMetricsAttributes(req.Metrics()), prometheusremotewrite.Settings{DisableTargetInfo: true}) |
| 41 | + if err != nil { |
| 42 | + level.Error(logger).Log("err", err.Error()) |
| 43 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + prwReq := cortexpb.WriteRequest{ |
| 48 | + Source: cortexpb.API, |
| 49 | + Metadata: nil, |
| 50 | + SkipLabelNameValidation: false, |
| 51 | + } |
| 52 | + |
| 53 | + tsList := []cortexpb.PreallocTimeseries(nil) |
| 54 | + for _, v := range tsMap { |
| 55 | + tsList = append(tsList, cortexpb.PreallocTimeseries{TimeSeries: &cortexpb.TimeSeries{ |
| 56 | + Labels: makeLabels(v.Labels), |
| 57 | + Samples: makeSamples(v.Samples), |
| 58 | + Exemplars: makeExemplars(v.Exemplars), |
| 59 | + }}) |
| 60 | + } |
| 61 | + prwReq.Timeseries = tsList |
| 62 | + |
| 63 | + if _, err := push(ctx, &prwReq); err != nil { |
| 64 | + resp, ok := httpgrpc.HTTPResponseFromError(err) |
| 65 | + if !ok { |
| 66 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 67 | + return |
| 68 | + } |
| 69 | + if resp.GetCode()/100 == 5 { |
| 70 | + level.Error(logger).Log("msg", "push error", "err", err) |
| 71 | + } else if resp.GetCode() != http.StatusAccepted && resp.GetCode() != http.StatusTooManyRequests { |
| 72 | + level.Warn(logger).Log("msg", "push refused", "err", err) |
| 73 | + } |
| 74 | + http.Error(w, string(resp.Body), int(resp.Code)) |
| 75 | + } |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func makeLabels(in []prompb.Label) []cortexpb.LabelAdapter { |
| 80 | + out := make(labels.Labels, 0, len(in)) |
| 81 | + for _, l := range in { |
| 82 | + out = append(out, labels.Label{Name: l.Name, Value: l.Value}) |
| 83 | + } |
| 84 | + return cortexpb.FromLabelsToLabelAdapters(out) |
| 85 | +} |
| 86 | + |
| 87 | +func makeSamples(in []prompb.Sample) []cortexpb.Sample { |
| 88 | + out := make([]cortexpb.Sample, 0, len(in)) |
| 89 | + for _, s := range in { |
| 90 | + out = append(out, cortexpb.Sample{ |
| 91 | + Value: s.Value, |
| 92 | + TimestampMs: s.Timestamp, |
| 93 | + }) |
| 94 | + } |
| 95 | + return out |
| 96 | +} |
| 97 | + |
| 98 | +func makeExemplars(in []prompb.Exemplar) []cortexpb.Exemplar { |
| 99 | + out := make([]cortexpb.Exemplar, 0, len(in)) |
| 100 | + for _, e := range in { |
| 101 | + out = append(out, cortexpb.Exemplar{ |
| 102 | + Labels: makeLabels(e.Labels), |
| 103 | + Value: e.Value, |
| 104 | + TimestampMs: e.Timestamp, |
| 105 | + }) |
| 106 | + } |
| 107 | + return out |
| 108 | +} |
| 109 | + |
| 110 | +func convertToMetricsAttributes(md pmetric.Metrics) pmetric.Metrics { |
| 111 | + cloneMd := pmetric.NewMetrics() |
| 112 | + md.CopyTo(cloneMd) |
| 113 | + rms := cloneMd.ResourceMetrics() |
| 114 | + for i := 0; i < rms.Len(); i++ { |
| 115 | + resource := rms.At(i).Resource() |
| 116 | + |
| 117 | + ilms := rms.At(i).ScopeMetrics() |
| 118 | + for j := 0; j < ilms.Len(); j++ { |
| 119 | + ilm := ilms.At(j) |
| 120 | + metricSlice := ilm.Metrics() |
| 121 | + for k := 0; k < metricSlice.Len(); k++ { |
| 122 | + addAttributesToMetric(metricSlice.At(k), resource.Attributes()) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return cloneMd |
| 127 | +} |
| 128 | + |
| 129 | +// addAttributesToMetric adds additional labels to the given metric |
| 130 | +func addAttributesToMetric(metric pmetric.Metric, labelMap pcommon.Map) { |
| 131 | + switch metric.Type() { |
| 132 | + case pmetric.MetricTypeGauge: |
| 133 | + addAttributesToNumberDataPoints(metric.Gauge().DataPoints(), labelMap) |
| 134 | + case pmetric.MetricTypeSum: |
| 135 | + addAttributesToNumberDataPoints(metric.Sum().DataPoints(), labelMap) |
| 136 | + case pmetric.MetricTypeHistogram: |
| 137 | + addAttributesToHistogramDataPoints(metric.Histogram().DataPoints(), labelMap) |
| 138 | + case pmetric.MetricTypeSummary: |
| 139 | + addAttributesToSummaryDataPoints(metric.Summary().DataPoints(), labelMap) |
| 140 | + case pmetric.MetricTypeExponentialHistogram: |
| 141 | + addAttributesToExponentialHistogramDataPoints(metric.ExponentialHistogram().DataPoints(), labelMap) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func addAttributesToNumberDataPoints(ps pmetric.NumberDataPointSlice, newAttributeMap pcommon.Map) { |
| 146 | + for i := 0; i < ps.Len(); i++ { |
| 147 | + joinAttributeMaps(newAttributeMap, ps.At(i).Attributes()) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func addAttributesToHistogramDataPoints(ps pmetric.HistogramDataPointSlice, newAttributeMap pcommon.Map) { |
| 152 | + for i := 0; i < ps.Len(); i++ { |
| 153 | + joinAttributeMaps(newAttributeMap, ps.At(i).Attributes()) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func addAttributesToSummaryDataPoints(ps pmetric.SummaryDataPointSlice, newAttributeMap pcommon.Map) { |
| 158 | + for i := 0; i < ps.Len(); i++ { |
| 159 | + joinAttributeMaps(newAttributeMap, ps.At(i).Attributes()) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func addAttributesToExponentialHistogramDataPoints(ps pmetric.ExponentialHistogramDataPointSlice, newAttributeMap pcommon.Map) { |
| 164 | + for i := 0; i < ps.Len(); i++ { |
| 165 | + joinAttributeMaps(newAttributeMap, ps.At(i).Attributes()) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func joinAttributeMaps(from, to pcommon.Map) { |
| 170 | + from.Range(func(k string, v pcommon.Value) bool { |
| 171 | + v.CopyTo(to.PutEmpty(k)) |
| 172 | + return true |
| 173 | + }) |
| 174 | +} |
0 commit comments