From b34574a2c26426f5bb5f49750473eb92ad2344d8 Mon Sep 17 00:00:00 2001 From: Maksym Ochenashko Date: Wed, 24 Jul 2024 13:57:59 +0300 Subject: [PATCH] core-metrics: add varargs apply to the BucketBoundaries --- .../otel4s/metrics/BucketBoundaries.scala | 14 ++++++++++++++ .../typelevel/otel4s/metrics/BaseMeterSuite.scala | 8 +++----- .../otel4s/metrics/BucketBoundariesSuite.scala | 8 ++++---- .../typelevel/otel4s/sdk/metrics/Aggregation.scala | 6 ++---- .../metrics/exemplar/ExemplarReservoirSuite.scala | 2 +- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/core/metrics/src/main/scala/org/typelevel/otel4s/metrics/BucketBoundaries.scala b/core/metrics/src/main/scala/org/typelevel/otel4s/metrics/BucketBoundaries.scala index 41576849b..285375dfe 100644 --- a/core/metrics/src/main/scala/org/typelevel/otel4s/metrics/BucketBoundaries.scala +++ b/core/metrics/src/main/scala/org/typelevel/otel4s/metrics/BucketBoundaries.scala @@ -75,6 +75,20 @@ object BucketBoundaries { Impl(boundaries) } + /** Creates [[BucketBoundaries]] using the given `boundaries`. + * + * Throws an exception if any of the following rules is violated: + * - the boundary cannot be `Double.NaN` + * - the boundaries must be in increasing order + * - first boundary cannot be `Double.NegativeInfinity` + * - last boundary cannot be `Double.PositiveInfinity` + * + * @param boundaries + * the bucket boundaries + */ + def apply(boundaries: Double*): BucketBoundaries = + apply(boundaries.toVector) + implicit val bucketBoundariesHash: Hash[BucketBoundaries] = Hash.by(_.boundaries) diff --git a/core/metrics/src/test/scala/org/typelevel/otel4s/metrics/BaseMeterSuite.scala b/core/metrics/src/test/scala/org/typelevel/otel4s/metrics/BaseMeterSuite.scala index 97b03da69..3f60928a8 100644 --- a/core/metrics/src/test/scala/org/typelevel/otel4s/metrics/BaseMeterSuite.scala +++ b/core/metrics/src/test/scala/org/typelevel/otel4s/metrics/BaseMeterSuite.scala @@ -154,7 +154,7 @@ abstract class BaseMeterSuite extends CatsEffectSuite { } sdkTest("Histogram - use explicit bucket boundaries") { sdk => - val boundaries = BucketBoundaries(Vector(1.0, 2.0, 3.0)) + val boundaries = BucketBoundaries(1.0, 2.0, 3.0) val expected = MetricData.histogram( name = "histogram", values = List(1.0), @@ -348,10 +348,8 @@ object BaseMeterSuite { object MetricData { private val DefaultBoundaries = BucketBoundaries( - Vector( - 0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 750.0, 1000.0, - 2500.0, 5000.0, 7500.0, 10000.0 - ) + 0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 750.0, 1000.0, + 2500.0, 5000.0, 7500.0, 10000.0 ) def sum( diff --git a/core/metrics/src/test/scala/org/typelevel/otel4s/metrics/BucketBoundariesSuite.scala b/core/metrics/src/test/scala/org/typelevel/otel4s/metrics/BucketBoundariesSuite.scala index c0114785e..478fb3bdf 100644 --- a/core/metrics/src/test/scala/org/typelevel/otel4s/metrics/BucketBoundariesSuite.scala +++ b/core/metrics/src/test/scala/org/typelevel/otel4s/metrics/BucketBoundariesSuite.scala @@ -51,7 +51,7 @@ class BucketBoundariesSuite extends DisciplineSuite { interceptMessage[IllegalArgumentException]( "requirement failed: bucket boundary cannot be NaN" )( - BucketBoundaries(Vector(1.0, 2.0, Double.NaN, 3.0)) + BucketBoundaries(1.0, 2.0, Double.NaN, 3.0) ) } @@ -59,7 +59,7 @@ class BucketBoundariesSuite extends DisciplineSuite { interceptMessage[IllegalArgumentException]( "requirement failed: bucket boundaries must be in increasing oder" )( - BucketBoundaries(Vector(1.0, 1.2, 0.5, 3.0)) + BucketBoundaries(1.0, 1.2, 0.5, 3.0) ) } @@ -67,7 +67,7 @@ class BucketBoundariesSuite extends DisciplineSuite { interceptMessage[IllegalArgumentException]( "requirement failed: first boundary cannot be -Inf" )( - BucketBoundaries(Vector(Double.NegativeInfinity)) + BucketBoundaries(Double.NegativeInfinity) ) } @@ -75,7 +75,7 @@ class BucketBoundariesSuite extends DisciplineSuite { interceptMessage[IllegalArgumentException]( "requirement failed: last boundary cannot be +Inf" )( - BucketBoundaries(Vector(Double.PositiveInfinity)) + BucketBoundaries(Double.PositiveInfinity) ) } diff --git a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/Aggregation.scala b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/Aggregation.scala index b611eaf3a..dbb25eded 100644 --- a/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/Aggregation.scala +++ b/sdk/metrics/src/main/scala/org/typelevel/otel4s/sdk/metrics/Aggregation.scala @@ -44,10 +44,8 @@ object Aggregation { private[metrics] object Defaults { // See https://opentelemetry.io/docs/specs/otel/metrics/sdk/#explicit-bucket-histogram-aggregation val Boundaries: BucketBoundaries = BucketBoundaries( - Vector( - 0d, 5d, 10d, 25d, 50d, 75d, 100d, 250d, 500d, 750d, 1000d, 2500d, 5000d, - 7500d, 10000d - ) + 0d, 5d, 10d, 25d, 50d, 75d, 100d, 250d, 500d, 750d, 1000d, 2500d, 5000d, + 7500d, 10000d ) } diff --git a/sdk/metrics/src/test/scala/org/typelevel/otel4s/sdk/metrics/exemplar/ExemplarReservoirSuite.scala b/sdk/metrics/src/test/scala/org/typelevel/otel4s/sdk/metrics/exemplar/ExemplarReservoirSuite.scala index 0f77f2f1a..6e0a33e02 100644 --- a/sdk/metrics/src/test/scala/org/typelevel/otel4s/sdk/metrics/exemplar/ExemplarReservoirSuite.scala +++ b/sdk/metrics/src/test/scala/org/typelevel/otel4s/sdk/metrics/exemplar/ExemplarReservoirSuite.scala @@ -44,7 +44,7 @@ class ExemplarReservoirSuite _.get(traceContextKey) private val boundaries: BucketBoundaries = - BucketBoundaries(Vector(1.0, 5.0, 10.0)) + BucketBoundaries(1.0, 5.0, 10.0) // // Fixed size