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

Correctly Handle ChronoUnit In Metric.timer #7698

Merged
merged 1 commit into from
Jan 13, 2023
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
8 changes: 8 additions & 0 deletions core-tests/shared/src/test/scala/zio/metrics/MetricSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import zio.metrics.MetricKeyType.Histogram
import zio.test._
import zio.test.TestAspect._

import java.time.temporal.ChronoUnit

object MetricSpec extends ZIOBaseSpec {

private val labels1 = Set(MetricLabel("x", "a"), MetricLabel("y", "b"))
Expand Down Expand Up @@ -395,6 +397,12 @@ object MetricSpec extends ZIOBaseSpec {
_ <- counter.increment @@ tagged("key" -> "value")
state <- counter.tagged(MetricLabel("key", "value")).value
} yield assertTrue(state == MetricState.Counter(1L))
},
test("timer") {
val timer = Metric.timer("timer", ChronoUnit.MILLIS)
for {
_ <- ZIO.unit @@ timer.trackDuration
} yield assertCompletes
}
)
}
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/metrics/Metric.scala
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ object Metric {
val base = histogram(name, boundaries).tagged(MetricLabel("time_unit", chronoUnit.toString.toLowerCase()))

base.contramap[Duration] { (duration: Duration) =>
duration.get(chronoUnit).toDouble
duration.toNanos / chronoUnit.getDuration.toNanos
}
}
}