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 public MetricData create(CounterSnapshot snapshot) { 031 return new PrometheusMetricData<>( 032 snapshot.getMetadata(), 033 new PrometheusCounter(snapshot, currentTimeMillis), 034 instrumentationScopeInfo, 035 resource); 036 } 037 038 public MetricData create(GaugeSnapshot snapshot) { 039 return new PrometheusMetricData<>( 040 snapshot.getMetadata(), 041 new PrometheusGauge(snapshot, currentTimeMillis), 042 instrumentationScopeInfo, 043 resource); 044 } 045 046 @Nullable 047 public MetricData create(HistogramSnapshot snapshot) { 048 if (!snapshot.getDataPoints().isEmpty()) { 049 HistogramSnapshot.HistogramDataPointSnapshot firstDataPoint = snapshot.getDataPoints().get(0); 050 if (firstDataPoint.hasNativeHistogramData()) { 051 return new PrometheusMetricData<>( 052 snapshot.getMetadata(), 053 new PrometheusNativeHistogram(snapshot, currentTimeMillis), 054 instrumentationScopeInfo, 055 resource); 056 } else if (firstDataPoint.hasClassicHistogramData()) { 057 return new PrometheusMetricData<>( 058 snapshot.getMetadata(), 059 new PrometheusClassicHistogram(snapshot, currentTimeMillis), 060 instrumentationScopeInfo, 061 resource); 062 } 063 } 064 return null; 065 } 066 067 public MetricData create(SummarySnapshot snapshot) { 068 return new PrometheusMetricData<>( 069 snapshot.getMetadata(), 070 new PrometheusSummary(snapshot, currentTimeMillis), 071 instrumentationScopeInfo, 072 resource); 073 } 074 075 public MetricData create(InfoSnapshot snapshot) { 076 return new PrometheusMetricData<>( 077 snapshot.getMetadata(), 078 new PrometheusInfo(snapshot, currentTimeMillis), 079 instrumentationScopeInfo, 080 resource); 081 } 082 083 public MetricData create(StateSetSnapshot snapshot) { 084 return new PrometheusMetricData<>( 085 snapshot.getMetadata(), 086 new PrometheusStateSet(snapshot, currentTimeMillis), 087 instrumentationScopeInfo, 088 resource); 089 } 090 091 public MetricData create(UnknownSnapshot snapshot) { 092 return new PrometheusMetricData<>( 093 snapshot.getMetadata(), 094 new PrometheusUnknown(snapshot, currentTimeMillis), 095 instrumentationScopeInfo, 096 resource); 097 } 098}