Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid exemplar allocations if there are no measurements #5182

Merged
merged 1 commit into from
Feb 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ abstract class FixedSizeExemplarReservoir<T extends ExemplarData> implements Exe
private final ReservoirCell[] storage;
private final ReservoirCellSelector reservoirCellSelector;
private final BiFunction<ReservoirCell, Attributes, T> mapAndResetCell;
private volatile boolean hasMeasurements = false;

/** Instantiates an exemplar reservoir of fixed size. */
FixedSizeExemplarReservoir(
Expand All @@ -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;
}
}

Expand All @@ -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<T> 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<T> results = new ArrayList<>();
Expand All @@ -63,6 +69,7 @@ public List<T> collectAndReset(Attributes pointAttributes) {
}
}
reservoirCellSelector.reset();
this.hasMeasurements = false;
return Collections.unmodifiableList(results);
}
}