| 1 |
// Copyright 2024 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
// Provenance-includes-location: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/95e8f8fdc2a9dc87230406c9a3cf02be4fd68bea/pkg/translator/prometheusremotewrite/metrics_to_prw.go |
| 14 |
// Provenance-includes-license: Apache-2.0 |
| 15 |
// Provenance-includes-copyright: Copyright The OpenTelemetry Authors. |
| 16 |
|
| 17 |
package prometheusremotewrite |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"sort" |
| 23 |
|
| 24 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 25 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 26 |
"go.uber.org/multierr" |
| 27 |
|
| 28 |
"github.com/prometheus/prometheus/prompb" |
| 29 |
prometheustranslator "github.com/prometheus/prometheus/storage/remote/otlptranslator/prometheus" |
| 30 |
) |
| 31 |
|
| 32 |
type Settings struct { |
| 33 |
Namespace string |
| 34 |
ExternalLabels map[string]string |
| 35 |
DisableTargetInfo bool |
| 36 |
ExportCreatedMetric bool |
| 37 |
AddMetricSuffixes bool |
| 38 |
SendMetadata bool |
| 39 |
} |
| 40 |
|
| 41 |
// PrometheusConverter converts from OTel write format to Prometheus remote write format. |
| 42 |
type PrometheusConverter struct { |
| 43 |
unique map[uint64]*prompb.TimeSeries |
| 44 |
conflicts map[uint64][]*prompb.TimeSeries |
| 45 |
} |
| 46 |
|
| 47 |
func NewPrometheusConverter() *PrometheusConverter { |
| 48 |
return &PrometheusConverter{ |
| 49 |
unique: map[uint64]*prompb.TimeSeries{}, |
| 50 |
conflicts: map[uint64][]*prompb.TimeSeries{}, |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// FromMetrics converts pmetric.Metrics to Prometheus remote write format. |
| 55 |
func (c *PrometheusConverter) FromMetrics(md pmetric.Metrics, settings Settings) (errs error) { |
| 56 |
resourceMetricsSlice := md.ResourceMetrics() |
| 57 |
for i := 0; i < resourceMetricsSlice.Len(); i++ { |
| 58 |
resourceMetrics := resourceMetricsSlice.At(i) |
| 59 |
resource := resourceMetrics.Resource() |
| 60 |
scopeMetricsSlice := resourceMetrics.ScopeMetrics() |
| 61 |
// keep track of the most recent timestamp in the ResourceMetrics for |
| 62 |
// use with the "target" info metric |
| 63 |
var mostRecentTimestamp pcommon.Timestamp |
| 64 |
for j := 0; j < scopeMetricsSlice.Len(); j++ { |
| 65 |
metricSlice := scopeMetricsSlice.At(j).Metrics() |
| 66 |
|
| 67 |
// TODO: decide if instrumentation library information should be exported as labels |
| 68 |
for k := 0; k < metricSlice.Len(); k++ { |
| 69 |
metric := metricSlice.At(k) |
| 70 |
mostRecentTimestamp = max(mostRecentTimestamp, mostRecentTimestampInMetric(metric)) |
| 71 |
|
| 72 |
if !isValidAggregationTemporality(metric) { |
| 73 |
errs = multierr.Append(errs, fmt.Errorf("invalid temporality and type combination for metric %q", metric.Name())) |
| 74 |
continue |
| 75 |
} |
| 76 |
|
| 77 |
promName := prometheustranslator.BuildCompliantName(metric, settings.Namespace, settings.AddMetricSuffixes) |
| 78 |
|
| 79 |
// handle individual metrics based on type |
| 80 |
//exhaustive:enforce |
| 81 |
switch metric.Type() { |
| 82 |
case pmetric.MetricTypeGauge: |
| 83 |
dataPoints := metric.Gauge().DataPoints() |
| 84 |
if dataPoints.Len() == 0 { |
| 85 |
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) |
| 86 |
break |
| 87 |
} |
| 88 |
c.addGaugeNumberDataPoints(dataPoints, resource, settings, promName) |
| 89 |
case pmetric.MetricTypeSum: |
| 90 |
dataPoints := metric.Sum().DataPoints() |
| 91 |
if dataPoints.Len() == 0 { |
| 92 |
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) |
| 93 |
break |
| 94 |
} |
| 95 |
c.addSumNumberDataPoints(dataPoints, resource, metric, settings, promName) |
| 96 |
case pmetric.MetricTypeHistogram: |
| 97 |
dataPoints := metric.Histogram().DataPoints() |
| 98 |
if dataPoints.Len() == 0 { |
| 99 |
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) |
| 100 |
break |
| 101 |
} |
| 102 |
c.addHistogramDataPoints(dataPoints, resource, settings, promName) |
| 103 |
case pmetric.MetricTypeExponentialHistogram: |
| 104 |
dataPoints := metric.ExponentialHistogram().DataPoints() |
| 105 |
if dataPoints.Len() == 0 { |
| 106 |
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) |
| 107 |
break |
| 108 |
} |
| 109 |
errs = multierr.Append(errs, c.addExponentialHistogramDataPoints( |
| 110 |
dataPoints, |
| 111 |
resource, |
| 112 |
settings, |
| 113 |
promName, |
| 114 |
)) |
| 115 |
case pmetric.MetricTypeSummary: |
| 116 |
dataPoints := metric.Summary().DataPoints() |
| 117 |
if dataPoints.Len() == 0 { |
| 118 |
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) |
| 119 |
break |
| 120 |
} |
| 121 |
c.addSummaryDataPoints(dataPoints, resource, settings, promName) |
| 122 |
default: |
| 123 |
errs = multierr.Append(errs, errors.New("unsupported metric type")) |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
addResourceTargetInfo(resource, settings, mostRecentTimestamp, c) |
| 128 |
} |
| 129 |
|
| 130 |
return |
| 131 |
} |
| 132 |
|
| 133 |
func isSameMetric(ts *prompb.TimeSeries, lbls []prompb.Label) bool { |
| 134 |
if len(ts.Labels) != len(lbls) { |
| 135 |
return false |
| 136 |
} |
| 137 |
for i, l := range ts.Labels { |
| 138 |
if l.Name != ts.Labels[i].Name || l.Value != ts.Labels[i].Value { |
| 139 |
return false |
| 140 |
} |
| 141 |
} |
| 142 |
return true |
| 143 |
} |
| 144 |
|
| 145 |
// addExemplars adds exemplars for the dataPoint. For each exemplar, if it can find a bucket bound corresponding to its value, |
| 146 |
// the exemplar is added to the bucket bound's time series, provided that the time series' has samples. |
| 147 |
func (c *PrometheusConverter) addExemplars(dataPoint pmetric.HistogramDataPoint, bucketBounds []bucketBoundsData) { |
| 148 |
if len(bucketBounds) == 0 { |
| 149 |
return |
| 150 |
} |
| 151 |
|
| 152 |
exemplars := getPromExemplars(dataPoint) |
| 153 |
if len(exemplars) == 0 { |
| 154 |
return |
| 155 |
} |
| 156 |
|
| 157 |
sort.Sort(byBucketBoundsData(bucketBounds)) |
| 158 |
for _, exemplar := range exemplars { |
| 159 |
for _, bound := range bucketBounds { |
| 160 |
if len(bound.ts.Samples) > 0 && exemplar.Value <= bound.bound { |
| 161 |
bound.ts.Exemplars = append(bound.ts.Exemplars, exemplar) |
| 162 |
break |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// addSample finds a TimeSeries that corresponds to lbls, and adds sample to it. |
| 169 |
// If there is no corresponding TimeSeries already, it's created. |
| 170 |
// The corresponding TimeSeries is returned. |
| 171 |
// If either lbls is nil/empty or sample is nil, nothing is done. |
| 172 |
func (c *PrometheusConverter) addSample(sample *prompb.Sample, lbls []prompb.Label) *prompb.TimeSeries { |
| 173 |
if sample == nil || len(lbls) == 0 { |
| 174 |
// This shouldn't happen |
| 175 |
return nil |
| 176 |
} |
| 177 |
|
| 178 |
ts, _ := c.getOrCreateTimeSeries(lbls) |
| 179 |
ts.Samples = append(ts.Samples, *sample) |
| 180 |
return ts |
| 181 |
} |