diff options
author | Alexey Noskov <alexey.noskov@gmail.com> | 2015-09-26 18:03:15 +0300 |
---|---|---|
committer | Alexey Noskov <alexey.noskov@gmail.com> | 2015-09-26 20:49:38 +0300 |
commit | c4b58f26f43f3767633d9c06c9a65d6549093e35 (patch) | |
tree | 661b573c77357acffb99881a34a3e547808c670b /kamon-system-metrics | |
parent | 6e07de4432c587a10c2ee27a5289898c722d0078 (diff) | |
download | Kamon-c4b58f26f43f3767633d9c06c9a65d6549093e35.tar.gz Kamon-c4b58f26f43f3767633d9c06c9a65d6549093e35.tar.bz2 Kamon-c4b58f26f43f3767633d9c06c9a65d6549093e35.zip |
! system-metrics: Fix heap metrics update
Don't store reference to MemoryUsage object: it's immutable, instead store function extracting it from MemoryMXBean
Diffstat (limited to 'kamon-system-metrics')
-rw-r--r-- | kamon-system-metrics/src/main/scala/kamon/system/jmx/MemoryUsageMetrics.scala | 16 | ||||
-rw-r--r-- | kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala | 14 |
2 files changed, 22 insertions, 8 deletions
diff --git a/kamon-system-metrics/src/main/scala/kamon/system/jmx/MemoryUsageMetrics.scala b/kamon-system-metrics/src/main/scala/kamon/system/jmx/MemoryUsageMetrics.scala index ef9d47a6..41681459 100644 --- a/kamon-system-metrics/src/main/scala/kamon/system/jmx/MemoryUsageMetrics.scala +++ b/kamon-system-metrics/src/main/scala/kamon/system/jmx/MemoryUsageMetrics.scala @@ -32,13 +32,13 @@ import scala.collection.convert.WrapAsScala class MemoryUsageMetrics(instrumentFactory: InstrumentFactory, beansWithNames: Iterable[MemoryUsageWithMetricName]) extends GenericEntityRecorder(instrumentFactory) { beansWithNames.foreach { - case MemoryUsageWithMetricName(name, bean) ⇒ + case MemoryUsageWithMetricName(name, beanFun) ⇒ gauge(name + "-used", Memory.Bytes, () ⇒ { - bean.getUsed + beanFun().getUsed }) gauge(name + "-max", Memory.Bytes, () ⇒ { - val max = bean.getMax + val max = beanFun().getMax // .getMax can return -1 if the max is not defined. if (max >= 0) max @@ -46,7 +46,7 @@ class MemoryUsageMetrics(instrumentFactory: InstrumentFactory, }) gauge(name + "-committed", Memory.Bytes, () ⇒ { - bean.getCommitted + beanFun().getCommitted }) } } @@ -54,9 +54,9 @@ class MemoryUsageMetrics(instrumentFactory: InstrumentFactory, /** * Objects of this kind may be passed to instances of [[MemoryUsageMetrics]] for data collection. * @param metricName The sanitized name for a metric. - * @param bean The data source for metrics. + * @param beanFun Function returning the data source for metrics. */ -private[jmx] final case class MemoryUsageWithMetricName(metricName: String, bean: MemoryUsage) +private[jmx] final case class MemoryUsageWithMetricName(metricName: String, beanFun: () ⇒ MemoryUsage) /** * Memory Pool metrics, as reported by JMX: @@ -79,7 +79,7 @@ object MemoryUsageMetrics extends JmxSystemMetricRecorderCompanion("jmx-memory") def apply(instrumentFactory: InstrumentFactory): MemoryUsageMetrics = new MemoryUsageMetrics(instrumentFactory, - MemoryUsageWithMetricName("non-heap", memoryMXBean.getNonHeapMemoryUsage) :: - MemoryUsageWithMetricName("heap", memoryMXBean.getHeapMemoryUsage) :: + MemoryUsageWithMetricName("non-heap", () ⇒ memoryMXBean.getNonHeapMemoryUsage) :: + MemoryUsageWithMetricName("heap", () ⇒ memoryMXBean.getHeapMemoryUsage) :: usagesWithNames) } diff --git a/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala b/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala index 7af704d5..452ee0c7 100644 --- a/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala +++ b/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala @@ -74,6 +74,20 @@ class SystemMetricsSpec extends BaseKamonSpec("system-metrics-spec") with Redire memoryMetrics.gauge("non-heap-committed").get.numberOfMeasurements should be > 0L } + "record correctly updatable values for heap metrics" in { + Thread.sleep(3000) + + val data = new Array[Byte](20 * 1024 * 1024) // 20 Mb of data + + Thread.sleep(3000) + + val memoryMetrics = takeSnapshotOf("jmx-memory", "system-metric") + val heapUsed = memoryMetrics.gauge("heap-used").get + + heapUsed.max should be > heapUsed.min + data.size should be > 0 // Just for data usage + } + "record daemon, count and peak jvm threads metrics" in { val threadsMetrics = takeSnapshotOf("threads", "system-metric") |