From b0818b6a7c96296d89e4630a5bc6a59fcb1a4f2c Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Tue, 7 Feb 2023 15:41:13 -0600 Subject: [PATCH] Avoid exemplar allocations if there are no measurements --- .../internal/exemplar/FixedSizeExemplarReservoir.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/FixedSizeExemplarReservoir.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/FixedSizeExemplarReservoir.java index a5b65382b59..05dfdd1afd3 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/FixedSizeExemplarReservoir.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/FixedSizeExemplarReservoir.java @@ -20,6 +20,7 @@ abstract class FixedSizeExemplarReservoir implements Exe private final ReservoirCell[] storage; private final ReservoirCellSelector reservoirCellSelector; private final BiFunction mapAndResetCell; + private volatile boolean hasMeasurements = false; /** Instantiates an exemplar reservoir of fixed size. */ FixedSizeExemplarReservoir( @@ -40,6 +41,7 @@ public void offerLongMeasurement(long value, Attributes attributes, Context cont int bucket = reservoirCellSelector.reservoirCellIndexFor(storage, value, attributes, context); if (bucket != -1) { this.storage[bucket].recordLongMeasurement(value, attributes, context); + this.hasMeasurements = true; } } @@ -48,11 +50,15 @@ public void offerDoubleMeasurement(double value, Attributes attributes, Context int bucket = reservoirCellSelector.reservoirCellIndexFor(storage, value, attributes, context); if (bucket != -1) { this.storage[bucket].recordDoubleMeasurement(value, attributes, context); + this.hasMeasurements = true; } } @Override public List collectAndReset(Attributes pointAttributes) { + if (!hasMeasurements) { + return Collections.emptyList(); + } // Note: we are collecting exemplars from buckets piecemeal, but we // could still be sampling exemplars during this process. List results = new ArrayList<>(); @@ -63,6 +69,7 @@ public List collectAndReset(Attributes pointAttributes) { } } reservoirCellSelector.reset(); + this.hasMeasurements = false; return Collections.unmodifiableList(results); } }