Search in sources :

Example 1 with HistogramDataPoint

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;
}
Also used : Exemplar(io.opentelemetry.proto.metrics.v1.Exemplar) Message(com.google.protobuf.Message) HistogramDataPoint(io.opentelemetry.proto.metrics.v1.HistogramDataPoint) ExponentialHistogramDataPoint(io.opentelemetry.proto.metrics.v1.ExponentialHistogramDataPoint) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) UncheckedIOException(java.io.UncheckedIOException) ByteString(com.google.protobuf.ByteString) ExponentialHistogramDataPoint(io.opentelemetry.proto.metrics.v1.ExponentialHistogramDataPoint) NumberDataPoint(io.opentelemetry.proto.metrics.v1.NumberDataPoint)

Aggregations

ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 Exemplar (io.opentelemetry.proto.metrics.v1.Exemplar)1 ExponentialHistogramDataPoint (io.opentelemetry.proto.metrics.v1.ExponentialHistogramDataPoint)1 HistogramDataPoint (io.opentelemetry.proto.metrics.v1.HistogramDataPoint)1 NumberDataPoint (io.opentelemetry.proto.metrics.v1.NumberDataPoint)1 UncheckedIOException (java.io.UncheckedIOException)1