1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical
21 
22 import akka.actor.typed.Behavior
23 import akka.actor.typed.scaladsl.Behaviors
24 import kamon.Kamon
25 import kamon.metric.Gauge
26 
27 import scala.concurrent.duration.DurationInt
28 
29 object MemoryMonitoringActor {
30   def apply(): Behavior[Monitor.type] = {
31     ActorSystemHelper.superviseWithBackoff(monitorMemory())
32   }
33 
34   private def monitorMemory(): Behavior[Monitor.type] = {
35     val memoryMax: Gauge = Kamon.gauge("jvm-memory-max").withoutTags()
36     val memoryFree: Gauge = Kamon.gauge("jvm-memory-free").withoutTags()
37     val memoryTotal: Gauge = Kamon.gauge("jvm-memory-total").withoutTags()
38 
39     Behaviors.withTimers { timers =>
40       timers.startTimerAtFixedRate(Monitor, 1.second)
41       Behaviors.receiveMessage {
42         case Monitor =>
43           memoryFree.update(Runtime.getRuntime.freeMemory().toDouble)
44           memoryMax.update(Runtime.getRuntime.maxMemory().toDouble)
45           memoryTotal.update(Runtime.getRuntime.totalMemory().toDouble)
46           Behaviors.same
47       }
48     }
49   }
50 
51   case object Monitor
52 
53   val name: String = "memory-monitor-backoff"
54 }
Line Stmt Id Pos Tree Symbol Tests Code
31 36388 1061 - 1076 Apply org.make.api.technical.MemoryMonitoringActor.monitorMemory MemoryMonitoringActor.this.monitorMemory()
31 49124 1022 - 1077 Apply org.make.api.technical.ActorSystemHelper.superviseWithBackoff ActorSystemHelper.superviseWithBackoff[org.make.api.technical.MemoryMonitoringActor.Monitor.type](MemoryMonitoringActor.this.monitorMemory())
35 41549 1168 - 1211 Apply kamon.metric.Metric.withoutTags kamon.Kamon.gauge("jvm-memory-max").withoutTags()
36 33132 1240 - 1284 Apply kamon.metric.Metric.withoutTags kamon.Kamon.gauge("jvm-memory-free").withoutTags()
37 50161 1314 - 1359 Apply kamon.metric.Metric.withoutTags kamon.Kamon.gauge("jvm-memory-total").withoutTags()
39 39713 1365 - 1757 Apply akka.actor.typed.scaladsl.Behaviors.withTimers akka.actor.typed.scaladsl.Behaviors.withTimers[org.make.api.technical.MemoryMonitoringActor.Monitor.type](((timers: akka.actor.typed.scaladsl.TimerScheduler[org.make.api.technical.MemoryMonitoringActor.Monitor.type]) => { timers.startTimerAtFixedRate(MemoryMonitoringActor.this.Monitor, scala.concurrent.duration.`package`.DurationInt(1).second); akka.actor.typed.scaladsl.Behaviors.receiveMessage[org.make.api.technical.MemoryMonitoringActor.Monitor.type](((x0$1: org.make.api.technical.MemoryMonitoringActor.Monitor.type) => x0$1 match { case MemoryMonitoringActor.this.Monitor => { memoryFree.update(java.lang.Runtime.getRuntime().freeMemory().toDouble); memoryMax.update(java.lang.Runtime.getRuntime().maxMemory().toDouble); memoryTotal.update(java.lang.Runtime.getRuntime().totalMemory().toDouble); akka.actor.typed.scaladsl.Behaviors.same[org.make.api.technical.MemoryMonitoringActor.Monitor.type] } })) }))
40 48082 1442 - 1450 Select scala.concurrent.duration.DurationConversions.second scala.concurrent.duration.`package`.DurationInt(1).second
40 39672 1404 - 1451 Apply akka.actor.typed.scaladsl.TimerScheduler.startTimerAtFixedRate timers.startTimerAtFixedRate(MemoryMonitoringActor.this.Monitor, scala.concurrent.duration.`package`.DurationInt(1).second)
40 34773 1442 - 1443 Literal <nosymbol> 1
40 42637 1433 - 1440 Select org.make.api.technical.MemoryMonitoringActor.Monitor MemoryMonitoringActor.this.Monitor
41 48603 1458 - 1751 Apply akka.actor.typed.scaladsl.Behaviors.receiveMessage akka.actor.typed.scaladsl.Behaviors.receiveMessage[org.make.api.technical.MemoryMonitoringActor.Monitor.type](((x0$1: org.make.api.technical.MemoryMonitoringActor.Monitor.type) => x0$1 match { case MemoryMonitoringActor.this.Monitor => { memoryFree.update(java.lang.Runtime.getRuntime().freeMemory().toDouble); memoryMax.update(java.lang.Runtime.getRuntime().maxMemory().toDouble); memoryTotal.update(java.lang.Runtime.getRuntime().totalMemory().toDouble); akka.actor.typed.scaladsl.Behaviors.same[org.make.api.technical.MemoryMonitoringActor.Monitor.type] } }))
43 49159 1519 - 1578 Apply kamon.metric.Gauge.update memoryFree.update(java.lang.Runtime.getRuntime().freeMemory().toDouble)
43 35811 1537 - 1577 Select scala.Long.toDouble java.lang.Runtime.getRuntime().freeMemory().toDouble
44 33172 1589 - 1646 Apply kamon.metric.Gauge.update memoryMax.update(java.lang.Runtime.getRuntime().maxMemory().toDouble)
44 41306 1606 - 1645 Select scala.Long.toDouble java.lang.Runtime.getRuntime().maxMemory().toDouble
45 43096 1657 - 1718 Apply kamon.metric.Gauge.update memoryTotal.update(java.lang.Runtime.getRuntime().totalMemory().toDouble)
45 46193 1676 - 1717 Select scala.Long.toDouble java.lang.Runtime.getRuntime().totalMemory().toDouble
46 34808 1729 - 1743 TypeApply akka.actor.typed.scaladsl.Behaviors.same akka.actor.typed.scaladsl.Behaviors.same[org.make.api.technical.MemoryMonitoringActor.Monitor.type]
53 35854 1807 - 1831 Literal <nosymbol> "memory-monitor-backoff"