diff --git a/exporters/otlp/src/main/java/io/opentelemetry/exporter/otlp/MetricAdapter.java b/exporters/otlp/src/main/java/io/opentelemetry/exporter/otlp/MetricAdapter.java index 104a8380bb4..26160c8403d 100644 --- a/exporters/otlp/src/main/java/io/opentelemetry/exporter/otlp/MetricAdapter.java +++ b/exporters/otlp/src/main/java/io/opentelemetry/exporter/otlp/MetricAdapter.java @@ -69,6 +69,11 @@ static List toProtoResourceMetrics(Collection metri groupByResourceAndLibrary(Collection metricDataList) { Map>> result = new HashMap<>(); for (MetricData metricData : metricDataList) { + if (metricData.getPoints().isEmpty()) { + // If no points available then ignore. + continue; + } + Resource resource = metricData.getResource(); Map> libraryInfoListMap = result.get(metricData.getResource()); @@ -92,10 +97,6 @@ static Metric toProtoMetric(MetricData metricData) { .setName(metricData.getName()) .setDescription(metricData.getDescription()) .setUnit(metricData.getUnit()); - // If no points available then return. - if (metricData.getPoints().isEmpty()) { - return builder.build(); - } boolean monotonic = false; diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporter/otlp/MetricAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporter/otlp/MetricAdapterTest.java index fffc8905179..485595cd427 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporter/otlp/MetricAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporter/otlp/MetricAdapterTest.java @@ -468,8 +468,26 @@ void toProtoResourceMetrics() { InstrumentationLibrary.newBuilder().setName("name").setVersion("version").build(); InstrumentationLibrary emptyInstrumentationLibraryProto = InstrumentationLibrary.newBuilder().setName("").setVersion("").build(); - Metric metricNoPoints = - Metric.newBuilder().setName("name").setDescription("description").setUnit("1").build(); + Metric metricDoubleSum = + Metric.newBuilder() + .setName("name") + .setDescription("description") + .setUnit("1") + .setDoubleSum( + DoubleSum.newBuilder() + .setIsMonotonic(true) + .setAggregationTemporality(AGGREGATION_TEMPORALITY_CUMULATIVE) + .addDataPoints( + DoubleDataPoint.newBuilder() + .setStartTimeUnixNano(123) + .setTimeUnixNano(456) + .addAllLabels( + singletonList( + StringKeyValue.newBuilder().setKey("k").setValue("v").build())) + .setValue(5.0) + .build()) + .build()) + .build(); assertThat( MetricAdapter.toProtoResourceMetrics( @@ -481,7 +499,8 @@ void toProtoResourceMetrics() { "description", "1", MetricData.Type.MONOTONIC_DOUBLE, - Collections.emptyList()), + Collections.singletonList( + MetricData.DoublePoint.create(123, 456, Labels.of("k", "v"), 5.0))), MetricData.create( resource, instrumentationLibraryInfo, @@ -489,7 +508,8 @@ void toProtoResourceMetrics() { "description", "1", MetricData.Type.MONOTONIC_DOUBLE, - Collections.emptyList()), + Collections.singletonList( + MetricData.DoublePoint.create(123, 456, Labels.of("k", "v"), 5.0))), MetricData.create( Resource.getEmpty(), instrumentationLibraryInfo, @@ -497,7 +517,8 @@ void toProtoResourceMetrics() { "description", "1", MetricData.Type.MONOTONIC_DOUBLE, - Collections.emptyList()), + Collections.singletonList( + MetricData.DoublePoint.create(123, 456, Labels.of("k", "v"), 5.0))), MetricData.create( Resource.getEmpty(), InstrumentationLibraryInfo.getEmpty(), @@ -505,7 +526,8 @@ void toProtoResourceMetrics() { "description", "1", MetricData.Type.MONOTONIC_DOUBLE, - Collections.emptyList())))) + Collections.singletonList( + MetricData.DoublePoint.create(123, 456, Labels.of("k", "v"), 5.0)))))) .containsExactlyInAnyOrder( ResourceMetrics.newBuilder() .setResource(resourceProto) @@ -513,7 +535,7 @@ void toProtoResourceMetrics() { singletonList( InstrumentationLibraryMetrics.newBuilder() .setInstrumentationLibrary(instrumentationLibraryProto) - .addAllMetrics(ImmutableList.of(metricNoPoints, metricNoPoints)) + .addAllMetrics(ImmutableList.of(metricDoubleSum, metricDoubleSum)) .build())) .build(), ResourceMetrics.newBuilder() @@ -522,11 +544,11 @@ void toProtoResourceMetrics() { ImmutableList.of( InstrumentationLibraryMetrics.newBuilder() .setInstrumentationLibrary(emptyInstrumentationLibraryProto) - .addAllMetrics(singletonList(metricNoPoints)) + .addAllMetrics(singletonList(metricDoubleSum)) .build(), InstrumentationLibraryMetrics.newBuilder() .setInstrumentationLibrary(instrumentationLibraryProto) - .addAllMetrics(singletonList(metricNoPoints)) + .addAllMetrics(singletonList(metricDoubleSum)) .build())) .build()); }