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