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

Bring back buffer pool metrics #900

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- run: git fetch --depth=1 origin '+refs/tags/*:refs/tags/*'
- uses: olafurpg/setup-scala@v2
- uses: olafurpg/setup-scala@v10
with:
java-version: adopt@1.8.0-242
- name: Test
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- run: git fetch --depth=1 origin '+refs/tags/*:refs/tags/*'
- uses: olafurpg/setup-scala@v2
- uses: olafurpg/setup-scala@v10
- name: Release
run: csbt release
shell: bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package kamon.instrumentation.system.jvm
import java.lang.management.ManagementFactory

import kamon.Kamon
import kamon.instrumentation.system.jvm.JvmMetrics.MemoryUsageInstruments.MemoryRegionInstruments
import kamon.instrumentation.system.jvm.JvmMetrics.MemoryUsageInstruments.{BufferPoolInstruments, MemoryRegionInstruments}
import kamon.instrumentation.system.jvm.JvmMetricsCollector.{Collector, MemoryPool}
import kamon.metric.{Gauge, Histogram, InstrumentGroup, MeasurementUnit}
import kamon.tag.TagSet
Expand Down Expand Up @@ -124,6 +124,23 @@ object JvmMetrics {
description = "Total number od classes currently loaded"
)

val BufferPoolCount = Kamon.gauge(
name = s"jvm.memory.buffer-pool.count",
description = "Estimated number of buffers in the pool"
)

val BufferPoolUsed = Kamon.gauge(
name = s"jvm.memory.buffer-pool.used",
description = "Estimate of memory used by the JVM for this buffer pool in bytes",
unit = MeasurementUnit.information.bytes
)

val BufferPoolCapacity = Kamon.gauge(
name = s"jvm.memory.buffer-pool.capacity",
description = "Estimate of the total capacity of this pool in bytes",
unit = MeasurementUnit.information.bytes
)

class GarbageCollectionInstruments(tags: TagSet) extends InstrumentGroup(tags) {
private val _collectorCache = mutable.Map.empty[String, Histogram]

Expand All @@ -144,6 +161,7 @@ object JvmMetrics {
class MemoryUsageInstruments(tags: TagSet) extends InstrumentGroup(tags) {
private val _memoryRegionsCache = mutable.Map.empty[String, MemoryRegionInstruments]
private val _memoryPoolsCache = mutable.Map.empty[String, MemoryRegionInstruments]
private val _memoryBuffersCache = mutable.Map.empty[String, BufferPoolInstruments]

def regionInstruments(regionName: String): MemoryRegionInstruments =
_memoryRegionsCache.getOrElseUpdate(regionName, {
Expand All @@ -168,6 +186,19 @@ object JvmMetrics {
register(MemoryPoolMax, region)
)
})

def bufferPoolInstruments(poolName: String): BufferPoolInstruments = {
_memoryBuffersCache.getOrElseUpdate(poolName, {
val bufferTags = tags
.withTag("pool", poolName)

BufferPoolInstruments(
register(BufferPoolCount, bufferTags),
register(BufferPoolUsed, bufferTags),
register(BufferPoolCapacity, bufferTags)
)
})
}
}

class ClassLoadingInstruments(tags: TagSet) extends InstrumentGroup(tags) {
Expand All @@ -183,12 +214,18 @@ object JvmMetrics {
}

object MemoryUsageInstruments {
case class MemoryRegionInstruments(
case class MemoryRegionInstruments (
used: Histogram,
free: Histogram,
committed: Gauge,
max: Gauge
)

case class BufferPoolInstruments (
count: Gauge,
used: Gauge,
capacity: Gauge
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package kamon.instrumentation.system.jvm

import java.lang.management.{ManagementFactory, MemoryUsage}
import java.lang.management.{BufferPoolMXBean, ManagementFactory, MemoryUsage}
import java.util.concurrent.TimeUnit

import com.sun.management.GarbageCollectionNotificationInfo
Expand All @@ -26,6 +26,7 @@ import javax.management.openmbean.CompositeData
import javax.management.{Notification, NotificationEmitter, NotificationListener}
import kamon.Kamon
import kamon.instrumentation.system.jvm.JvmMetrics.{ClassLoadingInstruments, GarbageCollectionInstruments, MemoryUsageInstruments, ThreadsInstruments}
import kamon.instrumentation.system.jvm.JvmMetricsCollector.MemoryPool.sanitize
import kamon.instrumentation.system.jvm.JvmMetricsCollector.{Collector, MemoryPool}
import kamon.module.{Module, ModuleFactory}
import kamon.tag.TagSet
Expand Down Expand Up @@ -164,6 +165,15 @@ class JvmMetricsCollector(ec: ExecutionContext) extends Module {
poolInstruments.max.update(memoryUsage.getMax)
poolInstruments.committed.update(memoryUsage.getCommitted)
})

ManagementFactory.getPlatformMXBeans(classOf[BufferPoolMXBean]).asScala.toList.map { bean =>
val bufferPoolInstruments = memoryUsageInstruments.bufferPoolInstruments(
MemoryPool.sanitize(bean.getName))

bufferPoolInstruments.count.update(bean.getCount)
bufferPoolInstruments.used.update(bean.getMemoryUsed)
bufferPoolInstruments.capacity.update(bean.getTotalCapacity)
}
}
}
}
Expand Down Expand Up @@ -229,9 +239,9 @@ object JvmMetricsCollector {
}

def find(poolName: String): MemoryPool =
_memoryRegionMappings.get(poolName).getOrElse {
MemoryPool(poolName, sanitize(poolName), if(poolName.endsWith("Eden Space")) Usage.Eden else Usage.Unknown)
}
_memoryRegionMappings.getOrElse(poolName,
MemoryPool(poolName,
sanitize(poolName), if (poolName.endsWith("Eden Space")) Usage.Eden else Usage.Unknown))

private val _memoryRegionMappings: Map[String, MemoryPool] = Map (
"Metaspace" -> MemoryPool("Metaspace", "metaspace", Usage.Metaspace),
Expand Down
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object BaseProject extends AutoPlugin {
val hdrHistogram = "org.hdrhistogram" % "HdrHistogram" % "2.1.10"
val okHttp = "com.squareup.okhttp3" % "okhttp" % "3.14.7"
val okHttpMockServer = "com.squareup.okhttp3" % "mockwebserver" % "3.10.0"
val oshiCore = "com.github.oshi" % "oshi-core" % "5.3.4"
val oshiCore = "com.github.oshi" % "oshi-core" % "5.3.6"


val kanelaAgentVersion = settingKey[String]("Kanela Agent version")
Expand Down