Search in sources :

Example 1 with NumberDataPoint

use of io.opentelemetry.proto.metrics.v1.internal.NumberDataPoint 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)

Example 2 with NumberDataPoint

use of io.opentelemetry.proto.metrics.v1.internal.NumberDataPoint in project opentelemetry-java by open-telemetry.

the class OtlpExporterIntegrationTest method testMetricExport.

private static void testMetricExport(MetricExporter metricExporter) {
    MetricReaderFactory reader = PeriodicMetricReader.builder(metricExporter).setInterval(Duration.ofSeconds(5)).newMetricReaderFactory();
    SdkMeterProvider meterProvider = SdkMeterProvider.builder().setResource(RESOURCE).registerMetricReader(reader).build();
    Meter meter = meterProvider.meterBuilder(OtlpExporterIntegrationTest.class.getName()).build();
    LongCounter longCounter = meter.counterBuilder("my-counter").build();
    longCounter.add(100, Attributes.builder().put("key", "value").build());
    try {
        await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.metricRequests).hasSize(1));
    } finally {
        meterProvider.close();
    }
    ExportMetricsServiceRequest request = grpcServer.metricRequests.get(0);
    assertThat(request.getResourceMetricsCount()).isEqualTo(1);
    ResourceMetrics resourceMetrics = request.getResourceMetrics(0);
    assertThat(resourceMetrics.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
    assertThat(resourceMetrics.getInstrumentationLibraryMetricsCount()).isEqualTo(1);
    InstrumentationLibraryMetrics ilMetrics = resourceMetrics.getInstrumentationLibraryMetrics(0);
    assertThat(ilMetrics.getInstrumentationLibrary().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
    assertThat(ilMetrics.getMetricsCount()).isEqualTo(1);
    Metric metric = ilMetrics.getMetrics(0);
    assertThat(metric.getName()).isEqualTo("my-counter");
    assertThat(metric.getDataCase()).isEqualTo(Metric.DataCase.SUM);
    Sum sum = metric.getSum();
    assertThat(sum.getAggregationTemporality()).isEqualTo(AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE);
    assertThat(sum.getDataPointsCount()).isEqualTo(1);
    NumberDataPoint dataPoint = sum.getDataPoints(0);
    assertThat(dataPoint.getAsInt()).isEqualTo(100);
    assertThat(dataPoint.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
}
Also used : ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) Meter(io.opentelemetry.api.metrics.Meter) NumberDataPoint(io.opentelemetry.proto.metrics.v1.NumberDataPoint) MetricReaderFactory(io.opentelemetry.sdk.metrics.export.MetricReaderFactory) Metric(io.opentelemetry.proto.metrics.v1.Metric) Sum(io.opentelemetry.proto.metrics.v1.Sum) InstrumentationLibraryMetrics(io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics) LongCounter(io.opentelemetry.api.metrics.LongCounter) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest)

Aggregations

NumberDataPoint (io.opentelemetry.proto.metrics.v1.NumberDataPoint)2 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 LongCounter (io.opentelemetry.api.metrics.LongCounter)1 Meter (io.opentelemetry.api.metrics.Meter)1 ExportMetricsServiceRequest (io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest)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 InstrumentationLibraryMetrics (io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics)1 Metric (io.opentelemetry.proto.metrics.v1.Metric)1 ResourceMetrics (io.opentelemetry.proto.metrics.v1.ResourceMetrics)1 Sum (io.opentelemetry.proto.metrics.v1.Sum)1 SdkMeterProvider (io.opentelemetry.sdk.metrics.SdkMeterProvider)1 MetricReaderFactory (io.opentelemetry.sdk.metrics.export.MetricReaderFactory)1 UncheckedIOException (java.io.UncheckedIOException)1