aboutsummaryrefslogtreecommitdiff
path: root/kamon-newrelic/src/test/scala/kamon/newrelic/AgentSpec.scala
blob: 8b61c241407b65ccdc04d7532d69f9b363c1363f (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
/*
 * =========================================================================================
 * 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.newrelic

import akka.actor.{ ActorRef, ActorSystem, Props }
import akka.io.IO
import akka.testkit.TestActor.{ AutoPilot, KeepRunning }
import akka.testkit._
import com.typesafe.config.ConfigFactory
import kamon.AkkaExtensionSwap
import kamon.newrelic.MetricTranslator.TimeSliceMetrics
import org.scalatest.{ BeforeAndAfterAll, WordSpecLike }
import spray.can.Http
import spray.http.{ HttpRequest, HttpResponse, _ }

class AgentSpec extends TestKitBase with WordSpecLike with BeforeAndAfterAll {

  import kamon.newrelic.AgentSpec._

  implicit lazy val system: ActorSystem = ActorSystem("Agent-Spec", ConfigFactory.parseString(
    """
      |akka {
      |  loggers = ["akka.testkit.TestEventListener"]
      |  loglevel = "INFO"
      |}
      |kamon {
      |  newrelic {
      |    retry-delay = 1 second
      |    max-retry = 3
      |  }
      |}
      |
    """.stripMargin))

  var agent: ActorRef = _

  setupFakeHttpManager

  "the Newrelic Agent" should {
    "try to connect upon creation, retry to connect if an error occurs" in {
      EventFilter.info(message = "Initialization failed: Unexpected response from HTTP transport: None, retrying in 1 seconds", occurrences = 3).intercept {
        system.actorOf(Props[Agent])
        Thread.sleep(1000)
      }
    }

    "when everything is fine should select a NewRelic collector" in {
      EventFilter.info(message = "Agent initialized with runID: [161221111] and collector: [collector-8.newrelic.com]", occurrences = 1).intercept {
        system.actorOf(Props[Agent])
      }
    }

    "merge the metrics if not possible send them and do it in the next post" in {
      EventFilter.info(pattern = "Trying to send metrics to NewRelic collector, attempt.*", occurrences = 2).intercept {
        agent = system.actorOf(Props[Agent].withDispatcher(CallingThreadDispatcher.Id))

        for (_  1 to 3) {
          sendDelayedMetric(agent)
        }
      }
    }

    "when the connection is re-established, the metrics should be send" in {
      EventFilter.info(message = "Sending metrics to NewRelic collector", occurrences = 2).intercept {
        sendDelayedMetric(agent)
      }
    }
  }

  def setupFakeHttpManager: Unit = {
    val ConnectionAttempts = 3 // an arbitrary value only for testing purposes
    val PostAttempts = 3 // if the number is achieved, the metrics should be discarded
    val fakeHttpManager = TestProbe()
    var attemptsToConnect: Int = 0 // should retry grab an NewRelic collector after retry-delay
    var attemptsToSendMetrics: Int = 0

    fakeHttpManager.setAutoPilot(new TestActor.AutoPilot {
      def run(sender: ActorRef, msg: Any): AutoPilot = {
        msg match {
          case HttpRequest(_, uri, _, _, _) if rawMethodIs("get_redirect_host", uri) 
            if (attemptsToConnect == ConnectionAttempts) {
              sender ! jsonResponse(
                """
                | {
                |   "return_value": "collector-8.newrelic.com"
                | }
                | """.stripMargin)
              system.log.info("Selecting Collector")

            } else {
              sender ! None
              attemptsToConnect += 1
              system.log.info("Network Error or Connection Refuse")
            }

          case HttpRequest(_, uri, _, _, _) if rawMethodIs("connect", uri) 
            sender ! jsonResponse(
              """
                | {
                |   "return_value": {
                |     "agent_run_id": 161221111
                |   }
                | }
                | """.stripMargin)
            system.log.info("Connecting")

          case HttpRequest(_, uri, _, _, _) if rawMethodIs("metric_data", uri) 
            if (attemptsToSendMetrics < PostAttempts) {
              sender ! None
              attemptsToSendMetrics += 1
              system.log.info("Error when trying to send metrics to NewRelic collector, the metrics will be merged")
            } else {
              system.log.info("Sending metrics to NewRelic collector")
            }
        }
        KeepRunning
      }

      def jsonResponse(json: String): HttpResponse = {
        HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, json))
      }

      def rawMethodIs(method: String, uri: Uri): Boolean = {
        uri.query.get("method").filter(_ == method).isDefined
      }
    })

    AkkaExtensionSwap.swap(system, Http, new IO.Extension {
      def manager: ActorRef = fakeHttpManager.ref
    })
  }

  override def afterAll() {
    super.afterAll()
    system.shutdown()
  }
}

object AgentSpec {
  def sendDelayedMetric(agent: ActorRef, delay: Int = 1000): Unit = {
    agent ! TimeSliceMetrics(100000L, 200000L, Map("Latency" -> NewRelic.Metric("Latency", None, 1000L, 2000D, 3000D, 1D, 100000D, 300D)))
    Thread.sleep(delay)
  }
}