-
Notifications
You must be signed in to change notification settings - Fork 36
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
Draft integration with IORuntimeMetrics #173
Draft
iRevive
wants to merge
4
commits into
typelevel:main
Choose a base branch
from
iRevive:ce-runtime-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
core/metrics/src/main/scala/org/typelevel/otel4s/metrics/preset/IOMetrics.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.typelevel.otel4s.metrics | ||
package preset | ||
|
||
import cats.effect.{Resource, Sync} | ||
import cats.effect.unsafe._ | ||
import cats.syntax.apply._ | ||
import cats.syntax.functor._ | ||
|
||
object IOMetrics { | ||
|
||
def fromRuntimeMetrics[F[_]: Sync: Meter]( | ||
metrics: IORuntimeMetrics | ||
): Resource[F, Unit] = { | ||
val meter = implicitly[Meter[F]] | ||
|
||
val starvation = metrics.cpuStarvation.map { cpuStarvation => | ||
val starvationCount = meter | ||
.observableUpDownCounter("cpu-starvation-count") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(cpuStarvation.starvationCount())) | ||
) | ||
|
||
val maxDrift = meter | ||
.observableUpDownCounter("max-clock-drift-ms") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(cpuStarvation.maxClockDriftMs())) | ||
) | ||
|
||
val currentDrift = meter | ||
.observableUpDownCounter("current-clock-drift-ms") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(cpuStarvation.currentClockDriftMs())) | ||
) | ||
|
||
(starvationCount, maxDrift, currentDrift).tupled.void | ||
} | ||
|
||
val compute = metrics.compute.map { compute => | ||
val wtc = meter | ||
.observableUpDownCounter("worker-thread-count") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(compute.workerThreadCount().toLong)) | ||
) | ||
|
||
val atc = meter | ||
.observableUpDownCounter("active-thread-count") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(compute.activeThreadCount().toLong)) | ||
) | ||
|
||
val stc = meter | ||
.observableUpDownCounter("searching-thread-count") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(compute.searchingThreadCount().toLong)) | ||
) | ||
|
||
val bwtc = meter | ||
.observableUpDownCounter("blocker-worker-thread-count") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(compute.blockedWorkerThreadCount().toLong)) | ||
) | ||
|
||
val lqfc = meter | ||
.observableUpDownCounter("local-queue-fiber-count") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(compute.localQueueFiberCount())) | ||
) | ||
|
||
val sfc = meter | ||
.observableUpDownCounter("suspended-fiber-count") | ||
.createWithCallback(cb => | ||
Sync[F].defer(cb.record(compute.suspendedFiberCount())) | ||
) | ||
|
||
(wtc, atc, stc, bwtc, lqfc, sfc).tupled.void | ||
} | ||
|
||
for { | ||
_ <- starvation.getOrElse(Resource.unit[F]) | ||
_ <- compute.getOrElse(Resource.unit[F]) | ||
} yield () | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import cats.effect.std.Random | ||
import cats.effect.{IO, IOApp, Resource} | ||
import cats.syntax.foldable._ | ||
import io.opentelemetry.sdk.OpenTelemetrySdk | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider | ||
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader | ||
import org.typelevel.otel4s.java.OtelJava | ||
import org.typelevel.otel4s.metrics.preset.IOMetrics | ||
|
||
import scala.concurrent.duration._ | ||
import scala.jdk.CollectionConverters._ | ||
|
||
object RuntimeMetricsExample extends IOApp.Simple { | ||
def run: IO[Unit] = { | ||
val jMetricReader = InMemoryMetricReader.create() | ||
val jSdk = { | ||
val meterProvider = SdkMeterProvider | ||
.builder() | ||
.registerMetricReader(jMetricReader) | ||
.build() | ||
|
||
val sdk = OpenTelemetrySdk | ||
.builder() | ||
.setMeterProvider(meterProvider) | ||
.build() | ||
|
||
sdk | ||
} | ||
|
||
def printMetrics: IO[Unit] = | ||
for { | ||
_ <- IO.println("New cycle: ") | ||
metrics <- IO.delay(jMetricReader.collectAllMetrics().asScala.toList) | ||
_ <- metrics.traverse_(v => IO.println(v.getName + " = " + v.getData)) | ||
} yield () | ||
|
||
Resource | ||
.eval(OtelJava.forAsync[IO](jSdk)) | ||
.evalMap(_.meterProvider.get("cats-effect-runtime-metrics")) | ||
.use { implicit meter => | ||
IOMetrics.fromRuntimeMetrics[IO](runtime.metrics).surround { | ||
printMetrics.delayBy(500.millis).foreverM.background.surround { | ||
compute | ||
} | ||
} | ||
} | ||
} | ||
|
||
private def compute: IO[Unit] = | ||
Random.scalaUtilRandom[IO].flatMap { random => | ||
val io = random.betweenLong(10, 3000).flatMap { delay => | ||
if (delay % 2 == 0) IO.blocking(Thread.sleep(delay)) | ||
else IO.delay(Thread.sleep(delay)) | ||
} | ||
|
||
IO.parReplicateAN(30)(100, io).void | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Runtime metrics are living in the
cats-effect
module.