3333import io .prometheus .metrics .model .snapshots .ClassicHistogramBuckets ;
3434import io .prometheus .metrics .model .snapshots .CounterSnapshot ;
3535import io .prometheus .metrics .model .snapshots .CounterSnapshot .CounterDataPointSnapshot ;
36+ import io .prometheus .metrics .model .snapshots .DataPointSnapshot ;
3637import io .prometheus .metrics .model .snapshots .Exemplar ;
3738import io .prometheus .metrics .model .snapshots .Exemplars ;
3839import io .prometheus .metrics .model .snapshots .GaugeSnapshot ;
@@ -109,11 +110,11 @@ MetricSnapshots convert(@Nullable Collection<MetricData> metricDataCollection) {
109110 if (metricDataCollection == null || metricDataCollection .isEmpty ()) {
110111 return MetricSnapshots .of ();
111112 }
112- Map <String , MetricSnapshot > snapshotsByName = new HashMap <>(metricDataCollection .size ());
113+ Map <String , MetricSnapshot <?> > snapshotsByName = new HashMap <>(metricDataCollection .size ());
113114 Resource resource = null ;
114115 Set <InstrumentationScopeInfo > scopes = new LinkedHashSet <>();
115116 for (MetricData metricData : metricDataCollection ) {
116- MetricSnapshot snapshot = convert (metricData );
117+ MetricSnapshot <?> snapshot = convert (metricData );
117118 if (snapshot == null ) {
118119 continue ;
119120 }
@@ -135,7 +136,7 @@ MetricSnapshots convert(@Nullable Collection<MetricData> metricDataCollection) {
135136 }
136137
137138 @ Nullable
138- private MetricSnapshot convert (MetricData metricData ) {
139+ private MetricSnapshot <?> convert (MetricData metricData ) {
139140
140141 // Note that AggregationTemporality.DELTA should never happen
141142 // because PrometheusMetricReader#getAggregationTemporality returns CUMULATIVE.
@@ -548,10 +549,10 @@ private static MetricMetadata convertMetadata(MetricData metricData) {
548549 }
549550
550551 private static void putOrMerge (
551- Map <String , MetricSnapshot > snapshotsByName , MetricSnapshot snapshot ) {
552+ Map <String , MetricSnapshot <?>> snapshotsByName , MetricSnapshot <?> snapshot ) {
552553 String name = snapshot .getMetadata ().getPrometheusName ();
553554 if (snapshotsByName .containsKey (name )) {
554- MetricSnapshot merged = merge (snapshotsByName .get (name ), snapshot );
555+ MetricSnapshot <?> merged = merge (snapshotsByName .get (name ), snapshot );
555556 if (merged != null ) {
556557 snapshotsByName .put (name , merged );
557558 }
@@ -567,7 +568,9 @@ private static void putOrMerge(
567568 * type. If the type differs, we log a message and drop one of them.
568569 */
569570 @ Nullable
570- private static MetricSnapshot merge (MetricSnapshot a , MetricSnapshot b ) {
571+ @ SuppressWarnings ("unchecked" )
572+ private static <T extends DataPointSnapshot > MetricSnapshot <T > merge (
573+ MetricSnapshot <?> a , MetricSnapshot <?> b ) {
571574 MetricMetadata metadata = mergeMetadata (a .getMetadata (), b .getMetadata ());
572575 if (metadata == null ) {
573576 return null ;
@@ -577,27 +580,27 @@ private static MetricSnapshot merge(MetricSnapshot a, MetricSnapshot b) {
577580 List <GaugeDataPointSnapshot > dataPoints = new ArrayList <>(numberOfDataPoints );
578581 dataPoints .addAll (((GaugeSnapshot ) a ).getDataPoints ());
579582 dataPoints .addAll (((GaugeSnapshot ) b ).getDataPoints ());
580- return new GaugeSnapshot (metadata , dataPoints );
583+ return ( MetricSnapshot < T >) new GaugeSnapshot (metadata , dataPoints );
581584 } else if (a instanceof CounterSnapshot && b instanceof CounterSnapshot ) {
582585 List <CounterDataPointSnapshot > dataPoints = new ArrayList <>(numberOfDataPoints );
583586 dataPoints .addAll (((CounterSnapshot ) a ).getDataPoints ());
584587 dataPoints .addAll (((CounterSnapshot ) b ).getDataPoints ());
585- return new CounterSnapshot (metadata , dataPoints );
588+ return ( MetricSnapshot < T >) new CounterSnapshot (metadata , dataPoints );
586589 } else if (a instanceof HistogramSnapshot && b instanceof HistogramSnapshot ) {
587590 List <HistogramDataPointSnapshot > dataPoints = new ArrayList <>(numberOfDataPoints );
588591 dataPoints .addAll (((HistogramSnapshot ) a ).getDataPoints ());
589592 dataPoints .addAll (((HistogramSnapshot ) b ).getDataPoints ());
590- return new HistogramSnapshot (metadata , dataPoints );
593+ return ( MetricSnapshot < T >) new HistogramSnapshot (metadata , dataPoints );
591594 } else if (a instanceof SummarySnapshot && b instanceof SummarySnapshot ) {
592595 List <SummaryDataPointSnapshot > dataPoints = new ArrayList <>(numberOfDataPoints );
593596 dataPoints .addAll (((SummarySnapshot ) a ).getDataPoints ());
594597 dataPoints .addAll (((SummarySnapshot ) b ).getDataPoints ());
595- return new SummarySnapshot (metadata , dataPoints );
598+ return ( MetricSnapshot < T >) new SummarySnapshot (metadata , dataPoints );
596599 } else if (a instanceof InfoSnapshot && b instanceof InfoSnapshot ) {
597600 List <InfoDataPointSnapshot > dataPoints = new ArrayList <>(numberOfDataPoints );
598601 dataPoints .addAll (((InfoSnapshot ) a ).getDataPoints ());
599602 dataPoints .addAll (((InfoSnapshot ) b ).getDataPoints ());
600- return new InfoSnapshot (metadata , dataPoints );
603+ return ( MetricSnapshot < T >) new InfoSnapshot (metadata , dataPoints );
601604 } else {
602605 THROTTLING_LOGGER .log (
603606 Level .WARNING ,
@@ -638,7 +641,7 @@ private static MetricMetadata mergeMetadata(MetricMetadata a, MetricMetadata b)
638641 return new MetricMetadata (name , help , unit );
639642 }
640643
641- private static String typeString (MetricSnapshot snapshot ) {
644+ private static String typeString (MetricSnapshot <?> snapshot ) {
642645 // Simple helper for a log message.
643646 return snapshot .getClass ().getSimpleName ().replace ("Snapshot" , "" ).toLowerCase (Locale .ENGLISH );
644647 }
0 commit comments