use of io.opentelemetry.proto.metrics.v1.internal.HistogramDataPoint in project opentelemetry-java by open-telemetry.
the class MetricsRequestMarshalerTest method parse.
@SuppressWarnings("unchecked")
private static <T extends Message> T parse(T prototype, Marshaler marshaler) {
byte[] serialized = toByteArray(marshaler);
T result;
try {
result = (T) prototype.newBuilderForType().mergeFrom(serialized).build();
} catch (InvalidProtocolBufferException e) {
throw new UncheckedIOException(e);
}
// Our marshaler should produce the exact same length of serialized output (for example, field
// default values are not outputted), so we check that here. The output itself may have slightly
// different ordering, mostly due to the way we don't output oneof values in field order all the
// tieme. If the lengths are equal and the resulting protos are equal, the marshaling is
// guaranteed to be valid.
assertThat(result.getSerializedSize()).isEqualTo(serialized.length);
// We don't compare JSON strings due to some differences (particularly serializing enums as
// numbers instead of names). This may improve in the future but what matters is what we produce
// can be parsed.
String json = toJson(marshaler);
Message.Builder builder = prototype.newBuilderForType();
try {
JsonFormat.parser().merge(json, builder);
} catch (InvalidProtocolBufferException e) {
throw new UncheckedIOException(e);
}
// libraries currently support customizing on the parse side.
if (result instanceof NumberDataPoint) {
NumberDataPoint.Builder fixed = (NumberDataPoint.Builder) builder;
for (Exemplar.Builder exemplar : fixed.getExemplarsBuilderList()) {
exemplar.setTraceId(toHex(exemplar.getTraceId()));
exemplar.setSpanId(toHex(exemplar.getSpanId()));
}
}
if (result instanceof HistogramDataPoint) {
HistogramDataPoint.Builder fixed = (HistogramDataPoint.Builder) builder;
for (Exemplar.Builder exemplar : fixed.getExemplarsBuilderList()) {
exemplar.setTraceId(toHex(exemplar.getTraceId()));
exemplar.setSpanId(toHex(exemplar.getSpanId()));
}
}
if (result instanceof ExponentialHistogramDataPoint) {
ExponentialHistogramDataPoint.Builder fixed = (ExponentialHistogramDataPoint.Builder) builder;
for (Exemplar.Builder exemplar : fixed.getExemplarsBuilderList()) {
exemplar.setTraceId(toHex(exemplar.getTraceId()));
exemplar.setSpanId(toHex(exemplar.getSpanId()));
}
}
assertThat(builder.build()).isEqualTo(result);
return result;
}
Aggregations