001package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
002
003import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
004import io.opentelemetry.sdk.metrics.data.DoublePointData;
005import io.opentelemetry.sdk.metrics.data.MetricDataType;
006import io.opentelemetry.sdk.metrics.data.SumData;
007import io.prometheus.metrics.model.snapshots.Labels;
008import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
009import java.util.ArrayList;
010import java.util.Collection;
011import java.util.Collections;
012import java.util.List;
013
014public class PrometheusStateSet extends PrometheusData<DoublePointData>
015    implements SumData<DoublePointData> {
016
017  private final List<DoublePointData> points;
018
019  @SuppressWarnings("this-escape")
020  public PrometheusStateSet(StateSetSnapshot snapshot, long currentTimeMillis) {
021    super(MetricDataType.DOUBLE_SUM);
022    this.points = new ArrayList<>();
023    for (StateSetSnapshot.StateSetDataPointSnapshot dataPoint : snapshot.getDataPoints()) {
024      for (int i = 0; i < dataPoint.size(); i++) {
025        this.points.add(toOtelDataPoint(snapshot, dataPoint, i, currentTimeMillis));
026      }
027    }
028  }
029
030  @Override
031  public boolean isMonotonic() {
032    return false;
033  }
034
035  @Override
036  public AggregationTemporality getAggregationTemporality() {
037    return AggregationTemporality.CUMULATIVE;
038  }
039
040  @Override
041  public Collection<DoublePointData> getPoints() {
042    return points;
043  }
044
045  private DoublePointData toOtelDataPoint(
046      StateSetSnapshot snapshot,
047      StateSetSnapshot.StateSetDataPointSnapshot dataPoint,
048      int i,
049      long currentTimeMillis) {
050    return new DoublePointDataImpl(
051        dataPoint.isTrue(i) ? 1.0 : 0.0,
052        getStartEpochNanos(dataPoint),
053        getEpochNanos(dataPoint, currentTimeMillis),
054        labelsToAttributes(
055            dataPoint
056                .getLabels()
057                .merge(Labels.of(snapshot.getMetadata().getName(), dataPoint.getName(i)))),
058        Collections.emptyList());
059  }
060}