Skip to content

Commit

Permalink
Slf4j to log exceptions in user-supplied callbacks (close #153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Streeter authored and istreeter committed Dec 23, 2020
1 parent 0cdf3f7 commit c8f4db0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ lazy val idEmitter = project
name := "snowplow-scala-tracker-emitter-id",
libraryDependencies ++= Seq(
Dependencies.Libraries.scalajHttp,
Dependencies.Libraries.slf4jApi
)
))
.dependsOn(core % "test->test;compile->compile")
Expand All @@ -75,7 +76,8 @@ lazy val http4sEmitter = project
.settings(Seq(
name := "snowplow-scala-tracker-emitter-http4s",
libraryDependencies ++= List(
Dependencies.Libraries.http4sClient
Dependencies.Libraries.http4sClient,
Dependencies.Libraries.slf4jApi
)
))
.dependsOn(core % "test->test;compile->compile")
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import fs2.concurrent.{Dequeue, Enqueue, Queue}
import org.http4s.client.Client
import org.http4s.headers.`Content-Type`
import org.http4s.{MediaType, Method, Request => HttpRequest, Uri}
import org.slf4j.LoggerFactory
import scala.concurrent.duration._
import scala.util.Random

Expand Down Expand Up @@ -183,13 +184,21 @@ object Http4sEmitter {
.handleError(Result.TrackerFailure(_))
}

private def invokeCallback[F[_]: MonadError[?[_], Throwable]](callback: Option[Callback[F]],
collector: EndpointParams,
request: Request,
result: Result): F[Unit] =
lazy val logger = LoggerFactory.getLogger(getClass.getName)

private def invokeCallback[F[_]: Sync](callback: Option[Callback[F]],
collector: EndpointParams,
request: Request,
result: Result): F[Unit] =
callback match {
case Some(cb) =>
cb(collector, request, result).handleError(_ => ())
cb(collector, request, result).handleErrorWith { throwable =>
if (logger.isWarnEnabled)
Sync[F].delay(
logger.warn(s"Snowplow Tracker bounded to ${collector.getUri} failed to execute callback", throwable))
else
Sync[F].unit
}
case None => Monad[F].unit
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.snowplowanalytics.snowplow.scalatracker.emitters.id

import org.slf4j.LoggerFactory
import scala.concurrent.{ExecutionContext, Future, blocking}
import scala.util.{Failure, Random, Success, Try}
import scala.util.control.NonFatal
Expand Down Expand Up @@ -51,6 +52,8 @@ private[id] object RequestProcessor {
.options(options)
}

lazy val logger = LoggerFactory.getLogger(getClass.getName)

/**
* Executes user-provided callback against event and collector response
* If callback fails to execute - message will be printed to stderr
Expand All @@ -67,9 +70,8 @@ private[id] object RequestProcessor {
cb(collector, payload, result)
} catch {
case NonFatal(throwable) =>
val error = Option(throwable.getMessage).getOrElse(throwable.toString)
val message = s"Snowplow Tracker bounded to ${collector.getUri} failed to execute callback: $error"
System.err.println(message)
if (logger.isWarnEnabled)
logger.warn(s"Snowplow Tracker bounded to ${collector.getUri} failed to execute callback", throwable)
}
}

Expand Down
6 changes: 6 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ object Dependencies {
val catsEffect = "2.2.0"
val http4s = "0.21.14"

// Java
val slf4j = "1.7.30"

// Scala (test only)
val specs2 = "4.10.5"
val scalaCheck = "1.14.3"
Expand All @@ -37,6 +40,9 @@ object Dependencies {
val catsEffect = "org.typelevel" %% "cats-effect" % V.catsEffect
val http4sClient = "org.http4s" %% "http4s-client" % V.http4s

// Java
val slf4jApi = "org.slf4j" % "slf4j-api" % V.slf4j

// Scala (test only)
val specs2 = "org.specs2" %% "specs2-core" % V.specs2 % "test"
val scalaCheck = "org.scalacheck" %% "scalacheck" % V.scalaCheck % "test"
Expand Down

0 comments on commit c8f4db0

Please sign in to comment.