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

SDK metrics: native runtime metrics #670

Open
iRevive opened this issue May 5, 2024 · 3 comments
Open

SDK metrics: native runtime metrics #670

iRevive opened this issue May 5, 2024 · 3 comments
Labels
metrics Improvement to metrics module module:sdk Features and improvements to the sdk module

Comments

@iRevive
Copy link
Contributor

iRevive commented May 5, 2024

Runtime metrics:

  • memory usage
  • cpu usage
  • GC stats
  • classloader stats

JVM

We can follow the same path as OpenTelemetry Java does and retrieve metrics using MXBeans.

Scala.js

https://github.com/open-telemetry/opentelemetry-js-contrib/pull/2136/files

Scala Native

scala-native/scala-native#4018

CPU: linux, macos, windows.

@iRevive iRevive added metrics Improvement to metrics module module:sdk Features and improvements to the sdk module labels May 5, 2024
@iRevive
Copy link
Contributor Author

iRevive commented Aug 21, 2024

Scala Native CPU time:

def getProcessCpuTime(): Long = {
  import scala.scalanative.meta.LinktimeInfo
  import scala.scalanative.unsafe._
  
  if (LinktimeInfo.isWindows) {
    import scala.scalanative.windows.MinWinBaseApi.FileTimeStruct
    import scala.scalanative.windows.ProcessThreadsApi
    import scala.scalanative.windows.MinWinBaseApiOps._
    
    val creationTime = stackalloc[FileTimeStruct]()
    val exitTime = stackalloc[FileTimeStruct]()
    val kernelTime = stackalloc[FileTimeStruct]()
    val userTime = stackalloc[FileTimeStruct]()
    val success = ProcessThreadsApi.GetProcessTimes(
      ProcessThreadsApi.GetCurrentProcess(),
      creationTime,
      exitTime,
      kernelTime,
      userTime
    )
    if (success) {
      val totalTime = kernelTime.fileTime + userTime.fileTime
      totalTime.toLong * FileTimeOps.EpochInterval
    } else {
      -1L
    }
  } else {
    import scala.scalanative.posix.sys.resource._
    import scala.scalanative.posix.sys.resourceOps._
    import scala.scalanative.posix.sys.timeOps._
    
    val usage = stackalloc[rusage]()
    if (getrusage(RUSAGE_SELF, usage) == 0) {
      val micros =
        usage.ru_utime.tv_sec * 1000 * 1000 + usage.ru_utime.tv_usec +
          usage.ru_stime.tv_sec * 1000 * 1000 + usage.ru_stime.tv_usec
      micros.toLong * 1000
    } else {
      -1L
    }
  }
}

@iRevive
Copy link
Contributor Author

iRevive commented Sep 1, 2024

Scala Native 0.5.5(6) will provide access to MemoryMXBean.
But we need to wait for cats-effect to catch up.

@iRevive iRevive changed the title MetricsSDK: native runtime metrics SDK metrics: native runtime metrics Sep 2, 2024
@iRevive
Copy link
Contributor Author

iRevive commented Oct 27, 2024

JVM: typelevel/otel4s-experimental#9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metrics Improvement to metrics module module:sdk Features and improvements to the sdk module
Projects
None yet
Development

No branches or pull requests

1 participant