aboutsummaryrefslogtreecommitdiff
path: root/kamon-newrelic/src/main/scala
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2014-11-03 23:27:57 +0100
committerIvan Topolnjak <ivantopo@gmail.com>2014-11-03 23:27:57 +0100
commit59faf588080b137817444a6877170e2bd687427f (patch)
treea5f1cadf6c21915938d435e1a9d01583475d0c6a /kamon-newrelic/src/main/scala
parent4b999c39b6bd09d891de718fad10b795264755c6 (diff)
parent6e3d9ae88ecce10420eeac82294c54c1b43dedf4 (diff)
downloadKamon-59faf588080b137817444a6877170e2bd687427f.tar.gz
Kamon-59faf588080b137817444a6877170e2bd687427f.tar.bz2
Kamon-59faf588080b137817444a6877170e2bd687427f.zip
Merge branch 'master' into release-0.2, kamon-play still need fixes.
Conflicts: kamon-core/src/main/scala/kamon/instrumentation/akka/ActorCellInstrumentation.scala kamon-core/src/test/scala/kamon/metric/RouterMetricsSpec.scala kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala kamon-playground/src/main/scala/test/SimpleRequestProcessor.scala kamon-statsd/src/main/scala/kamon/statsd/StatsD.scala project/Dependencies.scala
Diffstat (limited to 'kamon-newrelic/src/main/scala')
-rw-r--r--kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala128
-rw-r--r--kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala4
-rw-r--r--kamon-newrelic/src/main/scala/kamon/newrelic/MetricTranslator.scala11
-rw-r--r--kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala33
-rw-r--r--kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala43
5 files changed, 131 insertions, 88 deletions
diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala
index 299773e4..bca02582 100644
--- a/kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala
+++ b/kamon-newrelic/src/main/scala/kamon/newrelic/Agent.scala
@@ -1,37 +1,46 @@
-/* ===================================================
- * Copyright © 2013 the kamon project <http://kamon.io/>
+/*
+ * =========================================================================================
+ * 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
+ * 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
+ * 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.
- * ========================================================== */
+ * 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 java.util.concurrent.TimeUnit.{ MILLISECONDS ⇒ milliseconds }
+
import akka.actor.{ ActorLogging, Actor }
+import akka.event.LoggingAdapter
+import org.slf4j.LoggerFactory
import spray.json._
-import scala.concurrent.Future
+import scala.concurrent.{ ExecutionContext, Future }
import spray.httpx.{ SprayJsonSupport, RequestBuilding, ResponseTransformation }
import spray.httpx.encoding.Deflate
import spray.http._
import spray.json.lenses.JsonLenses._
-import akka.pattern.pipe
import java.lang.management.ManagementFactory
import spray.client.pipelining._
-import scala.util.control.NonFatal
+import scala.util.{ Failure, Success }
import spray.http.Uri.Query
import kamon.newrelic.MetricTranslator.TimeSliceMetrics
+import scala.concurrent.duration._
class Agent extends Actor with RequestBuilding with ResponseTransformation with SprayJsonSupport with ActorLogging {
+
import context.dispatcher
import Agent._
+ import Retry._
+
+ self ! Initialize
val agentInfo = {
val config = context.system.settings.config.getConfig("kamon.newrelic")
@@ -40,8 +49,10 @@ class Agent extends Actor with RequestBuilding with ResponseTransformation with
// Name has the format of pid@host
val runtimeName = ManagementFactory.getRuntimeMXBean.getName.split('@')
+ val retryDelay = FiniteDuration(config.getDuration("retry-delay", milliseconds), milliseconds)
+ val maxRetry = config.getInt("max-retry")
- AgentInfo(licenseKey, appName, runtimeName(1), runtimeName(0).toInt)
+ AgentInfo(licenseKey, appName, runtimeName(1), runtimeName(0).toInt, maxRetry, retryDelay)
}
val baseQuery = Query(
@@ -49,33 +60,36 @@ class Agent extends Actor with RequestBuilding with ResponseTransformation with
"marshal_format" -> "json",
"protocol_version" -> "12")
- def receive = {
- case Initialize(runId, collector) ⇒
- log.info("Agent initialized with runID: [{}] and collector: [{}]", runId, collector)
- context become reporting(runId, collector)
+ def receive: Receive = uninitialized
+
+ def uninitialized: Receive = {
+ case Initialize ⇒ {
+ connectToCollector onComplete {
+ case Success(agent) ⇒ {
+ log.info("Agent initialized with runID: [{}] and collector: [{}]", agent.runId, agent.collector)
+ context become reporting(agent.runId, agent.collector)
+ }
+ case Failure(reason) ⇒ self ! InitializationFailed(reason)
+ }
+ }
+ case InitializationFailed(reason) ⇒ {
+ log.info("Initialization failed: {}, retrying in {} seconds", reason.getMessage, agentInfo.retryDelay.toSeconds)
+ context.system.scheduler.scheduleOnce(agentInfo.retryDelay, self, Initialize)
+ }
+ case everythingElse ⇒ //ignore
}
def reporting(runId: Long, collector: String): Receive = {
case metrics: TimeSliceMetrics ⇒ sendMetricData(runId, collector, metrics)
}
- override def preStart(): Unit = {
- super.preStart()
- initialize
- }
-
- def initialize: Unit = {
- pipe({
- for (
- collector ← selectCollector;
- runId ← connect(collector, agentInfo)
- ) yield Initialize(runId, collector)
- } recover {
- case NonFatal(ex) ⇒ InitializationFailed(ex)
- }) to self
- }
+ def connectToCollector: Future[Initialized] = for {
+ collector ← selectCollector
+ runId ← connect(collector, agentInfo)
+ } yield Initialized(runId, collector)
import AgentJsonProtocol._
+
val compressedPipeline: HttpRequest ⇒ Future[HttpResponse] = encode(Deflate) ~> sendReceive
val compressedToJsonPipeline: HttpRequest ⇒ Future[JsValue] = compressedPipeline ~> toJson
@@ -111,19 +125,49 @@ class Agent extends Actor with RequestBuilding with ResponseTransformation with
val query = ("method" -> "metric_data") +: ("run_id" -> runId.toString) +: baseQuery
val sendMetricDataUri = Uri(s"http://$collector/agent_listener/invoke_raw_method").withQuery(query)
- compressedPipeline {
- Post(sendMetricDataUri, MetricData(runId, metrics))
+ withMaxAttempts(agentInfo.maxRetry, metrics, log) { currentMetrics ⇒
+ compressedPipeline {
+ log.info("Sending metrics to NewRelic collector")
+ Post(sendMetricDataUri, MetricData(runId, currentMetrics))
+ }
}
}
-
}
object Agent {
-
- case class Initialize(runId: Long, collector: String)
+ case class Initialize()
+ case class Initialized(runId: Long, collector: String)
case class InitializationFailed(reason: Throwable)
case class CollectorSelection(return_value: String)
- case class AgentInfo(licenseKey: String, appName: String, host: String, pid: Int)
-
+ case class AgentInfo(licenseKey: String, appName: String, host: String, pid: Int, maxRetry: Int = 0, retryDelay: FiniteDuration)
case class MetricData(runId: Long, timeSliceMetrics: TimeSliceMetrics)
+}
+
+object Retry {
+
+ @volatile private var attempts: Int = 0
+ @volatile private var pendingMetrics: Option[TimeSliceMetrics] = None
+
+ def withMaxAttempts[T](maxRetry: Int, metrics: TimeSliceMetrics, log: LoggingAdapter)(block: TimeSliceMetrics ⇒ Future[T])(implicit executor: ExecutionContext): Unit = {
+
+ val currentMetrics = metrics.merge(pendingMetrics)
+
+ if (currentMetrics.metrics.nonEmpty) {
+ block(currentMetrics) onComplete {
+ case Success(_) ⇒
+ pendingMetrics = None
+ attempts = 0
+ case Failure(_) ⇒
+ attempts += 1
+ if (maxRetry > attempts) {
+ log.info("Trying to send metrics to NewRelic collector, attempt [{}] of [{}]", attempts, maxRetry)
+ pendingMetrics = Some(currentMetrics)
+ } else {
+ log.info("Max attempts achieved, proceeding to remove all pending metrics")
+ pendingMetrics = None
+ attempts = 0
+ }
+ }
+ }
+ }
} \ No newline at end of file
diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala
index ef556e11..9b3e6dea 100644
--- a/kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala
+++ b/kamon-newrelic/src/main/scala/kamon/newrelic/AgentJsonProtocol.scala
@@ -33,7 +33,7 @@ object AgentJsonProtocol extends DefaultJsonProtocol {
}
implicit def seqWriter[T: JsonWriter] = new JsonWriter[Seq[T]] {
- def write(seq: Seq[T]) = JsArray(seq.map(_.toJson).toList)
+ def write(seq: Seq[T]) = JsArray(seq.map(_.toJson).toVector)
}
implicit object MetricDetailWriter extends JsonWriter[NewRelic.Metric] {
@@ -58,6 +58,6 @@ object AgentJsonProtocol extends DefaultJsonProtocol {
JsNumber(obj.runId),
JsNumber(obj.timeSliceMetrics.from),
JsNumber(obj.timeSliceMetrics.to),
- obj.timeSliceMetrics.metrics.toJson)
+ obj.timeSliceMetrics.metrics.values.toSeq.toJson)
}
}
diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/MetricTranslator.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/MetricTranslator.scala
index a3bb6311..5fa571e1 100644
--- a/kamon-newrelic/src/main/scala/kamon/newrelic/MetricTranslator.scala
+++ b/kamon-newrelic/src/main/scala/kamon/newrelic/MetricTranslator.scala
@@ -28,14 +28,21 @@ class MetricTranslator(receiver: ActorRef) extends Actor
val fromInSeconds = (from / 1E3).toInt
val toInSeconds = (to / 1E3).toInt
val allMetrics = collectWebTransactionMetrics(metrics) ++ collectCustomMetrics(metrics)
+ val groupedMetrics: Map[String, NewRelic.Metric] = allMetrics.map(metric ⇒ metric.name -> metric)(collection.breakOut) // avoid intermediate tuple
- receiver ! TimeSliceMetrics(fromInSeconds, toInSeconds, allMetrics)
+ receiver ! TimeSliceMetrics(fromInSeconds, toInSeconds, groupedMetrics)
}
}
object MetricTranslator {
- case class TimeSliceMetrics(from: Long, to: Long, metrics: Seq[NewRelic.Metric])
+ case class TimeSliceMetrics(from: Long, to: Long, metrics: Map[String, NewRelic.Metric]) {
+ import kamon.metric._
+
+ def merge(thatMetrics: Option[TimeSliceMetrics]): TimeSliceMetrics = {
+ thatMetrics.map(that ⇒ TimeSliceMetrics(from + that.from, to + that.to, combineMaps(metrics, that.metrics)((l, r) ⇒ l.merge(r)))).getOrElse(this)
+ }
+ }
def props(receiver: ActorRef): Props = Props(new MetricTranslator(receiver))
}
diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala
index bdac5298..92e673ee 100644
--- a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala
+++ b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelic.scala
@@ -1,27 +1,28 @@
-/* ===================================================
- * Copyright © 2013 the kamon project <http://kamon.io/>
+/*
+ * =========================================================================================
+ * 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
+ * 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
+ * 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.
- * ========================================================== */
+ * 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._
-import kamon.metric.UserMetrics.{ UserGauges, UserMinMaxCounters, UserCounters, UserHistograms }
-import scala.concurrent.duration._
import kamon.Kamon
-import kamon.metric.{ UserMetrics, TickMetricSnapshotBuffer, TraceMetrics, Metrics }
import kamon.metric.Subscriptions.TickMetricSnapshot
-import akka.actor
+import kamon.metric.UserMetrics.{ UserCounters, UserGauges, UserHistograms, UserMinMaxCounters }
+import kamon.metric.{ Metrics, TickMetricSnapshotBuffer, TraceMetrics }
+
+import scala.concurrent.duration._
class NewRelicExtension(system: ExtendedActorSystem) extends Kamon.Extension {
val config = system.settings.config.getConfig("kamon.newrelic")
diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala
index 4203f81f..08b5df99 100644
--- a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala
+++ b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala
@@ -1,23 +1,23 @@
-/* ===================================================
- * Copyright © 2013 the kamon project <http://kamon.io/>
+/*
+ * =========================================================================================
+ * 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
+ * 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
+ * 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.
- * ========================================================== */
+ * 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.{ ActorLogging, Actor }
-import akka.event.Logging.Error
-import akka.event.Logging.{ LoggerInitialized, InitializeLogger }
+import akka.actor.{ Actor, ActorLogging }
+import akka.event.Logging.{ Error, InitializeLogger, LoggerInitialized }
import com.newrelic.api.agent.{ NewRelic ⇒ NR }
import kamon.trace.TraceContextAware
@@ -33,17 +33,8 @@ class NewRelicErrorLogger extends Actor with ActorLogging {
def notifyError(error: Error): Unit = {
val params = new java.util.HashMap[String, String]()
- if (error.isInstanceOf[TraceContextAware]) {
- val ctx = error.asInstanceOf[TraceContextAware].traceContext
-
- for (c ← ctx) {
- params.put("TraceToken", c.token)
- }
- } else if (!aspectJMissingAlreadyReported) {
- log.warning("ASPECTJ WEAVER MISSING. You might have missed to include the javaagent JVM startup parameter in" +
- " your application. Please refer to http://kamon.io/get-started/ for instructions on how to do it.")
- aspectJMissingAlreadyReported = true
- }
+ val ctx = error.asInstanceOf[TraceContextAware].traceContext
+ params.put("TraceToken", ctx.token)
if (error.cause == Error.NoCause) {
NR.noticeError(error.message.toString, params)