-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from orrsella/exceeded-timeout-params
Add exceededTimeoutParams function to execution parameters to allow custom values reporting
- Loading branch information
Showing
5 changed files
with
103 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
package com.wix.async | ||
|
||
import org.slf4j.LoggerFactory | ||
import com.twitter.util.TimeoutException | ||
import com.wix.async.FuturePerfect._ | ||
import org.slf4j.LoggerFactory | ||
import scala.concurrent.duration.Duration | ||
|
||
/** | ||
* @author shaiyallin | ||
* @since 12/6/13 | ||
*/ | ||
|
||
trait LoggerReporting { this: Reporting[Event] => | ||
trait LoggerReporting extends LoggerReportingMessages { this: Reporting[Event] => | ||
|
||
private val log = LoggerFactory.getLogger(getClass) | ||
|
||
listenFor { | ||
case Retrying(timeout, remainingRetries, executionName) => log.warn(s"Execution [$executionName] timed out after ${timeout.toMillis} ms, retrying $remainingRetries more times.") | ||
case GaveUp(timeout, _, executionName) => log.error(s"Execution [$executionName] timed out after ${timeout.toMillis} ms, giving up.") | ||
case ExceededTimeout(actual, executionName) => log.error(s"Execution [$executionName] timed out, actual duration was ${actual.toMillis} ms.") | ||
case TimeSpentInQueue(time, executionName) => log.info(s"Execution [$executionName] started after spending ${time.toMillis} ms in queue.") | ||
case Successful(time, executionName) => log.info(s"Execution [$executionName] succeeded after ${time.toMillis} ms.") | ||
case Failed(time, error, executionName) => log.error(s"Execution [$executionName] failed after ${time.toMillis} ms.", error) | ||
case TimeoutWhileInQueue(time, error, executionName) => log.error(s"Execution [$executionName] timed out after waiting ${time.toMillis} in queue.", error) | ||
case Retrying(timeout, remainingRetries, executionName) => log.warn(retrying(timeout, remainingRetries, executionName)) | ||
case GaveUp(timeout, error, executionName) => log.error(gaveUp(timeout, error, executionName)) | ||
case ExceededTimeout(actual, executionName, params) => log.error(exceededTimeout(actual, executionName, params)) | ||
case TimeSpentInQueue(time, executionName) => log.info(timeSpentInQueue(time, executionName)) | ||
case Successful(elapsed, executionName, params) => log.info(successful(elapsed, executionName, params)) | ||
case Failed(elapsed, error, executionName) => log.error(failed(elapsed, error, executionName), error) | ||
case TimeoutWhileInQueue(timeInQueue, error, executionName) => log.error(timeoutWhileInQueue(timeInQueue, error, executionName), error) | ||
} | ||
} | ||
|
||
trait LoggerReportingMessages { | ||
def retrying(timeout: Duration, remainingRetries: Long, executionName: String) = s"Execution [$executionName] timed out after ${timeout.toMillis} ms, retrying $remainingRetries more times." | ||
def gaveUp(timeout: Duration, e: TimeoutException, executionName: String) = s"Execution [$executionName] timed out after ${timeout.toMillis} ms, giving up." | ||
def exceededTimeout(actual: Duration, executionName: String, params: Map[String, String]) = s"Execution [$executionName] timed out, actual duration was ${actual.toMillis} ms.${formatParams(params)}" | ||
def timeSpentInQueue(time: Duration, executionName: String) = s"Execution [$executionName] started after spending ${time.toMillis} ms in queue." | ||
def successful(elapsed: Duration, executionName: String, params: Map[String, String]) = s"Execution [$executionName] succeeded after ${elapsed.toMillis} ms.${formatParams(params)}" | ||
def failed(elapsed: Duration, error: Throwable, executionName: String) = s"Execution [$executionName] failed after ${elapsed.toMillis} ms." | ||
def timeoutWhileInQueue(timeInQueue: Duration, e: TimeoutException, executionName: String) = s"Execution [$executionName] timed out after waiting ${timeInQueue.toMillis} in queue." | ||
|
||
private def formatParams(params: Map[String, String]) = | ||
if (params.nonEmpty) { | ||
val p = params.map(p => s"${p._1}=[${p._2}]") | ||
s" Additional params: ${p.mkString(", ")}" | ||
} else "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/scala/com/wix/async/LoggerReportingMessagesTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.wix.async | ||
|
||
import org.specs2.mutable.SpecificationWithJUnit | ||
import org.specs2.specification.Scope | ||
import org.specs2.time.NoTimeConversions | ||
import scala.concurrent.duration._ | ||
|
||
class LoggerReportingMessagesTest extends SpecificationWithJUnit with NoTimeConversions { | ||
|
||
trait Context extends Scope with LoggerReportingMessages | ||
|
||
"logger reporting messages" should { | ||
"correctly format params in ExceededTimeout message with params" in new Context { | ||
val duration = 1 second | ||
val name = "exec name" | ||
val params = Map("Value1" -> "10.0", "Value2" -> "example.com") | ||
val message = exceededTimeout(duration, name, params) | ||
message must_== s"Execution [$name] timed out, actual duration was ${duration.toMillis} ms. Additional params: Value1=[10.0], Value2=[example.com]" | ||
} | ||
|
||
"correctly format params in ExceededTimeout message without params" in new Context { | ||
val duration = 1 second | ||
val name = "exec name" | ||
val message = exceededTimeout(duration, name, Map()) | ||
message must_== s"Execution [$name] timed out, actual duration was ${duration.toMillis} ms." | ||
} | ||
} | ||
} |