aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/Utilities.scala
blob: b5c7d2a69bf8431ec2e80860ab1ed6230afe8a2c (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
package kamon

import java.util.concurrent.{Executors, ScheduledExecutorService, ScheduledThreadPoolExecutor}

import com.typesafe.config.Config
import kamon.util.{Clock, Filters, Matcher}

/**
  * Base utilities used by other Kamon components.
  */
trait Utilities { self: Configuration =>
  private val _clock = new Clock.Default()
  private val _scheduler = Executors.newScheduledThreadPool(schedulerPoolSize(self.config()), numberedThreadFactory("kamon-scheduler", daemon = false))
  @volatile private var _filters = Filters.fromConfig(self.config())

  self.onReconfigure(newConfig => {
    self._filters = Filters.fromConfig(newConfig)
    self._scheduler match {
      case stpe: ScheduledThreadPoolExecutor => stpe.setCorePoolSize(schedulerPoolSize(config))
      case _ => // cannot change the pool size on other unknown types.
    }
  })

  sys.addShutdownHook(() => _scheduler.shutdown())


  /**
    * Applies a filter to the given pattern. All filters are configured on the kamon.util.filters configuration section.
    *
    * @return true if the pattern matches at least one includes pattern and none of the excludes patterns in the filter.
    */
  def filter(filterName: String, pattern: String): Boolean =
    _filters.accept(filterName, pattern)

  /**
    * Retrieves a matcher for the given filter name. All filters are configured on the kamon.util.filters configuration
    * section.
    */
  def filter(filterName: String): Matcher =
    _filters.get(filterName)

  /**
    * Kamon's clock implementation.
    */
  def clock(): Clock =
    _clock

  /**
    * Scheduler to be used for Kamon-related tasks like updating gauges.
    */
  def scheduler(): ScheduledExecutorService =
    _scheduler



  private def schedulerPoolSize(config: Config): Int =
    config.getInt("kamon.scheduler-pool-size")
}