-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from typelevel/runtime-metrics-jvm
Implement JVM RuntimeMetrics
- Loading branch information
Showing
11 changed files
with
822 additions
and
6 deletions.
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
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
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
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
55 changes: 55 additions & 0 deletions
55
...metrics/jvm/src/main/scala/org/typelevel/otel4s/experimental/metrics/RuntimeMetrics.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,55 @@ | ||
/* | ||
* Copyright 2024 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.experimental.metrics | ||
|
||
import cats.effect.Async | ||
import cats.effect.Resource | ||
import cats.effect.std.Console | ||
import org.typelevel.otel4s.experimental.metrics.jvm._ | ||
import org.typelevel.otel4s.metrics.Meter | ||
|
||
object RuntimeMetrics { | ||
|
||
/** Registers the JVM runtime metrics: | ||
* - Class | ||
* - `jvm.class.count` | ||
* - `jvm.class.loaded` | ||
* - `jvm.class.unloaded` | ||
* - CPU | ||
* - `jvm.cpu.count` | ||
* - `jvm.cpu.recent_utilization` | ||
* - `jvm.cpu.time` | ||
* - GC | ||
* - `jvm.gc.duration` | ||
* - Memory | ||
* - `jvm.memory.committed` | ||
* - `jvm.memory.limit` | ||
* - `jvm.memory.used` | ||
* - `jvm.memory.used_after_last_gc` | ||
* - Thread | ||
* - `jvm.thread.count` | ||
*/ | ||
def register[F[_]: Async: Meter: Console]: Resource[F, Unit] = | ||
for { | ||
_ <- ClassMetrics.register | ||
_ <- CpuMetrics.register | ||
_ <- GarbageCollectorMetrics.register | ||
_ <- MemoryPoolMetrics.register | ||
_ <- ThreadMetrics.register | ||
} yield () | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...trics/jvm/src/main/scala/org/typelevel/otel4s/experimental/metrics/jvm/ClassMetrics.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,52 @@ | ||
/* | ||
* Copyright 2024 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.experimental.metrics.jvm | ||
|
||
import cats.effect.Resource | ||
import cats.effect.Sync | ||
import cats.effect.syntax.resource._ | ||
import cats.syntax.flatMap._ | ||
import cats.syntax.functor._ | ||
import org.typelevel.otel4s.metrics.Meter | ||
import org.typelevel.otel4s.semconv.metrics.JvmMetrics | ||
|
||
import java.lang.management.ManagementFactory | ||
|
||
/** @see | ||
* [[https://opentelemetry.io/docs/specs/semconv/runtime/jvm-metrics/#jvm-classes]] | ||
*/ | ||
object ClassMetrics { | ||
|
||
def register[F[_]: Sync: Meter]: Resource[F, Unit] = | ||
Sync[F].delay(ManagementFactory.getClassLoadingMXBean).toResource.flatMap { bean => | ||
Meter[F].batchCallback.of( | ||
JvmMetrics.ClassCount.createObserver[F, Long], | ||
JvmMetrics.ClassLoaded.createObserver[F, Long], | ||
JvmMetrics.ClassUnloaded.createObserver[F, Long] | ||
) { (classCount, classLoaded, classUnloaded) => | ||
for { | ||
count <- Sync[F].delay(bean.getTotalLoadedClassCount) | ||
loaded <- Sync[F].delay(bean.getLoadedClassCount) | ||
unloaded <- Sync[F].delay(bean.getUnloadedClassCount) | ||
_ <- classCount.record(count) | ||
_ <- classLoaded.record(loaded.toLong) | ||
_ <- classUnloaded.record(unloaded) | ||
} yield () | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.