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 private final boolean preserveNames; 021 022 public MetricDataFactory( 023 Resource resource, 024 InstrumentationScopeInfo instrumentationScopeInfo, 025 long currentTimeMillis, 026 boolean preserveNames) { 027 this.resource = resource; 028 this.instrumentationScopeInfo = instrumentationScopeInfo; 029 this.currentTimeMillis = currentTimeMillis; 030 this.preserveNames = preserveNames; 031 } 032 033 @Nullable 034 public MetricData create(CounterSnapshot snapshot) { 035 if (snapshot.getDataPoints().isEmpty()) { 036 return null; 037 } 038 return new PrometheusMetricData<>( 039 snapshot.getMetadata(), 040 new PrometheusCounter(snapshot, currentTimeMillis), 041 instrumentationScopeInfo, 042 resource, 043 preserveNames); 044 } 045 046 @Nullable 047 public MetricData create(GaugeSnapshot snapshot) { 048 if (snapshot.getDataPoints().isEmpty()) { 049 return null; 050 } 051 return new PrometheusMetricData<>( 052 snapshot.getMetadata(), 053 new PrometheusGauge(snapshot, currentTimeMillis), 054 instrumentationScopeInfo, 055 resource, 056 preserveNames); 057 } 058 059 @Nullable 060 public MetricData create(HistogramSnapshot snapshot) { 061 if (!snapshot.getDataPoints().isEmpty()) { 062 HistogramSnapshot.HistogramDataPointSnapshot firstDataPoint = snapshot.getDataPoints().get(0); 063 if (firstDataPoint.hasNativeHistogramData()) { 064 return new PrometheusMetricData<>( 065 snapshot.getMetadata(), 066 new PrometheusNativeHistogram(snapshot, currentTimeMillis), 067 instrumentationScopeInfo, 068 resource, 069 preserveNames); 070 } else if (firstDataPoint.hasClassicHistogramData()) { 071 return new PrometheusMetricData<>( 072 snapshot.getMetadata(), 073 new PrometheusClassicHistogram(snapshot, currentTimeMillis), 074 instrumentationScopeInfo, 075 resource, 076 preserveNames); 077 } 078 } 079 return null; 080 } 081 082 @Nullable 083 public MetricData create(SummarySnapshot snapshot) { 084 if (snapshot.getDataPoints().isEmpty()) { 085 return null; 086 } 087 return new PrometheusMetricData<>( 088 snapshot.getMetadata(), 089 new PrometheusSummary(snapshot, currentTimeMillis), 090 instrumentationScopeInfo, 091 resource, 092 preserveNames); 093 } 094 095 @Nullable 096 public MetricData create(InfoSnapshot snapshot) { 097 if (snapshot.getDataPoints().isEmpty()) { 098 return null; 099 } 100 return new PrometheusMetricData<>( 101 snapshot.getMetadata(), 102 new PrometheusInfo(snapshot, currentTimeMillis), 103 instrumentationScopeInfo, 104 resource, 105 preserveNames); 106 } 107 108 @Nullable 109 public MetricData create(StateSetSnapshot snapshot) { 110 if (snapshot.getDataPoints().isEmpty()) { 111 return null; 112 } 113 return new PrometheusMetricData<>( 114 snapshot.getMetadata(), 115 new PrometheusStateSet(snapshot, currentTimeMillis), 116 instrumentationScopeInfo, 117 resource, 118 preserveNames); 119 } 120 121 @Nullable 122 public MetricData create(UnknownSnapshot snapshot) { 123 if (snapshot.getDataPoints().isEmpty()) { 124 return null; 125 } 126 return new PrometheusMetricData<>( 127 snapshot.getMetadata(), 128 new PrometheusUnknown(snapshot, currentTimeMillis), 129 instrumentationScopeInfo, 130 resource, 131 preserveNames); 132 } 133}