|
1 | 1 | package com.evolutiongaming.prometheus
|
2 | 2 |
|
3 |
| -import io.prometheus.client.Collector |
| 3 | +import io.prometheus.client.{Collector, SimpleTimer} |
4 | 4 |
|
| 5 | +import scala.annotation.{nowarn, unused} |
5 | 6 | import scala.concurrent.{ExecutionContext, Future}
|
6 |
| - |
7 |
| -trait ObserveDuration[F] { |
8 |
| - |
| 7 | +import scala.math.Numeric.Implicits.* |
| 8 | +import scala.util.Try |
| 9 | + |
| 10 | +/** Time duration measurement syntax for [[HasObserve]]-kind metrics from the prometheus client, i.e., Summary, Histogram. |
| 11 | + * |
| 12 | + * Here time is always reported in seconds, which means your prometheus metric name should end in `_seconds`. |
| 13 | + * |
| 14 | + * The class is not supposed to be used directly but as an implicit syntax provided by [[PrometheusHelper]]. |
| 15 | + */ |
| 16 | +sealed trait ObserveDuration[F] { |
| 17 | + |
| 18 | + /** Measures evaluation time of a block in seconds with nano-time precision |
| 19 | + */ |
9 | 20 | def timeFunc[T](f: => T): T
|
10 |
| - def timeFuncNanos[T](f: => T): T |
11 | 21 |
|
| 22 | + /** Measures evaluation time of a block in seconds with nano-time precision |
| 23 | + * |
| 24 | + * @deprecated |
| 25 | + * since 1.1.0 timeFunc has been changed to use nano-time precision, this method is obsolete and will be removed |
| 26 | + */ |
| 27 | + @deprecated( |
| 28 | + message = "use timeFunc instead - it has nano-time precision now", |
| 29 | + since = "1.1.0" |
| 30 | + ) |
| 31 | + def timeFuncNanos[T](f: => T): T = timeFunc(f) |
| 32 | + |
| 33 | + /** Measures evaluation time of an asynchronous block in seconds with nano-time precision |
| 34 | + */ |
12 | 35 | def timeFuture[T](f: => Future[T]): Future[T]
|
13 |
| - def timeFutureNanos[T](f: => Future[T]): Future[T] |
14 | 36 |
|
| 37 | + /** Measures evaluation time of an asynchronous block in seconds with nano-time precision |
| 38 | + * |
| 39 | + * @deprecated |
| 40 | + * since 1.1.0 timeFuture has been changed to use nano-time precision, this method is obsolete and will be removed |
| 41 | + */ |
| 42 | + @deprecated( |
| 43 | + message = "use timeFuture instead - it has nano-time precision now", |
| 44 | + since = "1.1.0" |
| 45 | + ) |
| 46 | + def timeFutureNanos[T](f: => Future[T]): Future[T] = timeFuture(f) |
| 47 | + |
| 48 | + /** Measures in seconds the time spent since the provided start time obtained using [[ClockPlatform.nowMillis]] |
| 49 | + * |
| 50 | + * @param start |
| 51 | + * start time from a millisecond-precision clock |
| 52 | + * @deprecated |
| 53 | + * since 1.1.0, use timeTillNowMillis(: Long) with a primitive arg type and explicit precision name suffix |
| 54 | + */ |
| 55 | + @deprecated( |
| 56 | + message = "use timeTillNowMillis(: Long) with a primitive arg type and explicit precision name suffix", |
| 57 | + since = "1.1.0" |
| 58 | + ) |
15 | 59 | def timeTillNow[T](start: T)(implicit numeric: Numeric[T]): Unit
|
| 60 | + |
| 61 | + /** Measures in seconds the time spent since the provided start time obtained using [[ClockPlatform.nowNano]] |
| 62 | + * |
| 63 | + * @param start |
| 64 | + * start time from a nanosecond-precision clock |
| 65 | + * @deprecated |
| 66 | + * since 1.1.0, use timeTillNowNanos(: Long) with a primitive arg type |
| 67 | + */ |
| 68 | + @deprecated( |
| 69 | + message = "use timeTillNowNanos(: Long) with a primitive arg type", |
| 70 | + since = "1.1.0" |
| 71 | + ) |
16 | 72 | def timeTillNowNanos[T](start: T)(implicit numeric: Numeric[T]): Unit
|
17 |
| -} |
18 | 73 |
|
19 |
| -object ObserveDuration { |
20 |
| - def apply[F](implicit observeDuration: ObserveDuration[F]): ObserveDuration[F] = observeDuration |
| 74 | + /** Measures in seconds the time spent since the provided start time obtained using [[ClockPlatform.nowMillis]] |
| 75 | + */ |
| 76 | + def timeTillNowMillis(startMs: Long): Unit = { |
| 77 | + // default impl for a new method of a trait - added for keeping binary compatibility |
| 78 | + // TODO: bincompat leftover, remove in 2.x |
21 | 79 |
|
22 |
| - def fromHasObserver[F](observer: F)(implicit hasObserve: HasObserve[F], clock: ClockPlatform, ec: ExecutionContext): ObserveDuration[F] = |
23 |
| - new ObserveDuration[F] { |
| 80 | + timeTillNow[Long](startMs): @nowarn("cat=deprecation") |
| 81 | + } |
24 | 82 |
|
25 |
| - override def timeFunc[T](f: => T): T = |
26 |
| - measureFunction(f, clock.nowMillis, timeTillNow[Long]) |
| 83 | + /** Measures in seconds the time spent since the provided start time obtained using [[ClockPlatform.nowNano]] |
| 84 | + */ |
| 85 | + def timeTillNowNanos(startNs: Long): Unit = { |
| 86 | + // default impl for a new method of a trait - added for keeping binary compatibility |
| 87 | + // TODO: bincompat leftover, remove in 2.x |
27 | 88 |
|
28 |
| - override def timeFuncNanos[T](f: => T): T = |
29 |
| - measureFunction(f, clock.nowNano, timeTillNowNanos[Long]) |
| 89 | + timeTillNowNanos[Long](startNs): @nowarn("cat=deprecation") |
| 90 | + } |
| 91 | +} |
30 | 92 |
|
31 |
| - private def measureFunction[A](f: => A, start: Long, measurer: Long => Unit): A = |
32 |
| - try f |
33 |
| - finally measurer(start) |
| 93 | +object ObserveDuration { |
| 94 | + def apply[F](implicit observeDuration: ObserveDuration[F]): ObserveDuration[F] = observeDuration |
34 | 95 |
|
35 |
| - override def timeFuture[T](f: => Future[T]): Future[T] = |
36 |
| - measureFuture(f, clock.nowMillis, timeTillNow[Long]) |
| 96 | + // TODO: bincompat leftover, remove in 2.x |
| 97 | + @deprecated( |
| 98 | + message = "use create", |
| 99 | + since = "1.1.0" |
| 100 | + ) |
| 101 | + def fromHasObserver[F]( |
| 102 | + observer: F |
| 103 | + )(implicit |
| 104 | + hasObserve: HasObserve[F], |
| 105 | + clock: ClockPlatform, |
| 106 | + @unused ec: ExecutionContext |
| 107 | + ): ObserveDuration[F] = new ObserveDurationImpl[F](observer) |
| 108 | + |
| 109 | + /** Creates [[ObserveDuration]] implementation instance |
| 110 | + * |
| 111 | + * @param observer |
| 112 | + * metric instance which has [[HasObserve]] |
| 113 | + * @param hasObserve |
| 114 | + * [[HasObserve]] instance for the metric |
| 115 | + * @param clock |
| 116 | + * [[ClockPlatform]] to use for time measurement |
| 117 | + * @tparam F |
| 118 | + * metric type |
| 119 | + */ |
| 120 | + def create[F]( |
| 121 | + observer: F |
| 122 | + )(implicit |
| 123 | + hasObserve: HasObserve[F], |
| 124 | + clock: ClockPlatform |
| 125 | + ): ObserveDuration[F] = new ObserveDurationImpl[F](observer) |
| 126 | + |
| 127 | + private final class ObserveDurationImpl[F]( |
| 128 | + observer: F |
| 129 | + )(implicit |
| 130 | + hasObserve: HasObserve[F], |
| 131 | + clock: ClockPlatform |
| 132 | + ) extends ObserveDuration[F] { |
| 133 | + |
| 134 | + override def timeFunc[T](f: => T): T = { |
| 135 | + val startNs = clock.nowNano |
| 136 | + try { |
| 137 | + f |
| 138 | + } finally { |
| 139 | + timeTillNowNanos(startNs) |
| 140 | + } |
| 141 | + } |
37 | 142 |
|
38 |
| - override def timeFutureNanos[T](f: => Future[T]): Future[T] = |
39 |
| - measureFuture(f, clock.nowNano, timeTillNowNanos[Long]) |
| 143 | + override def timeFuture[T](f: => Future[T]): Future[T] = { |
| 144 | + val startNs = clock.nowNano |
| 145 | + Future |
| 146 | + .fromTry(Try { |
| 147 | + f |
| 148 | + }) |
| 149 | + .flatten |
| 150 | + .andThen { case _ => |
| 151 | + timeTillNowNanos(startNs) |
| 152 | + }(ExecutionContext.parasitic) |
| 153 | + } |
40 | 154 |
|
41 |
| - private def measureFuture[A]( |
42 |
| - f: => Future[A], |
43 |
| - start: Long, |
44 |
| - measurer: Long => Unit |
45 |
| - )(implicit ec: ExecutionContext): Future[A] = f andThen { case _ => measurer(start) } |
| 155 | + override def timeTillNow[T]( |
| 156 | + start: T |
| 157 | + )(implicit numeric: Numeric[T]): Unit = { |
| 158 | + val value = duration(start, clock.nowMillis) / Collector.MILLISECONDS_PER_SECOND |
| 159 | + hasObserve.observe(observer, value) |
| 160 | + } |
46 | 161 |
|
47 |
| - override def timeTillNow[T]( |
48 |
| - start: T |
49 |
| - )(implicit numeric: Numeric[T]): Unit = { |
50 |
| - val value = duration(start, clock.nowMillis) / Collector.MILLISECONDS_PER_SECOND |
51 |
| - hasObserve.observe(observer, value) |
52 |
| - } |
| 162 | + override def timeTillNowNanos[T](start: T)(implicit |
| 163 | + numeric: Numeric[T] |
| 164 | + ): Unit = { |
| 165 | + val value = duration(start, clock.nowNano) / Collector.NANOSECONDS_PER_SECOND |
| 166 | + hasObserve.observe(observer, value) |
| 167 | + } |
53 | 168 |
|
54 |
| - override def timeTillNowNanos[T](start: T)(implicit |
55 |
| - numeric: Numeric[T] |
56 |
| - ): Unit = { |
57 |
| - val value = duration(start, clock.nowNano) / Collector.NANOSECONDS_PER_SECOND |
58 |
| - hasObserve.observe(observer, value) |
59 |
| - } |
| 169 | + override def timeTillNowMillis(startMs: Long): Unit = { |
| 170 | + val endMs = clock.nowMillis |
| 171 | + val elapsedSeconds = (endMs - startMs).toDouble / Collector.MILLISECONDS_PER_SECOND |
| 172 | + hasObserve.observe(observer, elapsedSeconds) |
60 | 173 | }
|
61 | 174 |
|
62 |
| - private def duration[A](start: A, now: Long)(implicit a: Numeric[A]): Double = { |
63 |
| - now.toDouble - a.toDouble(start) |
| 175 | + override def timeTillNowNanos(startNs: Long): Unit = { |
| 176 | + val endNs = clock.nowNano |
| 177 | + hasObserve.observe(observer, SimpleTimer.elapsedSecondsFromNanos(startNs, endNs)) |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private def duration[A: Numeric](start: A, now: Long): Double = { |
| 182 | + now.toDouble - start.toDouble |
64 | 183 | }
|
65 | 184 | }
|
0 commit comments