aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/test/scala/kamon/KamonLifecycleSpec.scala
blob: 2fbd1b71417ba1844f6da9d805e0112809b654f8 (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
package kamon

import akka.actor.ActorSystem
import akka.testkit.{TestKitBase, TestProbe}
import com.typesafe.config.ConfigFactory
import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot
import kamon.metric.{EntitySnapshot, SubscriptionsDispatcher}
import kamon.util.LazyActorRef
import org.scalatest.{Matchers, WordSpecLike}
import org.scalactic.TimesOnInt._

import scala.concurrent.duration._

class KamonLifecycleSpec extends TestKitBase with WordSpecLike with Matchers {
  override implicit lazy val system: ActorSystem = ActorSystem("kamon-lifecycle-spec")

  "The Kamon lifecycle" should {
    "allow Kamon to be used before it gets started" in {
      val someMetric = Kamon.metrics.histogram("allow-me-before-start")
    }

    "allow Kamon to be started/shutdown several times" in {
      10 times {
        Kamon.shutdown()
        Kamon.start()
        Kamon.start()
        Kamon.shutdown()
        Kamon.shutdown()
      }
    }

    "not dispatch subscriptions before Kamon startup" in {
      val subscriber = TestProbe()
      Kamon.metrics.histogram("only-after-startup").record(100)
      Kamon.metrics.subscribe("**", "**", subscriber.ref, permanently = true)

      flushSubscriptions()
      subscriber.expectNoMsg(300 millis)

      Kamon.metrics.histogram("only-after-startup").record(100)
      Kamon.start()
      flushSubscriptions()
      subscriber.expectMsgType[TickMetricSnapshot]
      Kamon.shutdown()
    }

    "not dispatch subscriptions after Kamon shutdown" in {
      val subscriber = TestProbe()
      Kamon.start()
      Kamon.metrics.histogram("only-before-shutdown").record(100)
      Kamon.metrics.subscribe("**", "**", subscriber.ref, permanently = true)

      flushSubscriptions()
      subscriber.expectMsgType[TickMetricSnapshot]

      Kamon.metrics.histogram("only-before-shutdown").record(100)
      Kamon.shutdown()
      Thread.sleep(500)
      flushSubscriptions()
      subscriber.expectNoMsg(300 millis)
    }

    "reconfigure filters after being started" in {
      val customConfig = ConfigFactory.parseString(
        """
          |kamon.metric.filters.histogram {
          |  includes = [ "**" ]
          |  excludes = ["untracked-histogram"]
          |}
        """.stripMargin
      )

      Kamon.metrics.shouldTrack("untracked-histogram", "histogram") shouldBe true
      Kamon.start(customConfig.withFallback(ConfigFactory.load()))
      Kamon.metrics.shouldTrack("untracked-histogram", "histogram") shouldBe false

    }
  }

  def takeSnapshotOf(name: String, category: String): EntitySnapshot = {
    val collectionContext = Kamon.metrics.buildDefaultCollectionContext
    val recorder = Kamon.metrics.find(name, category).get
    recorder.collect(collectionContext)
  }

  def flushSubscriptions(): Unit = {
    val subscriptionsField = Kamon.metrics.getClass.getDeclaredField("_subscriptions")
    subscriptionsField.setAccessible(true)
    val subscriptions = subscriptionsField.get(Kamon.metrics).asInstanceOf[LazyActorRef]

    subscriptions.tell(SubscriptionsDispatcher.Tick)
  }
}