2424import io .opentelemetry .proto .metrics .v1 .Int64DataPoint ;
2525import io .opentelemetry .proto .metrics .v1 .Metric ;
2626import io .opentelemetry .proto .metrics .v1 .MetricDescriptor ;
27+ import io .opentelemetry .proto .metrics .v1 .MetricDescriptor .Temporality ;
2728import io .opentelemetry .proto .metrics .v1 .MetricDescriptor .Type ;
2829import io .opentelemetry .proto .metrics .v1 .ResourceMetrics ;
2930import io .opentelemetry .proto .metrics .v1 .SummaryDataPoint ;
@@ -100,25 +101,26 @@ static Metric toProtoMetric(MetricData metricData) {
100101 if (metricData .getPoints ().isEmpty ()) {
101102 return builder .build ();
102103 }
103-
104104 switch (builder .getMetricDescriptor ().getType ()) {
105- case UNSPECIFIED :
106105 case UNRECOGNIZED :
106+ case INVALID_TYPE :
107107 break ;
108- case GAUGE_INT64 :
109- case COUNTER_INT64 :
110- builder .addAllInt64DataPoints (toInt64DataPoints (metricData .getPoints ()));
108+ case MONOTONIC_INT64 :
109+ case INT64 :
110+ builder .addAllInt64DataPoints (
111+ toInt64DataPoints (metricData .getPoints (), metricData .getDescriptor ()));
111112 break ;
112- case GAUGE_DOUBLE :
113- case COUNTER_DOUBLE :
114- builder .addAllDoubleDataPoints (toDoubleDataPoints (metricData .getPoints ()));
113+ case MONOTONIC_DOUBLE :
114+ case DOUBLE :
115+ builder .addAllDoubleDataPoints (
116+ toDoubleDataPoints (metricData .getPoints (), metricData .getDescriptor ()));
115117 break ;
116- case GAUGE_HISTOGRAM :
117- case CUMULATIVE_HISTOGRAM :
118+ case HISTOGRAM :
118119 // TODO: Add support for histogram.
119120 break ;
120121 case SUMMARY :
121- builder .addAllSummaryDataPoints (toSummaryDataPoints (metricData .getPoints ()));
122+ builder .addAllSummaryDataPoints (
123+ toSummaryDataPoints (metricData .getPoints (), metricData .getDescriptor ()));
122124 break ;
123125 }
124126 return builder .build ();
@@ -130,11 +132,25 @@ static MetricDescriptor toProtoMetricDescriptor(Descriptor descriptor) {
130132 .setDescription (descriptor .getDescription ())
131133 .setUnit (descriptor .getUnit ())
132134 .setType (toProtoMetricDescriptorType (descriptor .getType ()))
133- .addAllLabels ( toProtoLabels (descriptor . getConstantLabels () ))
135+ .setTemporality ( mapToTemporality (descriptor ))
134136 .build ();
135137 }
136138
137- static Collection <Int64DataPoint > toInt64DataPoints (Collection <Point > points ) {
139+ private static Temporality mapToTemporality (Descriptor descriptor ) {
140+ switch (descriptor .getType ()) {
141+ case NON_MONOTONIC_LONG :
142+ case NON_MONOTONIC_DOUBLE :
143+ case MONOTONIC_LONG :
144+ case MONOTONIC_DOUBLE :
145+ return Temporality .CUMULATIVE ;
146+ case SUMMARY :
147+ return Temporality .DELTA ;
148+ }
149+ return Temporality .UNRECOGNIZED ;
150+ }
151+
152+ static Collection <Int64DataPoint > toInt64DataPoints (
153+ Collection <Point > points , Descriptor descriptor ) {
138154 List <Int64DataPoint > result = new ArrayList <>(points .size ());
139155 for (Point point : points ) {
140156 LongPoint longPoint = (LongPoint ) point ;
@@ -144,6 +160,9 @@ static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
144160 .setTimeUnixNano (longPoint .getEpochNanos ())
145161 .setValue (longPoint .getValue ());
146162 // Not calling directly addAllLabels because that generates couple of unnecessary allocations.
163+ if (descriptor .getConstantLabels () != null && !descriptor .getConstantLabels ().isEmpty ()) {
164+ builder .addAllLabels (toProtoLabels (descriptor .getConstantLabels ()));
165+ }
147166 Collection <StringKeyValue > labels = toProtoLabels (longPoint .getLabels ());
148167 if (!labels .isEmpty ()) {
149168 builder .addAllLabels (labels );
@@ -153,7 +172,8 @@ static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
153172 return result ;
154173 }
155174
156- static Collection <DoubleDataPoint > toDoubleDataPoints (Collection <Point > points ) {
175+ static Collection <DoubleDataPoint > toDoubleDataPoints (
176+ Collection <Point > points , Descriptor descriptor ) {
157177 List <DoubleDataPoint > result = new ArrayList <>(points .size ());
158178 for (Point point : points ) {
159179 DoublePoint doublePoint = (DoublePoint ) point ;
@@ -163,6 +183,9 @@ static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points)
163183 .setTimeUnixNano (doublePoint .getEpochNanos ())
164184 .setValue (doublePoint .getValue ());
165185 // Not calling directly addAllLabels because that generates couple of unnecessary allocations.
186+ if (descriptor .getConstantLabels () != null && !descriptor .getConstantLabels ().isEmpty ()) {
187+ builder .addAllLabels (toProtoLabels (descriptor .getConstantLabels ()));
188+ }
166189 Collection <StringKeyValue > labels = toProtoLabels (doublePoint .getLabels ());
167190 if (!labels .isEmpty ()) {
168191 builder .addAllLabels (labels );
@@ -172,7 +195,8 @@ static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points)
172195 return result ;
173196 }
174197
175- static Collection <SummaryDataPoint > toSummaryDataPoints (Collection <Point > points ) {
198+ static Collection <SummaryDataPoint > toSummaryDataPoints (
199+ Collection <Point > points , Descriptor descriptor ) {
176200 List <SummaryDataPoint > result = new ArrayList <>(points .size ());
177201 for (Point point : points ) {
178202 SummaryPoint summaryPoint = (SummaryPoint ) point ;
@@ -184,6 +208,9 @@ static Collection<SummaryDataPoint> toSummaryDataPoints(Collection<Point> points
184208 .setSum (summaryPoint .getSum ());
185209 // Not calling directly addAllLabels because that generates couple of unnecessary allocations
186210 // if empty list.
211+ if (descriptor .getConstantLabels () != null && !descriptor .getConstantLabels ().isEmpty ()) {
212+ builder .addAllLabels (toProtoLabels (descriptor .getConstantLabels ()));
213+ }
187214 Collection <StringKeyValue > labels = toProtoLabels (summaryPoint .getLabels ());
188215 if (!labels .isEmpty ()) {
189216 builder .addAllLabels (labels );
@@ -221,17 +248,17 @@ static List<ValueAtPercentile> toProtoValueAtPercentiles(
221248 static MetricDescriptor .Type toProtoMetricDescriptorType (Descriptor .Type descriptorType ) {
222249 switch (descriptorType ) {
223250 case NON_MONOTONIC_LONG :
224- return Type .GAUGE_INT64 ;
251+ return Type .INT64 ;
225252 case NON_MONOTONIC_DOUBLE :
226- return Type .GAUGE_DOUBLE ;
253+ return Type .DOUBLE ;
227254 case MONOTONIC_LONG :
228- return Type .COUNTER_INT64 ;
255+ return Type .MONOTONIC_INT64 ;
229256 case MONOTONIC_DOUBLE :
230- return Type .COUNTER_DOUBLE ;
257+ return Type .MONOTONIC_DOUBLE ;
231258 case SUMMARY :
232259 return Type .SUMMARY ;
233260 }
234- return Type .UNSPECIFIED ;
261+ return Type .UNRECOGNIZED ;
235262 }
236263
237264 @ SuppressWarnings ("MixedMutabilityReturnType" )
0 commit comments