Skip to content

Commit 217bb34

Browse files
committed
Update otel4s
1 parent 8502140 commit 217bb34

File tree

3 files changed

+16
-57
lines changed

3 files changed

+16
-57
lines changed

build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ThisBuild / tlSiteApiUrl := Some(url("https://www.javadoc.io/doc/org.typelevel/k
1313
lazy val root = tlCrossRootProject.aggregate(core)
1414

1515
lazy val core = crossProject(JVMPlatform, JSPlatform)
16-
.crossType(CrossType.Full)
16+
.crossType(CrossType.Pure)
1717
.in(file("core"))
1818
.settings(commonSettings)
1919
.settings(
@@ -59,7 +59,7 @@ lazy val docs = project
5959
val catsV = "2.7.0"
6060
val catsEffectV = "3.3.12"
6161

62-
val otel4sV = "0.0-d3796fb-20220613T071000Z-SNAPSHOT"
62+
val otel4sV = "0.0-7f89139-20220613T071957Z-SNAPSHOT"
6363

6464
val munitV = "0.7.29"
6565
val munitCatsEffectV = "1.0.7"

core/src/main/scala/org/typelevel/keypool/KeyPool.scala

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package org.typelevel.keypool
2323

24+
import java.util.concurrent.TimeUnit
2425
import cats._
2526
import cats.effect.kernel._
2627
import cats.effect.kernel.syntax.spawn._
@@ -202,7 +203,7 @@ object KeyPool {
202203
val (m_, toDestroy) = findStale(now, idleCount, m)
203204
(
204205
m_,
205-
toDestroy.traverse_(r => metrics.decIdle >> r._2._2).attempt.flatMap {
206+
toDestroy.traverse_(r => metrics.idle.dec() >> r._2._2).attempt.flatMap {
206207
case Left(t) => onReaperException(t)
207208
// .handleErrorWith(t => F.delay(t.printStackTrace())) // CHEATING?
208209
case Right(()) => F.unit
@@ -259,7 +260,7 @@ object KeyPool {
259260
}
260261
}
261262

262-
def decIdle = kp.kpMetrics.decIdle.whenA(isFromPool)
263+
def decIdle = kp.kpMetrics.idle.dec().whenA(isFromPool)
263264

264265
def go(now: FiniteDuration, pc: PoolMap[A, (B, F[Unit])]): (PoolMap[A, (B, F[Unit])], F[Unit]) =
265266
pc match {
@@ -271,12 +272,12 @@ object KeyPool {
271272
case None =>
272273
val cnt_ = idleCount + 1
273274
val m_ = PoolMap.open(cnt_, m + (k -> One((r, destroy), now)))
274-
(m_, kp.kpMetrics.incIdle)
275+
(m_, kp.kpMetrics.idle.inc())
275276
case Some(l) =>
276277
val (l_, mx) = addToList(now, kp.kpMaxPerKey(k), (r, destroy), l)
277278
val cnt_ = idleCount + mx.fold(1)(_ => 0)
278279
val m_ = PoolMap.open(cnt_, m + (k -> l_))
279-
(m_, mx.fold(kp.kpMetrics.incIdle)(_ => decIdle >> destroy))
280+
(m_, mx.fold(kp.kpMetrics.idle.inc())(_ => decIdle >> destroy))
280281
}
281282
}
282283

@@ -302,13 +303,16 @@ object KeyPool {
302303
}
303304
}
304305

306+
def allocateNew: F[(B, F[Unit])] =
307+
kp.kpMetrics.acquireDuration
308+
.recordDuration(TimeUnit.MILLISECONDS)
309+
.use(_ => kp.kpRes(k).allocated)
310+
305311
for {
306312
_ <- kp.kpMaxTotalSem.permit
307313
optR <- Resource.eval(kp.kpVar.modify(go))
308314
releasedState <- Resource.eval(Ref[F].of[Reusable](kp.kpDefaultReuseState))
309-
resource <- Resource.make(
310-
optR.fold(kp.kpMetrics.recordAcquireDuration(kp.kpRes(k)))(r => Applicative[F].pure(r))
311-
) { resource =>
315+
resource <- Resource.make(optR.fold(allocateNew)(r => Applicative[F].pure(r))) { resource =>
312316
for {
313317
reusable <- releasedState.get
314318
out <- reusable match {
@@ -317,9 +321,9 @@ object KeyPool {
317321
}
318322
} yield out
319323
}
320-
_ <- Resource.eval(kp.kpMetrics.acquiredTotal.add(1).whenA(optR.isEmpty))
321-
_ <- Resource.make(kp.kpMetrics.incInUse)(_ => kp.kpMetrics.decInUse)
322-
_ <- kp.kpMetrics.recordInUseDuration
324+
_ <- Resource.eval(kp.kpMetrics.acquiredTotal.inc().whenA(optR.isEmpty))
325+
_ <- Resource.make(kp.kpMetrics.inUse.inc())(_ => kp.kpMetrics.inUse.dec())
326+
_ <- kp.kpMetrics.inUseDuration.recordDuration(TimeUnit.MILLISECONDS)
323327
} yield new Managed(resource._1, optR.isDefined, releasedState)
324328
}
325329

core/src/main/scala/org/typelevel/keypool/internal/Metrics.scala

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.typelevel.keypool.internal
22

33
import cats.Monad
4-
import cats.effect.{Resource, Temporal}
54
import cats.syntax.functor._
65
import cats.syntax.flatMap._
76
import org.typelevel.otel4s._
@@ -29,25 +28,10 @@ private[keypool] trait Metrics[F[_]] {
2928

3029
def acquireDuration: Histogram[F, Double]
3130

32-
private[keypool] final def incIdle: F[Unit] = idle.add(1L)
33-
private[keypool] final def decIdle: F[Unit] = idle.add(-1L)
34-
private[keypool] final def incInUse: F[Unit] = inUse.add(1L)
35-
private[keypool] final def decInUse: F[Unit] = inUse.add(-1L)
36-
37-
private[keypool] final def recordInUseDuration(implicit F: Temporal[F]): Resource[F, Unit] =
38-
Metrics.recordInUseDuration(this)
39-
40-
private[keypool] final def recordAcquireDuration[A](
41-
resource: Resource[F, A]
42-
)(implicit F: Temporal[F]): F[(A, F[Unit])] =
43-
Metrics.recordAcquireDuration(this, resource)
44-
4531
}
4632

4733
private[keypool] object Metrics {
4834

49-
private val CauseKey: AttributeKey[String] = AttributeKey.string("cause")
50-
5135
def fromMeterProvider[F[_]: Monad](meterProvider: MeterProvider[F]): F[Metrics[F]] = {
5236
for {
5337
meter <- meterProvider.get("org.typelevel.keypool")
@@ -88,33 +72,4 @@ private[keypool] object Metrics {
8872
}
8973
}
9074

91-
private def recordInUseDuration[F[_]](
92-
metrics: Metrics[F]
93-
)(implicit F: Temporal[F]): Resource[F, Unit] =
94-
Resource
95-
.makeCase(Temporal[F].monotonic) { case (start, ec) =>
96-
for {
97-
end <- Temporal[F].monotonic
98-
_ <- metrics.inUseDuration.record((end - start).toMillis, causeAttributes(ec): _*)
99-
} yield ()
100-
}
101-
.void
102-
103-
private def recordAcquireDuration[F[_], A](
104-
metrics: Metrics[F],
105-
resource: Resource[F, A]
106-
)(implicit F: Temporal[F]): F[(A, F[Unit])] =
107-
Temporal[F]
108-
.timed(resource.allocated)
109-
.flatMap { case (acquireTime, r) =>
110-
metrics.acquireDuration.record(acquireTime.toMillis).as(r)
111-
}
112-
113-
private def causeAttributes(ec: Resource.ExitCase): List[Attribute[String]] =
114-
ec match {
115-
case Resource.ExitCase.Succeeded => Nil
116-
case Resource.ExitCase.Errored(e) => List(Attribute(CauseKey, e.getClass.getName))
117-
case Resource.ExitCase.Canceled => List(Attribute(CauseKey, "canceled"))
118-
}
119-
12075
}

0 commit comments

Comments
 (0)