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;
}
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()));
}
Aggregations