Skip to content

Commit 34ca25a

Browse files
committed
Add test case to verify there are no batches with empty metric points
1 parent ea6330e commit 34ca25a

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/export/MetricExportBatcherTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,62 @@ void batchMetrics_SplitsLongGauge_MultipleMetrics_ExceedsCapacity() {
449449
assertThat(b2m1.getLongGaugeData().getPoints()).containsExactly(p5, p6);
450450
}
451451

452+
@Test
453+
void batchMetrics_SplitsLongGauge_MultipleMetrics_PerfectFillThenSplit() {
454+
// m1 fills the batch completely (remaining capacity becomes 0).
455+
// m2 has 3 points, which forces it to split from the start of a fully-exhausted
456+
// previous pass.
457+
// This test case fails if there is an empty batch
458+
MetricExportBatcher batcher = new MetricExportBatcher(2);
459+
LongPointData p1 = ImmutableLongPointData.create(1, 2, Attributes.empty(), 1L);
460+
LongPointData p2 = ImmutableLongPointData.create(1, 2, Attributes.empty(), 2L);
461+
LongPointData p3 = ImmutableLongPointData.create(1, 2, Attributes.empty(), 3L);
462+
LongPointData p4 = ImmutableLongPointData.create(1, 2, Attributes.empty(), 4L);
463+
LongPointData p5 = ImmutableLongPointData.create(1, 2, Attributes.empty(), 5L);
464+
465+
MetricData m1 =
466+
ImmutableMetricData.createLongGauge(
467+
Resource.empty(),
468+
InstrumentationScopeInfo.empty(),
469+
"name_1",
470+
"desc",
471+
"1",
472+
ImmutableGaugeData.create(Arrays.asList(p1, p2)));
473+
MetricData m2 =
474+
ImmutableMetricData.createLongGauge(
475+
Resource.empty(),
476+
InstrumentationScopeInfo.empty(),
477+
"name_2",
478+
"desc",
479+
"1",
480+
ImmutableGaugeData.create(Arrays.asList(p3, p4, p5)));
481+
482+
Collection<Collection<MetricData>> batches = batcher.batchMetrics(Arrays.asList(m1, m2));
483+
484+
assertThat(batches).hasSize(3);
485+
486+
// Batch 1 should contain exactly m1 (p1, p2)
487+
Collection<MetricData> firstBatch = batches.iterator().next();
488+
assertThat(firstBatch).hasSize(1);
489+
MetricData b1m1 = firstBatch.iterator().next();
490+
assertThat(b1m1.getName()).isEqualTo("name_1");
491+
assertThat(b1m1.getLongGaugeData().getPoints()).containsExactly(p1, p2);
492+
493+
// Batch 2 should contain the first part of m2 (p3, p4)
494+
Collection<MetricData> secondBatch = batches.stream().skip(1).findFirst().get();
495+
assertThat(secondBatch).hasSize(1);
496+
MetricData b2m1 = secondBatch.iterator().next();
497+
assertThat(b2m1.getName()).isEqualTo("name_2");
498+
assertThat(b2m1.getLongGaugeData().getPoints()).containsExactly(p3, p4);
499+
500+
// Batch 3 should contain the rest of m2 (p5)
501+
Collection<MetricData> thirdBatch = batches.stream().skip(2).findFirst().get();
502+
assertThat(thirdBatch).hasSize(1);
503+
MetricData b3m1 = thirdBatch.iterator().next();
504+
assertThat(b3m1.getName()).isEqualTo("name_2");
505+
assertThat(b3m1.getLongGaugeData().getPoints()).containsExactly(p5);
506+
}
507+
452508
@Test
453509
void batchMetrics_SplitsExponentialHistogram_MultipleBatchesCompletelyFilled_SingleMetric() {
454510
MetricExportBatcher batcher = new MetricExportBatcher(1);

0 commit comments

Comments
 (0)