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

Add a hasBucketBoundaries() variant that allows specifying precision #5457

Merged
Show file tree
Hide file tree
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
@@ -1,4 +1,7 @@
Comparing source compatibility of against
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.HistogramPointAssert (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.HistogramPointAssert hasBucketBoundaries(double[], org.assertj.core.data.Offset)
+++ NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.LogRecordDataAssert (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.LogRecordDataAssert hasAttributes(io.opentelemetry.api.common.Attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
package io.opentelemetry.sdk.testing.assertj;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;

import io.opentelemetry.sdk.metrics.data.DoubleExemplarData;
import io.opentelemetry.sdk.metrics.data.HistogramPointData;
import java.util.Arrays;
import java.util.function.Consumer;
import org.assertj.core.api.Assertions;
import org.assertj.core.data.Offset;

/**
* Test assertions for {@link HistogramPointData}.
Expand Down Expand Up @@ -68,9 +70,20 @@ public HistogramPointAssert hasCount(long expected) {
* @param boundaries The set of bucket boundaries in the same order as the expected collection.
*/
public HistogramPointAssert hasBucketBoundaries(double... boundaries) {
return hasBucketBoundaries(boundaries, within(0.000_001));
}

/**
* Asserts the {@code boundaries} field matches the expected value. The boundaries are may vary
* with a specified precision.
*
* @param boundaries The set of bucket boundaries in the same order as the expected collection.
* @param precision The precision under which boundaries may vary.
*/
public HistogramPointAssert hasBucketBoundaries(double[] boundaries, Offset<Double> precision) {
isNotNull();
Double[] bigBoundaries = Arrays.stream(boundaries).boxed().toArray(Double[]::new);
Assertions.assertThat(actual.getBoundaries()).as("boundaries").containsExactly(bigBoundaries);
double[] actualBoundaries = actual.getBoundaries().stream().mapToDouble(d -> d).toArray();
Assertions.assertThat(actualBoundaries).as("boundaries").containsExactly(boundaries, precision);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we change the behavior of hasBucketBoundaries(double... boundaries) to include some default reasonable precision.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
How about 0.000001?

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.offset;
import static org.assertj.core.api.Assertions.within;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -981,7 +982,8 @@ void histogram() {
.hasMax(7.0)
.hasMin(4.0)
.hasCount(3)
.hasBucketBoundaries(10.0)));
.hasBucketBoundaries(10.0)
.hasBucketBoundaries(new double[] {10.1}, within(0.1))));
assertThat(HISTOGRAM_METRIC_DELTA).hasHistogramSatisfying(histogram -> histogram.isDelta());
}

Expand Down Expand Up @@ -1044,6 +1046,15 @@ void histogram_failure() {
histogram.hasPointsSatisfying(
point -> point.hasBucketBoundaries(11.0))))
.isInstanceOf(AssertionError.class);
assertThatThrownBy(
() ->
assertThat(HISTOGRAM_METRIC)
.hasHistogramSatisfying(
histogram ->
histogram.hasPointsSatisfying(
point ->
point.hasBucketBoundaries(new double[] {11.0}, within(0.1)))))
.isInstanceOf(AssertionError.class);
}

@Test
Expand Down