Skip to content

Commit

Permalink
Data classes for exponential histogram prototype (#3550)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmoessis committed Sep 20, 2021
1 parent bad62ec commit 230f116
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.data;

import com.google.auto.value.AutoValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.concurrent.Immutable;

/**
* DoubleExponentialHistogramBuckets represents either the positive or negative measurements taken
* for a {@link DoubleExponentialHistogramPointData}.
*/
@AutoValue
@Immutable
public abstract class DoubleExponentialHistogramBuckets {
DoubleExponentialHistogramBuckets() {}

/**
* Create DoubleExponentialHistogramBuckets.
*
* @param offset Signed integer representing the bucket index of the first entry in bucketCounts.
* @param bucketCounts List of counts representing number of measurements that fall into each
* bucket.
* @return a DoubleExponentialHistogramBuckets.
*/
public DoubleExponentialHistogramBuckets create(int offset, List<Long> bucketCounts) {
long totalCount = 0;
for (long count : bucketCounts) {
totalCount += count;
}
return new AutoValue_DoubleExponentialHistogramBuckets(
offset, Collections.unmodifiableList(new ArrayList<>(bucketCounts)), totalCount);
}

public abstract int getOffset();

public abstract List<Long> getBucketCounts();

public abstract long getTotalCount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.data;

import com.google.auto.value.AutoValue;
import java.util.Collection;
import javax.annotation.concurrent.Immutable;

/**
* An exponential histogram metric point, as defined by the OpenTelemetry Exponential Histogram.
*
* <p>See:
* https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/datamodel.md#exponentialhistogram
*
* <p><i>Note: This is called "DoubleExponentialHistogramData" to reflect which primitives are used
* to record it, however "ExponentialHistogram" is the equivalent OTLP type.</i>
*/
@Immutable
@AutoValue
public abstract class DoubleExponentialHistogramData
implements Data<DoubleExponentialHistogramPointData> {
DoubleExponentialHistogramData() {}

public static DoubleExponentialHistogramData create(
AggregationTemporality temporality, Collection<DoubleExponentialHistogramPointData> points) {
return new AutoValue_DoubleExponentialHistogramData(temporality, points);
}

/**
* Returns the {@code AggregationTemporality} of this metric.
*
* <p>AggregationTemporality describes if the aggregator reports delta changes since last report
* time, or cumulative changes since a fixed start time.
*
* @return the {@code AggregationTemporality} of this metric
*/
public abstract AggregationTemporality getAggregationTemporality();

@Override
public abstract Collection<DoubleExponentialHistogramPointData> getPoints();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.data;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.Attributes;
import java.util.List;
import javax.annotation.concurrent.Immutable;

/**
* DoubleExponentialHistogramPointData represents an approximate distribution of measurements across
* exponentially increasing bucket boundaries, taken for a {@link DoubleExponentialHistogramData}.
* It also contains the necessary information to calculate bucket boundaries and perform
* aggregation.
*
* <p>The bucket boundaries increase exponentially according to the scale and the offset. See:
* https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/datamodel.md#exponentialhistogram
*/
@Immutable
@AutoValue
public abstract class DoubleExponentialHistogramPointData implements PointData {

DoubleExponentialHistogramPointData() {}

/**
* Creates a DoubleExponentialHistogramPointData.
*
* @param scale Scale is used to calculate bucket boundaries as outlined in the OTLP.
* @param sum The sum of all measurements in the histogram.
* @param zeroCount Number of values that are zero.
* @param positiveBuckets Buckets with positive values.
* @param negativeBuckets Buckets with negative values.
* @return a DoubleExponentialHistogramPointData
*/
public static DoubleExponentialHistogramPointData create(
long startEpochNanos,
long epochNanos,
Attributes attributes,
int scale,
double sum,
long zeroCount,
DoubleExponentialHistogramBuckets positiveBuckets,
DoubleExponentialHistogramBuckets negativeBuckets,
List<Exemplar> exemplars) {

long count = zeroCount + positiveBuckets.getTotalCount() + negativeBuckets.getTotalCount();
double base = Math.pow(2, Math.pow(2, -scale));

return new AutoValue_DoubleExponentialHistogramPointData(
startEpochNanos,
epochNanos,
attributes,
scale,
sum,
count,
base,
zeroCount,
positiveBuckets,
negativeBuckets,
exemplars);
}

@Override
public abstract long getStartEpochNanos();

@Override
public abstract long getEpochNanos();

@Override
public abstract Attributes getAttributes();

public abstract int getScale();

public abstract double getSum();

public abstract long getCount();

public abstract double getBase();

public abstract long getZeroCount();

public abstract DoubleExponentialHistogramBuckets getPositiveBuckets();

public abstract DoubleExponentialHistogramBuckets getNegativeBuckets();

@Override
public abstract List<Exemplar> getExemplars();
}

0 comments on commit 230f116

Please sign in to comment.