001package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
002
003import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
004import io.opentelemetry.sdk.metrics.data.MetricData;
005import io.opentelemetry.sdk.resources.Resource;
006import io.prometheus.metrics.model.snapshots.CounterSnapshot;
007import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
008import io.prometheus.metrics.model.snapshots.HistogramSnapshot;
009import io.prometheus.metrics.model.snapshots.InfoSnapshot;
010import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
011import io.prometheus.metrics.model.snapshots.SummarySnapshot;
012import io.prometheus.metrics.model.snapshots.UnknownSnapshot;
013import javax.annotation.Nullable;
014
015public class MetricDataFactory {
016
017  private final Resource resource;
018  private final InstrumentationScopeInfo instrumentationScopeInfo;
019  private final long currentTimeMillis;
020
021  public MetricDataFactory(
022      Resource resource,
023      InstrumentationScopeInfo instrumentationScopeInfo,
024      long currentTimeMillis) {
025    this.resource = resource;
026    this.instrumentationScopeInfo = instrumentationScopeInfo;
027    this.currentTimeMillis = currentTimeMillis;
028  }
029
030  @Nullable
031  public MetricData create(CounterSnapshot snapshot) {
032    if (snapshot.getDataPoints().isEmpty()) {
033      return null;
034    }
035    return new PrometheusMetricData<>(
036        snapshot.getMetadata(),
037        new PrometheusCounter(snapshot, currentTimeMillis),
038        instrumentationScopeInfo,
039        resource);
040  }
041
042  @Nullable
043  public MetricData create(GaugeSnapshot snapshot) {
044    if (snapshot.getDataPoints().isEmpty()) {
045      return null;
046    }
047    return new PrometheusMetricData<>(
048        snapshot.getMetadata(),
049        new PrometheusGauge(snapshot, currentTimeMillis),
050        instrumentationScopeInfo,
051        resource);
052  }
053
054  @Nullable
055  public MetricData create(HistogramSnapshot snapshot) {
056    if (!snapshot.getDataPoints().isEmpty()) {
057      HistogramSnapshot.HistogramDataPointSnapshot firstDataPoint = snapshot.getDataPoints().get(0);
058      if (firstDataPoint.hasNativeHistogramData()) {
059        return new PrometheusMetricData<>(
060            snapshot.getMetadata(),
061            new PrometheusNativeHistogram(snapshot, currentTimeMillis),
062            instrumentationScopeInfo,
063            resource);
064      } else if (firstDataPoint.hasClassicHistogramData()) {
065        return new PrometheusMetricData<>(
066            snapshot.getMetadata(),
067            new PrometheusClassicHistogram(snapshot, currentTimeMillis),
068            instrumentationScopeInfo,
069            resource);
070      }
071    }
072    return null;
073  }
074
075  @Nullable
076  public MetricData create(SummarySnapshot snapshot) {
077    if (snapshot.getDataPoints().isEmpty()) {
078      return null;
079    }
080    return new PrometheusMetricData<>(
081        snapshot.getMetadata(),
082        new PrometheusSummary(snapshot, currentTimeMillis),
083        instrumentationScopeInfo,
084        resource);
085  }
086
087  @Nullable
088  public MetricData create(InfoSnapshot snapshot) {
089    if (snapshot.getDataPoints().isEmpty()) {
090      return null;
091    }
092    return new PrometheusMetricData<>(
093        snapshot.getMetadata(),
094        new PrometheusInfo(snapshot, currentTimeMillis),
095        instrumentationScopeInfo,
096        resource);
097  }
098
099  @Nullable
100  public MetricData create(StateSetSnapshot snapshot) {
101    if (snapshot.getDataPoints().isEmpty()) {
102      return null;
103    }
104    return new PrometheusMetricData<>(
105        snapshot.getMetadata(),
106        new PrometheusStateSet(snapshot, currentTimeMillis),
107        instrumentationScopeInfo,
108        resource);
109  }
110
111  @Nullable
112  public MetricData create(UnknownSnapshot snapshot) {
113    if (snapshot.getDataPoints().isEmpty()) {
114      return null;
115    }
116    return new PrometheusMetricData<>(
117        snapshot.getMetadata(),
118        new PrometheusUnknown(snapshot, currentTimeMillis),
119        instrumentationScopeInfo,
120        resource);
121  }
122}