diff options
Diffstat (limited to 'kamon-newrelic/src/main/scala/kamon')
6 files changed, 69 insertions, 94 deletions
diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala index 7c2b34ea..4082458c 100644 --- a/kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala +++ b/kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala @@ -15,10 +15,10 @@ * ========================================================== */ package kamon.newrelic -import akka.actor.{ActorLogging, Actor} +import akka.actor.{ ActorLogging, Actor } import spray.json._ import scala.concurrent.Future -import spray.httpx.{SprayJsonSupport, RequestBuilding, ResponseTransformation} +import spray.httpx.{ SprayJsonSupport, RequestBuilding, ResponseTransformation } import spray.httpx.encoding.Deflate import spray.http._ import spray.json.lenses.JsonLenses._ @@ -26,7 +26,7 @@ import akka.pattern.pipe import java.lang.management.ManagementFactory import spray.client.pipelining._ import scala.util.control.NonFatal -import kamon.newrelic.NewRelicMetric.{Data, ID, MetricBatch} +import kamon.newrelic.NewRelicMetric.{ Data, ID, MetricBatch } class Agent extends Actor with RequestBuilding with ResponseTransformation with SprayJsonSupport with ActorLogging { import context.dispatcher @@ -43,15 +43,12 @@ class Agent extends Actor with RequestBuilding with ResponseTransformation with AgentInfo(licenseKey, appName, runtimeName(1), runtimeName(0).toInt) } - - def receive = { - case Initialize(runId, collector) => context become reporting(runId, collector) + case Initialize(runId, collector) ⇒ context become reporting(runId, collector) } - def reporting(runId: Long, collector: String): Receive = { - case batch: MetricBatch => sendMetricData(runId, collector, batch.metrics) + case batch: MetricBatch ⇒ sendMetricData(runId, collector, batch.metrics) } override def preStart(): Unit = { @@ -60,26 +57,26 @@ class Agent extends Actor with RequestBuilding with ResponseTransformation with } def initialize: Unit = { - pipe ({ - for( - collector <- selectCollector; - runId <- connect(collector, agentInfo) - ) yield Initialize(runId, collector) - } recover { - case NonFatal(ex) => InitializationFailed(ex) - }) to self + pipe({ + for ( + collector ← selectCollector; + runId ← connect(collector, agentInfo) + ) yield Initialize(runId, collector) + } recover { + case NonFatal(ex) ⇒ InitializationFailed(ex) + }) to self } import AgentJsonProtocol._ - val compressedPipeline: HttpRequest => Future[HttpResponse] = encode(Deflate) ~> sendReceive - val compressedToJsonPipeline: HttpRequest => Future[JsValue] = compressedPipeline ~> toJson + val compressedPipeline: HttpRequest ⇒ Future[HttpResponse] = encode(Deflate) ~> sendReceive + val compressedToJsonPipeline: HttpRequest ⇒ Future[JsValue] = compressedPipeline ~> toJson def toJson(response: HttpResponse): JsValue = response.entity.asString.asJson def selectCollector: Future[String] = { compressedToJsonPipeline { Post(s"http://collector.newrelic.com/agent_listener/invoke_raw_method?method=get_redirect_host&license_key=${agentInfo.licenseKey}&marshal_format=json&protocol_version=12", JsArray()) - } map { json => + } map { json ⇒ json.extract[String]('return_value) } } @@ -87,12 +84,11 @@ class Agent extends Actor with RequestBuilding with ResponseTransformation with def connect(collectorHost: String, connect: AgentInfo): Future[Long] = { compressedToJsonPipeline { Post(s"http://$collectorHost/agent_listener/invoke_raw_method?method=connect&license_key=${agentInfo.licenseKey}&marshal_format=json&protocol_version=12", connect) - } map { json => + } map { json ⇒ json.extract[Long]('return_value / 'agent_run_id) } } - def sendMetricData(runId: Long, collector: String, metrics: List[(ID, Data)]) = { log.info("Reporting this to NewRelic: " + metrics.mkString("\n")) @@ -103,14 +99,10 @@ class Agent extends Actor with RequestBuilding with ResponseTransformation with } } - - } object Agent { - - case class Initialize(runId: Long, collector: String) case class InitializationFailed(reason: Throwable) case class CollectorSelection(return_value: String) diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala index b133aee0..da8199ab 100644 --- a/kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala +++ b/kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala @@ -25,18 +25,16 @@ object AgentJsonProtocol extends DefaultJsonProtocol { JsArray( JsObject( "agent_version" -> JsString("3.1.0"), - "app_name" -> JsArray(JsString(obj.appName)), - "host" -> JsString(obj.host), - "identifier" -> JsString(s"java:${obj.appName}"), - "language" -> JsString("java"), - "pid" -> JsNumber(obj.pid) - ) - ) + "app_name" -> JsArray(JsString(obj.appName)), + "host" -> JsString(obj.host), + "identifier" -> JsString(s"java:${obj.appName}"), + "language" -> JsString("java"), + "pid" -> JsNumber(obj.pid))) } import NewRelicMetric._ - implicit def listWriter[T : JsonWriter] = new JsonWriter[List[T]] { + implicit def listWriter[T: JsonWriter] = new JsonWriter[List[T]] { def write(list: List[T]) = JsArray(list.map(_.toJson)) } @@ -46,16 +44,14 @@ object AgentJsonProtocol extends DefaultJsonProtocol { JsArray( JsObject( "name" -> JsString(id.name) // TODO Include scope - ), + ), JsArray( JsNumber(data.callCount), JsNumber(data.total), JsNumber(data.totalExclusive), JsNumber(data.min), JsNumber(data.max), - JsNumber(data.sumOfSquares) - ) - ) + JsNumber(data.sumOfSquares))) } } @@ -65,7 +61,6 @@ object AgentJsonProtocol extends DefaultJsonProtocol { JsNumber(obj.runId), JsNumber(obj.start), JsNumber(obj.end), - obj.metrics.toJson - ) + obj.metrics.toJson) } } diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/Apdex.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/Apdex.scala index d0a71c08..53240c89 100644 --- a/kamon-newrelic/src/main/scala/kamon/newrelic/Apdex.scala +++ b/kamon-newrelic/src/main/scala/kamon/newrelic/Apdex.scala @@ -17,7 +17,7 @@ package kamon.newrelic import akka.actor.Actor import kamon.trace.UowTrace -import com.newrelic.api.agent.{NewRelic => NRAgent} +import com.newrelic.api.agent.{ NewRelic ⇒ NRAgent } import kamon.trace.UowTracing.WebExternal class Apdex extends Actor { @@ -28,7 +28,7 @@ class Apdex extends Actor { var frustrated: Int = 0 def receive = { - case trace: UowTrace => recordTransaction(trace) + case trace: UowTrace ⇒ recordTransaction(trace) } @@ -41,26 +41,25 @@ class Apdex extends Actor { def total: Int = satisfied + tolerating + frustrated def updateStats(sampleTime: Double): Unit = { - if(sampleTime < t) + if (sampleTime < t) satisfied += 1 + else if (sampleTime >= t && sampleTime <= 4 * t) + tolerating += 1 else - if(sampleTime >= t && sampleTime <= 4*t) - tolerating += 1 - else - frustrated += 1 + frustrated += 1 } def recordTransaction(uowTrace: UowTrace): Unit = { - val time = ((uowTrace.segments.last.timestamp - uowTrace.segments.head.timestamp)/1E9) + val time = ((uowTrace.segments.last.timestamp - uowTrace.segments.head.timestamp) / 1E9) updateStats(time) - NRAgent.recordMetric("WebTransaction/Custom" + uowTrace.name, time.toFloat ) + NRAgent.recordMetric("WebTransaction/Custom" + uowTrace.name, time.toFloat) NRAgent.recordMetric("WebTransaction", time.toFloat) NRAgent.recordMetric("HttpDispatcher", time.toFloat) - uowTrace.segments.collect { case we: WebExternal => we }.foreach { webExternalTrace => - val external = ((webExternalTrace.finish - webExternalTrace.start)/1E9).toFloat + uowTrace.segments.collect { case we: WebExternal ⇒ we }.foreach { webExternalTrace ⇒ + val external = ((webExternalTrace.finish - webExternalTrace.start) / 1E9).toFloat println("Web External: " + webExternalTrace) NRAgent.recordMetric(s"External/${webExternalTrace.host}/http", external) @@ -68,22 +67,19 @@ class Apdex extends Actor { NRAgent.recordMetric(s"External/${webExternalTrace.host}/http/WebTransaction/Custom" + uowTrace.name, external) } - - val allExternals = uowTrace.segments.collect { case we: WebExternal => we } sortBy(_.timestamp) - + val allExternals = uowTrace.segments.collect { case we: WebExternal ⇒ we } sortBy (_.timestamp) def measureExternal(accum: Long, lastEnd: Long, segments: Seq[WebExternal]): Long = segments match { - case Nil => accum - case head :: tail => - if(head.start > lastEnd) - measureExternal(accum + (head.finish-head.start), head.finish, tail) + case Nil ⇒ accum + case head :: tail ⇒ + if (head.start > lastEnd) + measureExternal(accum + (head.finish - head.start), head.finish, tail) else - measureExternal(accum + (head.finish-lastEnd), head.finish, tail) + measureExternal(accum + (head.finish - lastEnd), head.finish, tail) } val external = measureExternal(0, 0, allExternals) / 1E9 - NRAgent.recordMetric(s"External/all", external.toFloat) NRAgent.recordMetric(s"External/allWeb", external.toFloat) diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala index f9b8b916..e76c9bde 100644 --- a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala +++ b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala @@ -18,8 +18,8 @@ package kamon.newrelic import akka.actor._ import scala.collection.mutable import kamon.Kamon -import kamon.trace.{UowTrace, Trace} -import kamon.newrelic.NewRelicMetric.{MetricBatch, FlushMetrics} +import kamon.trace.{ UowTrace, Trace } +import kamon.newrelic.NewRelicMetric.{ MetricBatch, FlushMetrics } import scala.concurrent.duration._ class NewRelic extends ExtensionId[NewRelicExtension] { @@ -35,8 +35,6 @@ class NewRelicManager extends Actor with ActorLogging { Kamon(Trace)(context.system) ! Trace.Register - - val webTransactionMetrics = context.actorOf(Props[WebTransactionMetrics], "web-transaction-metrics") val agent = context.actorOf(Props[Agent], "agent") @@ -46,7 +44,7 @@ class NewRelicManager extends Actor with ActorLogging { } def receive = { - case trace: UowTrace => webTransactionMetrics ! trace + case trace: UowTrace ⇒ webTransactionMetrics ! trace } } @@ -54,8 +52,8 @@ object NewRelicMetric { case class ID(name: String, scope: Option[String]) case class Data(var callCount: Long, var total: Double, var totalExclusive: Double, var min: Double, var max: Double, var sumOfSquares: Double) { def record(value: Double): Unit = { - if(value > max) max = value - if(value < min) min = value + if (value > max) max = value + if (value < min) min = value total += value totalExclusive += value @@ -72,15 +70,14 @@ object NewRelicMetric { case class MetricBatch(metrics: List[(ID, Data)]) } - class WebTransactionMetrics extends Actor with ActorLogging { val apdexT = 0.5D var metrics = mutable.Map.empty[NewRelicMetric.ID, NewRelicMetric.Data] var apdex = NewRelicMetric.Data(0, 0, 0, apdexT, apdexT, 0) def receive = { - case trace: UowTrace => updateStats(trace) - case FlushMetrics => flush + case trace: UowTrace ⇒ updateStats(trace) + case FlushMetrics ⇒ flush } def flush: Unit = { @@ -94,13 +91,12 @@ class WebTransactionMetrics extends Actor with ActorLogging { } def recordApdex(time: Double): Unit = { - if(time <= apdexT) + if (time <= apdexT) apdex.callCount += 1 + else if (time > apdexT && time <= (4 * apdexT)) + apdex.total += 1 else - if(time > apdexT && time <= (4 * apdexT)) - apdex.total += 1 - else - apdex.totalExclusive += 1 + apdex.totalExclusive += 1 } diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala index 844f18ea..c6d87769 100644 --- a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala +++ b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala @@ -17,22 +17,22 @@ package kamon.newrelic import akka.actor.Actor import akka.event.Logging.Error -import akka.event.Logging.{LoggerInitialized, InitializeLogger} +import akka.event.Logging.{ LoggerInitialized, InitializeLogger } import com.newrelic.api.agent.NewRelic import kamon.trace.ContextAware class NewRelicErrorLogger extends Actor { def receive = { - case InitializeLogger(_) => sender ! LoggerInitialized - case error @ Error(cause, logSource, logClass, message) => notifyError(error) - case anythingElse => + case InitializeLogger(_) ⇒ sender ! LoggerInitialized + case error @ Error(cause, logSource, logClass, message) ⇒ notifyError(error) + case anythingElse ⇒ } def notifyError(error: Error): Unit = { val params = new java.util.HashMap[String, String]() val ctx = error.asInstanceOf[ContextAware].traceContext - for(c <- ctx) { + for (c ← ctx) { params.put("UOW", c.uow) } diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicReporting.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicReporting.scala index 2a2d4442..260fc71e 100644 --- a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicReporting.scala +++ b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicReporting.scala @@ -20,21 +20,20 @@ import kamon.trace.UowTrace import com.newrelic.api.agent.NewRelic import kamon.trace.UowTracing.WebExternal - class NewRelicReporting extends Actor { def receive = { - case trace: UowTrace => recordTransaction(trace) + case trace: UowTrace ⇒ recordTransaction(trace) } def recordTransaction(uowTrace: UowTrace): Unit = { - val time = ((uowTrace.segments.last.timestamp - uowTrace.segments.head.timestamp)/1E9) + val time = ((uowTrace.segments.last.timestamp - uowTrace.segments.head.timestamp) / 1E9) - NewRelic.recordMetric("WebTransaction/Custom" + uowTrace.name, time.toFloat ) + NewRelic.recordMetric("WebTransaction/Custom" + uowTrace.name, time.toFloat) NewRelic.recordMetric("WebTransaction", time.toFloat) NewRelic.recordMetric("HttpDispatcher", time.toFloat) - uowTrace.segments.collect { case we: WebExternal => we }.foreach { webExternalTrace => - val external = ((webExternalTrace.finish - webExternalTrace.start)/1E9).toFloat + uowTrace.segments.collect { case we: WebExternal ⇒ we }.foreach { webExternalTrace ⇒ + val external = ((webExternalTrace.finish - webExternalTrace.start) / 1E9).toFloat println("Web External: " + webExternalTrace) NewRelic.recordMetric(s"External/${webExternalTrace.host}/http", external) @@ -42,22 +41,19 @@ class NewRelicReporting extends Actor { NewRelic.recordMetric(s"External/${webExternalTrace.host}/http/WebTransaction/Custom" + uowTrace.name, external) } - - val allExternals = uowTrace.segments.collect { case we: WebExternal => we } sortBy(_.timestamp) - + val allExternals = uowTrace.segments.collect { case we: WebExternal ⇒ we } sortBy (_.timestamp) def measureExternal(accum: Long, lastEnd: Long, segments: Seq[WebExternal]): Long = segments match { - case Nil => accum - case head :: tail => - if(head.start > lastEnd) - measureExternal(accum + (head.finish-head.start), head.finish, tail) + case Nil ⇒ accum + case head :: tail ⇒ + if (head.start > lastEnd) + measureExternal(accum + (head.finish - head.start), head.finish, tail) else - measureExternal(accum + (head.finish-lastEnd), head.finish, tail) + measureExternal(accum + (head.finish - lastEnd), head.finish, tail) } val external = measureExternal(0, 0, allExternals) / 1E9 - NewRelic.recordMetric(s"External/all", external.toFloat) NewRelic.recordMetric(s"External/allWeb", external.toFloat) |