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

lazily initialize ReservoirCells #6851

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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 @@ -13,13 +13,16 @@
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import javax.annotation.Nullable;

/** Base for fixed-size reservoir sampling of Exemplars. */
abstract class FixedSizeExemplarReservoir<T extends ExemplarData> implements ExemplarReservoir<T> {

private final ReservoirCell[] storage;
@Nullable private ReservoirCell[] storage;
private final ReservoirCellSelector reservoirCellSelector;
private final BiFunction<ReservoirCell, Attributes, T> mapAndResetCell;
private final int size;
private final Clock clock;
private volatile boolean hasMeasurements = false;

/** Instantiates an exemplar reservoir of fixed size. */
Expand All @@ -28,16 +31,18 @@
int size,
ReservoirCellSelector reservoirCellSelector,
BiFunction<ReservoirCell, Attributes, T> mapAndResetCell) {
this.storage = new ReservoirCell[size];
for (int i = 0; i < size; ++i) {
this.storage[i] = new ReservoirCell(clock);
}
this.storage = null; // lazily initialize to avoid allocations
this.size = size;
this.clock = clock;
this.reservoirCellSelector = reservoirCellSelector;
this.mapAndResetCell = mapAndResetCell;
}

@Override
public void offerLongMeasurement(long value, Attributes attributes, Context context) {
if (storage == null) {
storage = initStorage();
}
int bucket = reservoirCellSelector.reservoirCellIndexFor(storage, value, attributes, context);
if (bucket != -1) {
this.storage[bucket].recordLongMeasurement(value, attributes, context);
Expand All @@ -47,18 +52,32 @@

@Override
public void offerDoubleMeasurement(double value, Attributes attributes, Context context) {
if (storage == null) {
storage = initStorage();
}
int bucket = reservoirCellSelector.reservoirCellIndexFor(storage, value, attributes, context);
if (bucket != -1) {
this.storage[bucket].recordDoubleMeasurement(value, attributes, context);
this.hasMeasurements = true;
}
}

private ReservoirCell[] initStorage() {
ReservoirCell[] storage = new ReservoirCell[this.size];
for (int i = 0; i < size; ++i) {
storage[i] = new ReservoirCell(this.clock);
}
return storage;
}

@Override
public List<T> collectAndReset(Attributes pointAttributes) {
if (!hasMeasurements) {
return Collections.emptyList();
}
if (storage == null) {
storage = initStorage();

Check warning on line 79 in sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/FixedSizeExemplarReservoir.java

View check run for this annotation

Codecov / codecov/patch

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/FixedSizeExemplarReservoir.java#L79

Added line #L79 was not covered by tests
}
edma2 marked this conversation as resolved.
Show resolved Hide resolved
// Note: we are collecting exemplars from buckets piecemeal, but we
// could still be sampling exemplars during this process.
List<T> results = new ArrayList<>();
Expand Down
Loading