diff options
Diffstat (limited to 'kamon-core/src/main/scala/kamon/util')
3 files changed, 65 insertions, 28 deletions
diff --git a/kamon-core/src/main/scala/kamon/util/ConfigTools.scala b/kamon-core/src/main/scala/kamon/util/ConfigTools.scala index bcec22c3..d0665764 100644 --- a/kamon-core/src/main/scala/kamon/util/ConfigTools.scala +++ b/kamon-core/src/main/scala/kamon/util/ConfigTools.scala @@ -22,6 +22,8 @@ import com.typesafe.config.Config import scala.concurrent.duration.FiniteDuration +import kamon.metric.instrument.{ Memory, Time } + object ConfigTools { implicit class Syntax(val config: Config) extends AnyVal { // We are using the deprecated .getNanoseconds option to keep Kamon source code compatible with @@ -37,6 +39,10 @@ object ConfigTools { case entry ⇒ entry.getKey.takeWhile(_ != '.') } toSet } + + def time(path: String): Time = Time(config.getString(path)) + + def memory(path: String): Memory = Memory(config.getString(path)) } } diff --git a/kamon-core/src/main/scala/kamon/util/NeedToScale.scala b/kamon-core/src/main/scala/kamon/util/NeedToScale.scala new file mode 100644 index 00000000..19e1ae06 --- /dev/null +++ b/kamon-core/src/main/scala/kamon/util/NeedToScale.scala @@ -0,0 +1,37 @@ +/* + * ========================================================================================= + * Copyright © 2013-2015 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.util + +import com.typesafe.config.Config +import kamon.metric.instrument.{ Memory, Time } +import kamon.util.ConfigTools._ + +object NeedToScale { + val TimeUnits = "time-units" + val MemoryUnits = "memory-units" + + def unapply(config: Config): Option[(Option[Time], Option[Memory])] = { + val scaleTimeTo: Option[Time] = + if (config.hasPath(TimeUnits)) Some(config.time(TimeUnits)) else None + + val scaleMemoryTo: Option[Memory] = + if (config.hasPath(MemoryUnits)) Some(config.memory(MemoryUnits)) else None + if (scaleTimeTo.isDefined || scaleMemoryTo.isDefined) Some(scaleTimeTo -> scaleMemoryTo) + else None + } +} + diff --git a/kamon-core/src/main/scala/kamon/util/executors/ExecutorServiceMetrics.scala b/kamon-core/src/main/scala/kamon/util/executors/ExecutorServiceMetrics.scala index 7a87163f..98d2ea0c 100644 --- a/kamon-core/src/main/scala/kamon/util/executors/ExecutorServiceMetrics.scala +++ b/kamon-core/src/main/scala/kamon/util/executors/ExecutorServiceMetrics.scala @@ -16,10 +16,10 @@ package kamon.util.executors +import java.util.concurrent.{ ExecutorService, ForkJoinPool ⇒ JavaForkJoinPool, ThreadPoolExecutor } + import kamon.Kamon import kamon.metric.Entity -import java.util.concurrent.{ ForkJoinPool ⇒ JavaForkJoinPool } -import java.util.concurrent.{ ExecutorService, ThreadPoolExecutor } import scala.concurrent.forkjoin.ForkJoinPool import scala.util.control.NoStackTrace @@ -48,8 +48,10 @@ object ExecutorServiceMetrics { * @param threadPool The intance of the [[ThreadPoolExecutor]] * @param tags The tags associated to the [[ThreadPoolExecutor]] */ - private def registerThreadPool(name: String, threadPool: ThreadPoolExecutor, tags: Map[String, String]): Unit = { - Kamon.metrics.entity(ThreadPoolExecutorMetrics.factory(threadPool, Category), Entity(name, Category, tags)) + @inline private def registerThreadPool(name: String, threadPool: ThreadPoolExecutor, tags: Map[String, String]): Entity = { + val threadPoolEntity = Entity(name, Category, tags + ("executor-type" -> "thread-pool-executor")) + Kamon.metrics.entity(ThreadPoolExecutorMetrics.factory(threadPool, Category), threadPoolEntity) + threadPoolEntity } /** @@ -60,8 +62,10 @@ object ExecutorServiceMetrics { * @param forkJoinPool The instance of the [[ForkJoinPool]] * @param tags The tags associated to the [[ForkJoinPool]] */ - private def registerScalaForkJoin(name: String, forkJoinPool: ForkJoinPool, tags: Map[String, String] = Map.empty): Unit = { - Kamon.metrics.entity(ForkJoinPoolMetrics.factory(forkJoinPool, Category), Entity(name, Category, tags)) + @inline private def registerScalaForkJoin(name: String, forkJoinPool: ForkJoinPool, tags: Map[String, String]): Entity = { + val forkJoinEntity = Entity(name, Category, tags + ("executor-type" -> "fork-join-pool")) + Kamon.metrics.entity(ForkJoinPoolMetrics.factory(forkJoinPool, Category), forkJoinEntity) + forkJoinEntity } /** @@ -72,8 +76,10 @@ object ExecutorServiceMetrics { * @param forkJoinPool The instance of the [[JavaForkJoinPool]] * @param tags The tags associated to the [[JavaForkJoinPool]] */ - private def registerJavaForkJoin(name: String, forkJoinPool: JavaForkJoinPool, tags: Map[String, String] = Map.empty): Unit = { - Kamon.metrics.entity(ForkJoinPoolMetrics.factory(forkJoinPool, Category), Entity(name, Category, tags)) + @inline private def registerJavaForkJoin(name: String, forkJoinPool: JavaForkJoinPool, tags: Map[String, String]): Entity = { + val forkJoinEntity = Entity(name, Category, tags + ("executor-type" -> "fork-join-pool")) + Kamon.metrics.entity(ForkJoinPoolMetrics.factory(forkJoinPool, Category), forkJoinEntity) + forkJoinEntity } /** @@ -84,7 +90,7 @@ object ExecutorServiceMetrics { * @param executorService The instance of the [[ExecutorService]] * @param tags The tags associated to the [[ExecutorService]] */ - def register(name: String, executorService: ExecutorService, tags: Map[String, String]): Unit = executorService match { + def register(name: String, executorService: ExecutorService, tags: Map[String, String]): Entity = executorService match { case threadPoolExecutor: ThreadPoolExecutor ⇒ registerThreadPool(name, threadPoolExecutor, tags) case scalaForkJoinPool: ForkJoinPool if scalaForkJoinPool.getClass.isAssignableFrom(ScalaForkJoinPool) ⇒ registerScalaForkJoin(name, scalaForkJoinPool, tags) case javaForkJoinPool: JavaForkJoinPool if javaForkJoinPool.getClass.isAssignableFrom(JavaForkJoinPool) ⇒ registerJavaForkJoin(name, javaForkJoinPool, tags) @@ -94,36 +100,24 @@ object ExecutorServiceMetrics { case other ⇒ throw new NotSupportedException(s"The ExecutorService $name is not supported.") } - //Java variants - def register(name: String, executorService: ExecutorService): Unit = { - register(name, executorService, Map.empty[String, String]) - } - - def register(name: String, executorService: ExecutorService, tags: java.util.Map[String, String]): Unit = { + //Java variant + def register(name: String, executorService: ExecutorService, tags: java.util.Map[String, String]): Entity = { import scala.collection.JavaConverters._ register(name, executorService, tags.asScala.toMap) } /** * - * Remove the [[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html ExecutorService]] to Monitor. + * Register the [[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html ExecutorService]] to Monitor. * * @param name The name of the [[ExecutorService]] - * @param tags The tags associated to the [[ExecutorService]] + * @param executorService The instance of the [[ExecutorService]] */ - def remove(name: String, tags: Map[String, String]): Unit = { - Kamon.metrics.removeEntity(name, Category, tags) - } - - //Java variants - def remove(name: String): Unit = { - remove(name, Map.empty[String, String]) + def register(name: String, executorService: ExecutorService): Entity = { + register(name, executorService, Map.empty[String, String]) } - def remove(name: String, tags: java.util.Map[String, String]): Unit = { - import scala.collection.JavaConverters._ - remove(name, tags.asScala.toMap) - } + def remove(entity: Entity): Unit = Kamon.metrics.removeEntity(entity) /** * INTERNAL USAGE ONLY |