|
7 | 7 | "math"
|
8 | 8 |
|
9 | 9 | "github.com/prometheus/common/model"
|
| 10 | + "github.com/prometheus/prometheus/model/timestamp" |
10 | 11 | "github.com/prometheus/prometheus/model/value"
|
| 12 | + "github.com/prometheus/prometheus/prompb" |
11 | 13 | writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2"
|
12 | 14 | "go.opentelemetry.io/collector/pdata/pcommon"
|
13 | 15 | "go.opentelemetry.io/collector/pdata/pmetric"
|
@@ -45,3 +47,82 @@ func (c *prometheusConverterV2) addGaugeNumberDataPoints(dataPoints pmetric.Numb
|
45 | 47 | c.addSample(sample, labels)
|
46 | 48 | }
|
47 | 49 | }
|
| 50 | + |
| 51 | +func (c *prometheusConverterV2) addSumNumberDataPoints(dataPoints pmetric.NumberDataPointSlice, |
| 52 | + resource pcommon.Resource, metric pmetric.Metric, settings Settings, name string, |
| 53 | +) { |
| 54 | + for x := 0; x < dataPoints.Len(); x++ { |
| 55 | + pt := dataPoints.At(x) |
| 56 | + lbls := createAttributes( |
| 57 | + resource, |
| 58 | + pt.Attributes(), |
| 59 | + settings.ExternalLabels, |
| 60 | + nil, |
| 61 | + true, |
| 62 | + model.MetricNameLabel, |
| 63 | + name, |
| 64 | + ) |
| 65 | + |
| 66 | + sample := &writev2.Sample{ |
| 67 | + // convert ns to ms |
| 68 | + Timestamp: convertTimeStamp(pt.Timestamp()), |
| 69 | + } |
| 70 | + switch pt.ValueType() { |
| 71 | + case pmetric.NumberDataPointValueTypeInt: |
| 72 | + sample.Value = float64(pt.IntValue()) |
| 73 | + case pmetric.NumberDataPointValueTypeDouble: |
| 74 | + sample.Value = pt.DoubleValue() |
| 75 | + } |
| 76 | + if pt.Flags().NoRecordedValue() { |
| 77 | + sample.Value = math.Float64frombits(value.StaleNaN) |
| 78 | + } |
| 79 | + // TODO: properly add exemplars to the TimeSeries |
| 80 | + c.addSample(sample, lbls) |
| 81 | + |
| 82 | + if settings.ExportCreatedMetric && metric.Sum().IsMonotonic() { |
| 83 | + startTimestamp := pt.StartTimestamp() |
| 84 | + if startTimestamp != 0 { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + createdLabels := make([]prompb.Label, len(lbls)) |
| 89 | + copy(createdLabels, lbls) |
| 90 | + for i, l := range createdLabels { |
| 91 | + if l.Name == model.MetricNameLabel { |
| 92 | + createdLabels[i].Value = name + createdSuffix |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + // TODO: implement this function. |
| 97 | + c.addTimeSeriesIfNeeded(createdLabels, startTimestamp, pt.Timestamp()) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// getPromExemplarsV2 returns a slice of writev2.Exemplar from pdata exemplars. |
| 103 | +func getPromExemplarsV2[T exemplarType](pt T) []writev2.Exemplar { |
| 104 | + promExemplars := make([]writev2.Exemplar, 0, pt.Exemplars().Len()) |
| 105 | + for i := 0; i < pt.Exemplars().Len(); i++ { |
| 106 | + exemplar := pt.Exemplars().At(i) |
| 107 | + |
| 108 | + var promExemplar writev2.Exemplar |
| 109 | + |
| 110 | + switch exemplar.ValueType() { |
| 111 | + case pmetric.ExemplarValueTypeInt: |
| 112 | + promExemplar = writev2.Exemplar{ |
| 113 | + Value: float64(exemplar.IntValue()), |
| 114 | + Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()), |
| 115 | + } |
| 116 | + case pmetric.ExemplarValueTypeDouble: |
| 117 | + promExemplar = writev2.Exemplar{ |
| 118 | + Value: exemplar.DoubleValue(), |
| 119 | + Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()), |
| 120 | + } |
| 121 | + } |
| 122 | + // TODO append labels to promExemplar.Labels |
| 123 | + |
| 124 | + promExemplars = append(promExemplars, promExemplar) |
| 125 | + } |
| 126 | + |
| 127 | + return promExemplars |
| 128 | +} |
0 commit comments