24
24
import io .opentelemetry .proto .metrics .v1 .Int64DataPoint ;
25
25
import io .opentelemetry .proto .metrics .v1 .Metric ;
26
26
import io .opentelemetry .proto .metrics .v1 .MetricDescriptor ;
27
+ import io .opentelemetry .proto .metrics .v1 .MetricDescriptor .Temporality ;
27
28
import io .opentelemetry .proto .metrics .v1 .MetricDescriptor .Type ;
28
29
import io .opentelemetry .proto .metrics .v1 .ResourceMetrics ;
29
30
import io .opentelemetry .proto .metrics .v1 .SummaryDataPoint ;
@@ -100,25 +101,26 @@ static Metric toProtoMetric(MetricData metricData) {
100
101
if (metricData .getPoints ().isEmpty ()) {
101
102
return builder .build ();
102
103
}
103
-
104
104
switch (builder .getMetricDescriptor ().getType ()) {
105
- case UNSPECIFIED :
106
105
case UNRECOGNIZED :
106
+ case INVALID_TYPE :
107
107
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 ()));
111
112
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 ()));
115
117
break ;
116
- case GAUGE_HISTOGRAM :
117
- case CUMULATIVE_HISTOGRAM :
118
+ case HISTOGRAM :
118
119
// TODO: Add support for histogram.
119
120
break ;
120
121
case SUMMARY :
121
- builder .addAllSummaryDataPoints (toSummaryDataPoints (metricData .getPoints ()));
122
+ builder .addAllSummaryDataPoints (
123
+ toSummaryDataPoints (metricData .getPoints (), metricData .getDescriptor ()));
122
124
break ;
123
125
}
124
126
return builder .build ();
@@ -130,11 +132,25 @@ static MetricDescriptor toProtoMetricDescriptor(Descriptor descriptor) {
130
132
.setDescription (descriptor .getDescription ())
131
133
.setUnit (descriptor .getUnit ())
132
134
.setType (toProtoMetricDescriptorType (descriptor .getType ()))
133
- .addAllLabels ( toProtoLabels (descriptor . getConstantLabels () ))
135
+ .setTemporality ( mapToTemporality (descriptor ))
134
136
.build ();
135
137
}
136
138
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 ) {
138
154
List <Int64DataPoint > result = new ArrayList <>(points .size ());
139
155
for (Point point : points ) {
140
156
LongPoint longPoint = (LongPoint ) point ;
@@ -144,6 +160,9 @@ static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
144
160
.setTimeUnixNano (longPoint .getEpochNanos ())
145
161
.setValue (longPoint .getValue ());
146
162
// 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
+ }
147
166
Collection <StringKeyValue > labels = toProtoLabels (longPoint .getLabels ());
148
167
if (!labels .isEmpty ()) {
149
168
builder .addAllLabels (labels );
@@ -153,7 +172,8 @@ static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
153
172
return result ;
154
173
}
155
174
156
- static Collection <DoubleDataPoint > toDoubleDataPoints (Collection <Point > points ) {
175
+ static Collection <DoubleDataPoint > toDoubleDataPoints (
176
+ Collection <Point > points , Descriptor descriptor ) {
157
177
List <DoubleDataPoint > result = new ArrayList <>(points .size ());
158
178
for (Point point : points ) {
159
179
DoublePoint doublePoint = (DoublePoint ) point ;
@@ -163,6 +183,9 @@ static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points)
163
183
.setTimeUnixNano (doublePoint .getEpochNanos ())
164
184
.setValue (doublePoint .getValue ());
165
185
// 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
+ }
166
189
Collection <StringKeyValue > labels = toProtoLabels (doublePoint .getLabels ());
167
190
if (!labels .isEmpty ()) {
168
191
builder .addAllLabels (labels );
@@ -172,7 +195,8 @@ static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points)
172
195
return result ;
173
196
}
174
197
175
- static Collection <SummaryDataPoint > toSummaryDataPoints (Collection <Point > points ) {
198
+ static Collection <SummaryDataPoint > toSummaryDataPoints (
199
+ Collection <Point > points , Descriptor descriptor ) {
176
200
List <SummaryDataPoint > result = new ArrayList <>(points .size ());
177
201
for (Point point : points ) {
178
202
SummaryPoint summaryPoint = (SummaryPoint ) point ;
@@ -184,6 +208,9 @@ static Collection<SummaryDataPoint> toSummaryDataPoints(Collection<Point> points
184
208
.setSum (summaryPoint .getSum ());
185
209
// Not calling directly addAllLabels because that generates couple of unnecessary allocations
186
210
// if empty list.
211
+ if (descriptor .getConstantLabels () != null && !descriptor .getConstantLabels ().isEmpty ()) {
212
+ builder .addAllLabels (toProtoLabels (descriptor .getConstantLabels ()));
213
+ }
187
214
Collection <StringKeyValue > labels = toProtoLabels (summaryPoint .getLabels ());
188
215
if (!labels .isEmpty ()) {
189
216
builder .addAllLabels (labels );
@@ -221,17 +248,17 @@ static List<ValueAtPercentile> toProtoValueAtPercentiles(
221
248
static MetricDescriptor .Type toProtoMetricDescriptorType (Descriptor .Type descriptorType ) {
222
249
switch (descriptorType ) {
223
250
case NON_MONOTONIC_LONG :
224
- return Type .GAUGE_INT64 ;
251
+ return Type .INT64 ;
225
252
case NON_MONOTONIC_DOUBLE :
226
- return Type .GAUGE_DOUBLE ;
253
+ return Type .DOUBLE ;
227
254
case MONOTONIC_LONG :
228
- return Type .COUNTER_INT64 ;
255
+ return Type .MONOTONIC_INT64 ;
229
256
case MONOTONIC_DOUBLE :
230
- return Type .COUNTER_DOUBLE ;
257
+ return Type .MONOTONIC_DOUBLE ;
231
258
case SUMMARY :
232
259
return Type .SUMMARY ;
233
260
}
234
- return Type .UNSPECIFIED ;
261
+ return Type .UNRECOGNIZED ;
235
262
}
236
263
237
264
@ SuppressWarnings ("MixedMutabilityReturnType" )
0 commit comments