| 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/number_data_points.go |
| 14 |
// Provenance-includes-license: Apache-2.0 |
| 15 |
// Provenance-includes-copyright: Copyright The OpenTelemetry Authors. |
| 16 |
|
| 17 |
package prometheusremotewrite |
| 18 |
|
| 19 |
import ( |
| 20 |
"math" |
| 21 |
|
| 22 |
"github.com/prometheus/common/model" |
| 23 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 24 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 25 |
|
| 26 |
"github.com/prometheus/prometheus/model/value" |
| 27 |
"github.com/prometheus/prometheus/prompb" |
| 28 |
) |
| 29 |
|
| 30 |
func (c *PrometheusConverter) addGaugeNumberDataPoints(dataPoints pmetric.NumberDataPointSlice, |
| 31 |
resource pcommon.Resource, settings Settings, name string) { |
| 32 |
for x := 0; x < dataPoints.Len(); x++ { |
| 33 |
pt := dataPoints.At(x) |
| 34 |
labels := createAttributes( |
| 35 |
resource, |
| 36 |
pt.Attributes(), |
| 37 |
settings.ExternalLabels, |
| 38 |
nil, |
| 39 |
true, |
| 40 |
model.MetricNameLabel, |
| 41 |
name, |
| 42 |
) |
| 43 |
sample := &prompb.Sample{ |
| 44 |
// convert ns to ms |
| 45 |
Timestamp: convertTimeStamp(pt.Timestamp()), |
| 46 |
} |
| 47 |
switch pt.ValueType() { |
| 48 |
case pmetric.NumberDataPointValueTypeInt: |
| 49 |
sample.Value = float64(pt.IntValue()) |
| 50 |
case pmetric.NumberDataPointValueTypeDouble: |
| 51 |
sample.Value = pt.DoubleValue() |
| 52 |
} |
| 53 |
if pt.Flags().NoRecordedValue() { |
| 54 |
sample.Value = math.Float64frombits(value.StaleNaN) |
| 55 |
} |
| 56 |
c.addSample(sample, labels) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func (c *PrometheusConverter) addSumNumberDataPoints(dataPoints pmetric.NumberDataPointSlice, |
| 61 |
resource pcommon.Resource, metric pmetric.Metric, settings Settings, name string) { |
| 62 |
for x := 0; x < dataPoints.Len(); x++ { |
| 63 |
pt := dataPoints.At(x) |
| 64 |
lbls := createAttributes( |
| 65 |
resource, |
| 66 |
pt.Attributes(), |
| 67 |
settings.ExternalLabels, |
| 68 |
nil, |
| 69 |
true, |
| 70 |
model.MetricNameLabel, |
| 71 |
name, |
| 72 |
) |
| 73 |
sample := &prompb.Sample{ |
| 74 |
// convert ns to ms |
| 75 |
Timestamp: convertTimeStamp(pt.Timestamp()), |
| 76 |
} |
| 77 |
switch pt.ValueType() { |
| 78 |
case pmetric.NumberDataPointValueTypeInt: |
| 79 |
sample.Value = float64(pt.IntValue()) |
| 80 |
case pmetric.NumberDataPointValueTypeDouble: |
| 81 |
sample.Value = pt.DoubleValue() |
| 82 |
} |
| 83 |
if pt.Flags().NoRecordedValue() { |
| 84 |
sample.Value = math.Float64frombits(value.StaleNaN) |
| 85 |
} |
| 86 |
ts := c.addSample(sample, lbls) |
| 87 |
if ts != nil { |
| 88 |
exemplars := getPromExemplars[pmetric.NumberDataPoint](pt) |
| 89 |
ts.Exemplars = append(ts.Exemplars, exemplars...) |
| 90 |
} |
| 91 |
|
| 92 |
// add created time series if needed |
| 93 |
if settings.ExportCreatedMetric && metric.Sum().IsMonotonic() { |
| 94 |
startTimestamp := pt.StartTimestamp() |
| 95 |
if startTimestamp == 0 { |
| 96 |
return |
| 97 |
} |
| 98 |
|
| 99 |
createdLabels := make([]prompb.Label, len(lbls)) |
| 100 |
copy(createdLabels, lbls) |
| 101 |
for i, l := range createdLabels { |
| 102 |
if l.Name == model.MetricNameLabel { |
| 103 |
createdLabels[i].Value = name + createdSuffix |
| 104 |
break |
| 105 |
} |
| 106 |
} |
| 107 |
c.addTimeSeriesIfNeeded(createdLabels, startTimestamp, pt.Timestamp()) |
| 108 |
} |
| 109 |
} |
| 110 |
} |