Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
22beaee
add MetricDataType.HISTOGRAM and related data container types
beanliu Jan 22, 2021
ed0251b
add no-op histogram support for the OTLP/Prometheus exporters
beanliu Jan 25, 2021
c5697b6
add Histogram aggregator and accumulation
beanliu Jan 22, 2021
264979b
fixup! add MetricDataType.HISTOGRAM and related data container types
beanliu Jan 27, 2021
0859553
fixup! add Histogram aggregator and accumulation
beanliu Jan 27, 2021
088f615
fixup! add Histogram aggregator and accumulation
beanliu Jan 27, 2021
a7e9142
fixup! add Histogram aggregator and accumulation
beanliu Jan 27, 2021
7b8cebd
fixup! add Histogram aggregator and accumulation
beanliu Jan 27, 2021
75c9e4b
fixup! add Histogram aggregator and accumulation
beanliu Jan 28, 2021
79c1e0e
use Immutable(Long|Double)Array for histogram aggregation
beanliu Jan 28, 2021
e12b6c8
fixup! use Immutable(Long|Double)Array for histogram aggregation
beanliu Jan 28, 2021
d4c2a0d
fixup! use Immutable(Long|Double)Array for histogram aggregation
beanliu Jan 28, 2021
ce83dbc
fixup! use Immutable(Long|Double)Array for histogram aggregation
beanliu Jan 29, 2021
d46ba13
fixup! use Immutable(Long|Double)Array for histogram aggregation
beanliu Jan 31, 2021
09dd32b
remove ImmutableDoubleArray.of()
beanliu Feb 22, 2021
7696284
remove boundaries from HistogramAccumulation
beanliu Feb 22, 2021
df1d459
Merge remote-tracking branch 'upstream/main' into histogram-aggregati…
beanliu Feb 23, 2021
34ef27b
move ImmutableDoubleArray and ImmutableLongArray to sdk.metrics.aggre…
beanliu Feb 23, 2021
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ static AggregatorFactory count(AggregationTemporality temporality) {
* the measurements taken.
*
* @param stateful configures if the aggregator is stateful.
* @param boundary configures the fixed bucket boundaries.
* @param boundaries configures the fixed bucket boundaries.
* @return an {@code AggregationFactory} that calculates histogram of recorded measurements.
*/
static AggregatorFactory histogram(double[] boundary, boolean stateful) {
return new HistogramAggregatorFactory(boundary, stateful);
static AggregatorFactory histogram(double[] boundaries, boolean stateful) {
return new HistogramAggregatorFactory(boundaries, stateful);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
import io.opentelemetry.sdk.metrics.data.DoubleHistogramData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.resources.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.concurrent.GuardedBy;

final class DoubleHistogramAggregator extends AbstractAggregator<HistogramAccumulation> {
private static final Logger logger = Logger.getLogger(DoubleHistogramAggregator.class.getName());

private static boolean LoggedMergingInvalidBoundaries = false;
Comment thread
beanliu marked this conversation as resolved.
Outdated

private final double[] boundaries;

DoubleHistogramAggregator(
Expand All @@ -32,6 +39,22 @@ final class DoubleHistogramAggregator extends AbstractAggregator<HistogramAccumu
this.boundaries = boundaries;
}

private static List<Double> asUnmodifiableDoubleList(double[] xs) {
List<Double> result = new ArrayList<>(xs.length);
for (double x : xs) {
result.add(x);
}
return result;
}

private static List<Long> asUnmodifiableLongList(long[] xs) {
List<Long> result = new ArrayList<>(xs.length);
for (long x : xs) {
result.add(x);
}
return result;
}

@Override
public AggregatorHandle<HistogramAccumulation> createHandle() {
return new Handle(this.boundaries);
Expand All @@ -40,7 +63,19 @@ public AggregatorHandle<HistogramAccumulation> createHandle() {
@Override
public final HistogramAccumulation merge(HistogramAccumulation x, HistogramAccumulation y) {
if (!x.getBoundaries().equals(y.getBoundaries())) {
throw new IllegalArgumentException("can't merge histograms with different boundaries");
// If this happens, it's a pretty severe bug in the SDK.
if (!LoggedMergingInvalidBoundaries) {
logger.log(
Level.SEVERE,
"can't merge histograms with different boundaries, something's very wrong: "
+ "x.boundaries="
+ x.getBoundaries()
+ " y.boundaries="
+ y.getBoundaries());
LoggedMergingInvalidBoundaries = true;
}
return HistogramAccumulation.create(
0, 0, Collections.emptyList(), Collections.singletonList(0L));
}

long[] mergedCounts = new long[x.getCounts().size()];
Expand All @@ -51,7 +86,7 @@ public final HistogramAccumulation merge(HistogramAccumulation x, HistogramAccum
x.getCount() + y.getCount(),
x.getSum() + y.getSum(),
x.getBoundaries(),
Arrays.stream(mergedCounts).boxed().collect(Collectors.toList()));
asUnmodifiableLongList(mergedCounts));
}

@Override
Expand Down Expand Up @@ -98,19 +133,20 @@ static final class Handle extends AggregatorHandle<HistogramAccumulation> {

@Override
protected HistogramAccumulation doAccumulateThenReset() {
List<Long> counts;
lock.writeLock().lock();
Comment thread
beanliu marked this conversation as resolved.
Outdated
try {
HistogramAccumulation result =
HistogramAccumulation.create(
current.count,
current.sum,
Arrays.stream(current.boundaries).boxed().collect(Collectors.toList()),
Arrays.stream(current.counts).boxed().collect(Collectors.toList()));
counts = asUnmodifiableLongList(current.counts);
current.reset();
return result;
} finally {
lock.writeLock().unlock();
}

return HistogramAccumulation.create(
counts.stream().mapToLong(i -> i).sum(),
current.sum,
asUnmodifiableDoubleList(current.boundaries),
asUnmodifiableLongList(current.counts));
}

@Override
Expand All @@ -131,7 +167,6 @@ protected void doRecordLong(long value) {
}

private static final class State {
private long count;
private double sum;
private final double[] boundaries;
private final long[] counts;
Expand All @@ -153,13 +188,11 @@ private int findBucketIndex(double value) {
}

private void reset() {
this.count = 0;
this.sum = 0;
Arrays.fill(this.counts, 0);
}

private void record(int bucketIndex, double value) {
this.count++;
this.sum += value;
this.counts[bucketIndex]++;
}
Expand Down
Loading