aboutsummaryrefslogtreecommitdiff
path: root/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala
blob: a1c41551aa183812020a9269afee14e5d1b4d129 (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* =========================================================================================
 * Copyright © 2013-2014 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.metric

import java.lang.management.ManagementFactory

import com.typesafe.config.ConfigFactory
import kamon.system.jmx.GarbageCollectionMetrics
import kamon.testkit.BaseKamonSpec
import scala.collection.JavaConverters._

class SystemMetricsSpec extends BaseKamonSpec("system-metrics-spec") with RedirectLogging {

  override lazy val config =
    ConfigFactory.parseString(
      """
        |kamon.metric {
        |  tick-interval = 1 hour
        |}
        |
        |akka {
        |  extensions = ["kamon.system.SystemMetrics"]
        |}
      """.stripMargin)

  override protected def beforeAll(): Unit =
    Thread.sleep(2000) // Give some room to the recorders to store some values.

  "the Kamon System Metrics module" should {
    "record user, system, wait, idle and stolen CPU metrics" in {
      val cpuMetrics = takeSnapshotOf("cpu", "system-metric")

      cpuMetrics.histogram("cpu-user").get.numberOfMeasurements should be > 0L
      cpuMetrics.histogram("cpu-system").get.numberOfMeasurements should be > 0L
      cpuMetrics.histogram("cpu-wait").get.numberOfMeasurements should be > 0L
      cpuMetrics.histogram("cpu-idle").get.numberOfMeasurements should be > 0L
      cpuMetrics.histogram("cpu-stolen").get.numberOfMeasurements should be > 0L
    }

    "record count and time garbage collection metrics" in {
      val availableGarbageCollectors = ManagementFactory.getGarbageCollectorMXBeans.asScala.filter(_.isValid)

      for (collectorName  availableGarbageCollectors) {
        val sanitizedName = GarbageCollectionMetrics.sanitizeCollectorName(collectorName.getName)
        val collectorMetrics = takeSnapshotOf(s"$sanitizedName-garbage-collector", "system-metric")

        collectorMetrics.gauge("garbage-collection-count").get.numberOfMeasurements should be > 0L
        collectorMetrics.gauge("garbage-collection-time").get.numberOfMeasurements should be > 0L
      }
    }

    "record used, max and committed heap and non-heap metrics" in {
      val memoryMetrics = takeSnapshotOf("jmx-memory", "system-metric")

      memoryMetrics.gauge("heap-used").get.numberOfMeasurements should be > 0L
      memoryMetrics.gauge("heap-max").get.numberOfMeasurements should be > 0L
      memoryMetrics.gauge("heap-committed").get.numberOfMeasurements should be > 0L

      memoryMetrics.gauge("non-heap-used").get.numberOfMeasurements should be > 0L
      memoryMetrics.gauge("non-heap-max").get.numberOfMeasurements should be > 0L
      memoryMetrics.gauge("non-heap-committed").get.numberOfMeasurements should be > 0L

      memoryMetrics.gauge("direct-buffer-pool-count").get.numberOfMeasurements should be > 0L
      memoryMetrics.gauge("direct-buffer-pool-used").get.numberOfMeasurements should be > 0L
      memoryMetrics.gauge("direct-buffer-pool-capacity").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")

      threadsMetrics.gauge("daemon-thread-count").get.numberOfMeasurements should be > 0L
      threadsMetrics.gauge("peak-thread-count").get.numberOfMeasurements should be > 0L
      threadsMetrics.gauge("thread-count").get.numberOfMeasurements should be > 0L
    }

    "record loaded, unloaded and current class loading metrics" in {
      val classLoadingMetrics = takeSnapshotOf("class-loading", "system-metric")

      classLoadingMetrics.gauge("classes-loaded").get.numberOfMeasurements should be > 0L
      classLoadingMetrics.gauge("classes-unloaded").get.numberOfMeasurements should be > 0L
      classLoadingMetrics.gauge("classes-currently-loaded").get.numberOfMeasurements should be > 0L
    }

    "record reads, writes, queue time and service time file system metrics" in {
      val fileSystemMetrics = takeSnapshotOf("file-system", "system-metric")

      fileSystemMetrics.histogram("file-system-reads").get.numberOfMeasurements should be > 0L
      fileSystemMetrics.histogram("file-system-writes").get.numberOfMeasurements should be > 0L
    }

    "record 1 minute, 5 minutes and 15 minutes metrics load average metrics" in {
      val loadAverage = takeSnapshotOf("load-average", "system-metric")

      loadAverage.histogram("one-minute").get.numberOfMeasurements should be > 0L
      loadAverage.histogram("five-minutes").get.numberOfMeasurements should be > 0L
      loadAverage.histogram("fifteen-minutes").get.numberOfMeasurements should be > 0L
    }

    "record used, free, swap used, swap free system memory metrics" in {
      val memoryMetrics = takeSnapshotOf("memory", "system-metric")

      memoryMetrics.histogram("memory-used").get.numberOfMeasurements should be > 0L
      memoryMetrics.histogram("memory-free").get.numberOfMeasurements should be > 0L
      memoryMetrics.histogram("swap-used").get.numberOfMeasurements should be > 0L
      memoryMetrics.histogram("swap-free").get.numberOfMeasurements should be > 0L
    }

    "record rxBytes, txBytes, rxErrors, txErrors, rxDropped, txDropped network metrics" in {
      val networkMetrics = takeSnapshotOf("network", "system-metric")

      networkMetrics.histogram("tx-bytes").get.numberOfMeasurements should be > 0L
      networkMetrics.histogram("rx-bytes").get.numberOfMeasurements should be > 0L
      networkMetrics.histogram("tx-errors").get.numberOfMeasurements should be > 0L
      networkMetrics.histogram("rx-errors").get.numberOfMeasurements should be > 0L
      networkMetrics.histogram("tx-dropped").get.numberOfMeasurements should be > 0L
      networkMetrics.histogram("rx-dropped").get.numberOfMeasurements should be > 0L
    }

    "record system and user CPU percentage for the application process" in {
      val processCpuMetrics = takeSnapshotOf("process-cpu", "system-metric")

      processCpuMetrics.histogram("process-user-cpu").get.numberOfMeasurements should be > 0L
      processCpuMetrics.histogram("process-system-cpu").get.numberOfMeasurements should be > 0L
      processCpuMetrics.histogram("process-cpu").get.numberOfMeasurements should be > 0L
    }

    "record the open files for the application process" in {
      val openFilesMetrics = takeSnapshotOf("ulimit", "system-metric")

      openFilesMetrics.histogram("open-files").get.numberOfMeasurements should be > 0L
    }

    "record Context Switches Global, Voluntary and Non Voluntary metrics when running on Linux" in {
      if (isLinux) {
        val contextSwitchesMetrics = takeSnapshotOf("context-switches", "system-metric")

        contextSwitchesMetrics.histogram("context-switches-process-voluntary").get.numberOfMeasurements should be > 0L
        contextSwitchesMetrics.histogram("context-switches-process-non-voluntary").get.numberOfMeasurements should be > 0L
        contextSwitchesMetrics.histogram("context-switches-global").get.numberOfMeasurements should be > 0L
      }
    }
  }

  def isLinux: Boolean =
    System.getProperty("os.name").indexOf("Linux") != -1

}