This repository has been archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathMemoryUsageMetrics.scala
131 lines (109 loc) · 5.41 KB
/
MemoryUsageMetrics.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* =========================================================================================
* Copyright © 2013-2017 the kamon project <http://kamon.io/>
*
* 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 kamon.system.jvm
import java.lang.management.{BufferPoolMXBean, ManagementFactory, MemoryUsage}
import kamon.Kamon
import kamon.metric.{Gauge, Histogram, MeasurementUnit}
import kamon.system.{JmxMetricBuilder, Metric, MetricBuilder}
import org.slf4j.Logger
import scala.collection.JavaConverters._
import scala.util.matching.Regex
/**
* Memory Pool metrics, as reported by JMX:
* - @see [[http://docs.oracle.com/javase/8/docs/api/java/lang/management/MemoryMXBean.html "MemoryMXBean"]]
* Pools in HotSpot Java 8:
* code-cache, metaspace, compressed-class-space, ps-eden-space, ps-survivor-space, ps-old-gen
*/
object MemoryUsageMetrics extends MetricBuilder("jvm.memory") with JmxMetricBuilder{
def build(metricPrefix: String, logger: Logger) = new Metric {
val invalidChars: Regex = """[^a-z0-9]""".r
val memoryMetrics = MemoryMetrics(metricPrefix)
val bufferPoolMetrics = BufferPoolMetrics(metricPrefix)
def update(): Unit = {
memoryUsageWithNames.foreach {
case MemoryUsageWithMetricName(name, beanFun) ⇒
val memory = memoryMetrics.forSegment(name)
memory.memoryUsed.record(beanFun().getUsed)
memory.memoryCommitted.record(beanFun().getCommitted)
memory.memoryMax.record({
val max = beanFun().getMax
// .getMax can return -1 if the max is not defined.
if (max >= 0) max
else 0
})
}
bufferPoolsWithNames.foreach {
case BufferPoolWithMetricName(name, beanFun) ⇒
val pool = bufferPoolMetrics.forPool(name)
pool.poolCount.set(beanFun().getCount)
pool.poolUsed.set(beanFun().getMemoryUsed)
pool.poolCapacity.set(beanFun().getTotalCapacity)
}
}
def sanitize(name: String): String =
invalidChars.replaceAllIn(name.toLowerCase, "-")
def usagesWithNames =
ManagementFactory.getMemoryPoolMXBeans.asScala.toList.map { bean ⇒
MemoryUsageWithMetricName(sanitize(bean.getName), () => bean.getUsage)
}
def bufferPoolsWithNames =
ManagementFactory.getPlatformMXBeans(classOf[BufferPoolMXBean]).asScala.toList.map { bean ⇒
BufferPoolWithMetricName(sanitize(bean.getName), () ⇒ bean)
}
def memoryUsageWithNames =
Seq(MemoryUsageWithMetricName("non-heap", () => ManagementFactory.getMemoryMXBean.getNonHeapMemoryUsage),
MemoryUsageWithMetricName("heap", () => ManagementFactory.getMemoryMXBean.getHeapMemoryUsage)) ++ usagesWithNames
}
}
final case class MemoryMetrics(metricPrefix:String) {
val memoryUsageMetric = Kamon.histogram(metricPrefix, MeasurementUnit.information.bytes)
def forSegment(segment: String): MemoryMetrics = {
val memoryTags = Map("segment" -> segment)
MemoryMetrics(
memoryTags,
memoryUsageMetric.refine("component" -> "system-metrics", "measure" -> "used", "segment" -> segment),
memoryUsageMetric.refine("component" -> "system-metrics", "measure" -> "committed", "segment" -> segment),
memoryUsageMetric.refine("component" -> "system-metrics", "measure" -> "max", "segment" -> segment)
)
}
case class MemoryMetrics(tags: Map[String, String], memoryUsed: Histogram, memoryCommitted: Histogram, memoryMax: Histogram)
}
final case class BufferPoolMetrics(metricPrefix:String) {
val bufferPoolCountMetric = Kamon.gauge(s"$metricPrefix.buffer-pool.count")
val bufferPoolUsageMetric = Kamon.gauge(s"$metricPrefix.buffer-pool.usage")
def forPool(pool: String): BufferPoolMetrics = {
val poolTags = Map("pool" -> pool)
BufferPoolMetrics(
poolTags,
bufferPoolCountMetric.refine("component" -> "system-metrics", "pool" -> pool),
bufferPoolUsageMetric.refine("component" -> "system-metrics", "pool" -> pool, "measure" -> "used"),
bufferPoolUsageMetric.refine("component" -> "system-metrics", "pool" -> pool, "measure" -> "capacity")
)
}
case class BufferPoolMetrics(tags: Map[String, String], poolCount: Gauge, poolUsed: Gauge, poolCapacity: Gauge)
}
/**
* Objects of this kind may be passed to instances of [[MemoryUsageMetrics]] for data collection.
* @param metricName The sanitized name for a metric.
* @param beanFun Function returning the data source for metrics.
*/
private final case class MemoryUsageWithMetricName(metricName: String, beanFun: () => MemoryUsage)
/**
* Objects of this kind may be passed to instances of [[BufferPoolMetrics]] for data collection.
* @param metricName The sanitized name for a metric.
* @param beanFun Function returning the data source for metrics.
*/
private final case class BufferPoolWithMetricName(metricName: String, beanFun: () => BufferPoolMXBean)