aboutsummaryrefslogtreecommitdiff
path: root/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala
diff options
context:
space:
mode:
authorColin Smith <smithcolina@hotmail.com>2015-10-14 08:17:03 +0100
committerColin Smith <smithcolina@hotmail.com>2015-10-14 09:01:33 +0100
commitee009e02a20a94ee6011cf5ddf537722c173e44f (patch)
treed76c0bfc8c7636b7b65fc462686f0befe2aabfe7 /kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala
parent91859be4df789c64ae30ef879b9c6c83503b99c2 (diff)
downloadKamon-ee009e02a20a94ee6011cf5ddf537722c173e44f.tar.gz
Kamon-ee009e02a20a94ee6011cf5ddf537722c173e44f.tar.bz2
Kamon-ee009e02a20a94ee6011cf5ddf537722c173e44f.zip
= newrelic: Associate logged errors with correct transaction
Previously, errors were associated with transactions named "OtherTransaction/<trace-name>", with this change they are associated with "WebTransaction/Uri/<trace-name" which is the name expected by NewRelic. The additional complexity of wrapping messages in LoggedError instances ensures that the logged message is not repeated in the New Relic UI.
Diffstat (limited to 'kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala')
-rw-r--r--kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala47
1 files changed, 31 insertions, 16 deletions
diff --git a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala
index c1601fa8..0076b668 100644
--- a/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala
+++ b/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicErrorLogger.scala
@@ -17,11 +17,12 @@
package kamon.newrelic
import java.util
-
import akka.actor.{ Actor, ActorLogging }
import akka.event.Logging.{ Error, InitializeLogger, LoggerInitialized }
-import com.newrelic.api.agent.{ NewRelic ⇒ NR }
+import com.newrelic.agent.errors.{ ThrowableError }
+import com.newrelic.agent.service.ServiceFactory
import kamon.trace.{ Tracer, TraceContextAware }
+import scala.util.control.NoStackTrace
trait CustomParamsSupport {
this: NewRelicErrorLogger ⇒
@@ -38,24 +39,38 @@ class NewRelicErrorLogger extends Actor with ActorLogging with CustomParamsSuppo
case anythingElse ⇒
}
- def notifyError(error: Error): Unit = runInFakeTransaction {
+ def notifyError(error: Error): Unit = {
val params = new util.HashMap[String, String]()
+ customParams foreach { case (k, v) ⇒ params.put(k, v) }
- if (error.isInstanceOf[TraceContextAware]) {
- val ctx = error.asInstanceOf[TraceContextAware].traceContext
- params put ("TraceToken", ctx.token)
+ params.put("LogSource", error.logSource)
+ params.put("LogClass", error.logClass.toString)
+ error match {
+ case e: TraceContextAware ⇒ params.put("TraceToken", e.traceContext.token)
+ case _ ⇒
}
- customParams foreach { case (k, v) ⇒ params.put(k, v) }
-
- if (error.cause == Error.NoCause) NR.noticeError(error.message.toString, params)
- else NR.noticeError(error.cause, params)
+ if (error.cause == Error.NoCause)
+ reportError(loggedMessage(error.message.toString, params))
+ else
+ reportError(loggedException(error.message.toString, error.cause, params))
}
- //Really ugly, but temporal hack until next release...
- def runInFakeTransaction[T](thunk: ⇒ T): T = {
- val oldName = Thread.currentThread.getName
- Thread.currentThread.setName(Tracer.currentContext.name)
- try thunk finally Thread.currentThread.setName(oldName)
+ def loggedMessage(message: String, params: util.HashMap[String, String]) =
+ loggedException("", LoggedMessage(message), params)
+
+ def loggedException(message: String, cause: Throwable, params: util.HashMap[String, String]) = {
+ if (null != message && message.length > 0) params.put("ErrorMessage", message)
+ val uri = s"/${Tracer.currentContext.name}"
+ val transaction = s"WebTransaction/Uri$uri"
+ LoggedException(cause, params, transaction, uri);
}
-} \ No newline at end of file
+
+ def reportError(error: ThrowableError) = ServiceFactory.getRPMService().getErrorService().reportError(error)
+
+}
+
+case class LoggedMessage(message: String) extends Throwable(message) with NoStackTrace
+
+case class LoggedException(cause: Throwable, params: util.HashMap[String, String], transaction: String, uri: String)
+ extends ThrowableError(null, transaction, cause, uri, System.currentTimeMillis(), null, null, null, params, null)