-
Notifications
You must be signed in to change notification settings - Fork 993
Histogram Aggregation - core SDK implementation #2581
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
Closed
beanliu
wants to merge
18
commits into
open-telemetry:main
from
atlassian-forks:histogram-aggregation-core
Closed
Changes from 12 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 ed0251b
add no-op histogram support for the OTLP/Prometheus exporters
beanliu c5697b6
add Histogram aggregator and accumulation
beanliu 264979b
fixup! add MetricDataType.HISTOGRAM and related data container types
beanliu 0859553
fixup! add Histogram aggregator and accumulation
beanliu 088f615
fixup! add Histogram aggregator and accumulation
beanliu a7e9142
fixup! add Histogram aggregator and accumulation
beanliu 7b8cebd
fixup! add Histogram aggregator and accumulation
beanliu 75c9e4b
fixup! add Histogram aggregator and accumulation
beanliu 79c1e0e
use Immutable(Long|Double)Array for histogram aggregation
beanliu e12b6c8
fixup! use Immutable(Long|Double)Array for histogram aggregation
beanliu d4c2a0d
fixup! use Immutable(Long|Double)Array for histogram aggregation
beanliu ce83dbc
fixup! use Immutable(Long|Double)Array for histogram aggregation
beanliu d46ba13
fixup! use Immutable(Long|Double)Array for histogram aggregation
beanliu 09dd32b
remove ImmutableDoubleArray.of()
beanliu 7696284
remove boundaries from HistogramAccumulation
beanliu df1d459
Merge remote-tracking branch 'upstream/main' into histogram-aggregati…
beanliu 34ef27b
move ImmutableDoubleArray and ImmutableLongArray to sdk.metrics.aggre…
beanliu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...etrics/src/jmh/java/io/opentelemetry/sdk/metrics/aggregator/DoubleHistogramBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.metrics.aggregator; | ||
|
|
||
| import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; | ||
| import io.opentelemetry.sdk.metrics.common.ImmutableDoubleArray; | ||
| import io.opentelemetry.sdk.metrics.common.InstrumentDescriptor; | ||
| import io.opentelemetry.sdk.metrics.common.InstrumentType; | ||
| import io.opentelemetry.sdk.metrics.common.InstrumentValueType; | ||
| import io.opentelemetry.sdk.resources.Resource; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public class DoubleHistogramBenchmark { | ||
| private static final Aggregator<HistogramAccumulation> aggregator = | ||
| AggregatorFactory.histogram( | ||
| ImmutableDoubleArray.copyOf(new double[] {10, 100, 1_000}), /* stateful= */ false) | ||
| .create( | ||
| Resource.getDefault(), | ||
| InstrumentationLibraryInfo.getEmpty(), | ||
| InstrumentDescriptor.create( | ||
| "name", | ||
| "description", | ||
| "1", | ||
| InstrumentType.VALUE_RECORDER, | ||
| InstrumentValueType.DOUBLE)); | ||
| private AggregatorHandle<HistogramAccumulation> aggregatorHandle; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public final void setup() { | ||
| aggregatorHandle = aggregator.createHandle(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Fork(1) | ||
| @Warmup(iterations = 5, time = 1) | ||
| @Measurement(iterations = 10, time = 1) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| @Threads(value = 10) | ||
| public void aggregate_10Threads() { | ||
| aggregatorHandle.recordDouble(100.0056); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Fork(1) | ||
| @Warmup(iterations = 5, time = 1) | ||
| @Measurement(iterations = 10, time = 1) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| @Threads(value = 5) | ||
| public void aggregate_5Threads() { | ||
| aggregatorHandle.recordDouble(100.0056); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Fork(1) | ||
| @Warmup(iterations = 5, time = 1) | ||
| @Measurement(iterations = 10, time = 1) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| @Threads(value = 1) | ||
| public void aggregate_1Threads() { | ||
| aggregatorHandle.recordDouble(100.0056); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
...rics/src/main/java/io/opentelemetry/sdk/metrics/aggregator/DoubleHistogramAggregator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.metrics.aggregator; | ||
|
|
||
| import io.opentelemetry.api.common.Labels; | ||
| import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; | ||
| import io.opentelemetry.sdk.metrics.common.ImmutableDoubleArray; | ||
| import io.opentelemetry.sdk.metrics.common.ImmutableLongArray; | ||
| import io.opentelemetry.sdk.metrics.common.InstrumentDescriptor; | ||
| import io.opentelemetry.sdk.metrics.data.AggregationTemporality; | ||
| import io.opentelemetry.sdk.metrics.data.DoubleHistogramData; | ||
| import io.opentelemetry.sdk.metrics.data.MetricData; | ||
| import io.opentelemetry.sdk.resources.Resource; | ||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
| 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 volatile boolean loggedMergingInvalidBoundaries = false; | ||
|
|
||
| private final ImmutableDoubleArray boundaries; | ||
|
|
||
| DoubleHistogramAggregator( | ||
| Resource resource, | ||
| InstrumentationLibraryInfo instrumentationLibraryInfo, | ||
| InstrumentDescriptor instrumentDescriptor, | ||
| ImmutableDoubleArray boundaries, | ||
| boolean stateful) { | ||
| super(resource, instrumentationLibraryInfo, instrumentDescriptor, stateful); | ||
| this.boundaries = boundaries; | ||
| } | ||
|
|
||
| @Override | ||
| public AggregatorHandle<HistogramAccumulation> createHandle() { | ||
| return new Handle(this.boundaries); | ||
| } | ||
|
|
||
| @Override | ||
| public final HistogramAccumulation merge(HistogramAccumulation x, HistogramAccumulation y) { | ||
| if (!x.getBoundaries().equals(y.getBoundaries())) { | ||
| // 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, ImmutableDoubleArray.of(), ImmutableLongArray.of(0)); | ||
| } | ||
|
|
||
| long[] mergedCounts = new long[x.getCounts().length()]; | ||
| for (int i = 0; i < x.getCounts().length(); ++i) { | ||
| mergedCounts[i] = x.getCounts().get(i) + y.getCounts().get(i); | ||
| } | ||
| return HistogramAccumulation.create( | ||
| x.getCount() + y.getCount(), | ||
| x.getSum() + y.getSum(), | ||
| x.getBoundaries(), | ||
| ImmutableLongArray.copyOf(mergedCounts)); | ||
| } | ||
|
|
||
| @Override | ||
| public final MetricData toMetricData( | ||
| Map<Labels, HistogramAccumulation> accumulationByLabels, | ||
| long startEpochNanos, | ||
| long lastCollectionEpoch, | ||
| long epochNanos) { | ||
| return MetricData.createDoubleHistogram( | ||
| getResource(), | ||
| getInstrumentationLibraryInfo(), | ||
| getInstrumentDescriptor().getName(), | ||
| getInstrumentDescriptor().getDescription(), | ||
| getInstrumentDescriptor().getUnit(), | ||
| DoubleHistogramData.create( | ||
| isStateful() ? AggregationTemporality.CUMULATIVE : AggregationTemporality.DELTA, | ||
| MetricDataUtils.toDoubleHistogramPointList( | ||
| accumulationByLabels, | ||
| isStateful() ? startEpochNanos : lastCollectionEpoch, | ||
| epochNanos))); | ||
| } | ||
|
|
||
| @Override | ||
| public HistogramAccumulation accumulateDouble(double value) { | ||
| return HistogramAccumulation.create( | ||
| 1, value, ImmutableDoubleArray.of(), ImmutableLongArray.of(1)); | ||
| } | ||
|
|
||
| @Override | ||
| public HistogramAccumulation accumulateLong(long value) { | ||
| return HistogramAccumulation.create( | ||
| 1, value, ImmutableDoubleArray.of(), ImmutableLongArray.of(1)); | ||
| } | ||
|
|
||
| static final class Handle extends AggregatorHandle<HistogramAccumulation> { | ||
| private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
|
|
||
| @GuardedBy("lock") | ||
| private final State current; | ||
|
|
||
| Handle(ImmutableDoubleArray boundaries) { | ||
| current = new State(boundaries); | ||
| } | ||
|
|
||
| @Override | ||
| protected HistogramAccumulation doAccumulateThenReset() { | ||
| double sum; | ||
| ImmutableLongArray counts; | ||
| lock.writeLock().lock(); | ||
|
beanliu marked this conversation as resolved.
Outdated
|
||
| try { | ||
| sum = current.sum; | ||
| counts = ImmutableLongArray.copyOf(current.counts); | ||
| current.reset(); | ||
| } finally { | ||
| lock.writeLock().unlock(); | ||
| } | ||
|
|
||
| long totalCount = 0; | ||
| for (int i = 0; i < counts.length(); ++i) { | ||
| totalCount += counts.get(i); | ||
| } | ||
|
|
||
| return HistogramAccumulation.create(totalCount, sum, current.boundaries, counts); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doRecordDouble(double value) { | ||
| int bucketIndex = current.findBucketIndex(value); | ||
|
|
||
| lock.writeLock().lock(); | ||
| try { | ||
| current.record(bucketIndex, value); | ||
| } finally { | ||
| lock.writeLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void doRecordLong(long value) { | ||
| doRecordDouble((double) value); | ||
| } | ||
|
|
||
| private static final class State { | ||
| private double sum; | ||
| private final ImmutableDoubleArray boundaries; | ||
| private final long[] counts; | ||
|
|
||
| public State(ImmutableDoubleArray boundaries) { | ||
| this.boundaries = boundaries; | ||
| this.counts = new long[this.boundaries.length() + 1]; | ||
| reset(); | ||
| } | ||
|
|
||
| // Benchmark shows that linear search performs better than binary search with ordinary | ||
| // buckets. | ||
| private int findBucketIndex(double value) { | ||
| for (int i = 0; i < this.boundaries.length(); ++i) { | ||
| if (value < this.boundaries.get(i)) { | ||
| return i; | ||
| } | ||
| } | ||
| return this.boundaries.length(); | ||
| } | ||
|
|
||
| private void reset() { | ||
| this.sum = 0; | ||
| Arrays.fill(this.counts, 0); | ||
| } | ||
|
|
||
| private void record(int bucketIndex, double value) { | ||
| this.sum += value; | ||
| this.counts[bucketIndex]++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
63 changes: 63 additions & 0 deletions
63
sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/aggregator/HistogramAccumulation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.metrics.aggregator; | ||
|
|
||
| import com.google.auto.value.AutoValue; | ||
| import io.opentelemetry.api.common.Labels; | ||
| import io.opentelemetry.sdk.metrics.common.ImmutableDoubleArray; | ||
| import io.opentelemetry.sdk.metrics.common.ImmutableLongArray; | ||
| import io.opentelemetry.sdk.metrics.data.DoubleHistogramPointData; | ||
| import javax.annotation.concurrent.Immutable; | ||
|
|
||
| @Immutable | ||
| @AutoValue | ||
| public abstract class HistogramAccumulation { | ||
|
beanliu marked this conversation as resolved.
Outdated
beanliu marked this conversation as resolved.
Outdated
|
||
| /** | ||
| * Creates a new {@link HistogramAccumulation} with the given values. | ||
| * | ||
| * @return a new {@link HistogramAccumulation} with the given values. | ||
| */ | ||
| static HistogramAccumulation create( | ||
| long count, double sum, ImmutableDoubleArray boundaries, ImmutableLongArray counts) { | ||
| return new AutoValue_HistogramAccumulation(count, sum, boundaries, counts); | ||
| } | ||
|
|
||
| HistogramAccumulation() {} | ||
|
|
||
| /** | ||
| * The number of measurements taken. | ||
| * | ||
| * @return the count of recorded measurements. | ||
| */ | ||
| abstract long getCount(); | ||
|
|
||
| /** | ||
| * The sum of all measurements recorded. | ||
| * | ||
| * @return the sum of recorded measurements. | ||
| */ | ||
| abstract double getSum(); | ||
|
|
||
| /** | ||
| * The bucket boundaries. For a Histogram with N defined boundaries, e.g, [x, y, z]. There are N+1 | ||
| * counts: [-inf, x), [x, y), [y, z), [z, +inf]. | ||
| * | ||
| * @return the bucket boundaries in increasing order. | ||
| */ | ||
| abstract ImmutableDoubleArray getBoundaries(); | ||
|
|
||
| /** | ||
| * The counts in each bucket. | ||
| * | ||
| * @return the counts in each bucket. | ||
| */ | ||
| abstract ImmutableLongArray getCounts(); | ||
|
|
||
| final DoubleHistogramPointData toPoint(long startEpochNanos, long epochNanos, Labels labels) { | ||
| return DoubleHistogramPointData.create( | ||
| startEpochNanos, epochNanos, labels, getSum(), getCount(), getBoundaries(), getCounts()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.