Skip to content

Commit

Permalink
Update scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wsargent committed May 10, 2020
1 parent 1f08ccd commit ccfa199
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 36 deletions.
7 changes: 6 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
version = 2.5.0-RC2
version = 2.5.2
align = more
maxColumn = 100 // For my wide 30" display.
# align.openParenDefnSite = true
docstrings = JavaDoc
version = 2.4.1
9 changes: 6 additions & 3 deletions app/controllers/HomeController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import play.api.mvc._
* application's home page.
*/
@Singleton
class HomeController @Inject()(val controllerComponents: ControllerComponents)
extends BaseController with HoneycombImplicits {
class HomeController @Inject() (val controllerComponents: ControllerComponents)
extends BaseController
with HoneycombImplicits {

def index(): Action[AnyContent] = Action { implicit request: Request[AnyContent] =>
implicit val logger: Logger = getLogger(request)
Expand All @@ -51,7 +52,9 @@ class HomeController @Inject()(val controllerComponents: ControllerComponents)

// https://docs.honeycomb.io/working-with-your-data/tracing/send-trace-data/#span-events
def eventLogger(implicit spanInfo: SpanInfo): Logger = {
val eventInfo = EventInfo.builder().setName(spanInfo.duration().toString)
val eventInfo = EventInfo
.builder()
.setName(spanInfo.duration().toString)
.setParentId(spanInfo.spanId())
.setTraceId(spanInfo.traceId())
.build()
Expand Down
21 changes: 13 additions & 8 deletions app/logging/HoneycombFlowBehavior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import org.slf4j.event.Level
import scala.collection.mutable
import scala.compat.java8.FunctionConverters._

class HoneycombFlowBehavior[B: ToArguments](implicit spanInfo: SpanInfo)
extends FlowBehavior[B] {
class HoneycombFlowBehavior[B: ToArguments](implicit spanInfo: SpanInfo) extends FlowBehavior[B] {
import HoneycombFlowBehavior._

// Create a thread local stack of span info.
Expand All @@ -38,31 +37,37 @@ class HoneycombFlowBehavior[B: ToArguments](implicit spanInfo: SpanInfo)
None
}

override def throwingStatement(throwable: Throwable, source: Source): Option[(Level, Statement)] = {
override def throwingStatement(
throwable: Throwable,
source: Source
): Option[(Level, Statement)] = {
val span = popCurrentSpan
Some {
(Level.ERROR,
(
Level.ERROR,
Statement()
.withThrowable(throwable)
.withMarkers(Markers(spanMarkerFactory(span)))
.withMessage(s"${source.enclosing.value} exception, duration ${span.duration()}"))
.withMessage(s"${source.enclosing.value} exception, duration ${span.duration()}")
)
}
}

override def exitStatement(resultValue: B, source: Source): Option[Statement] = Some {
val span = popCurrentSpan
Statement()
.withMarkers(Markers(spanMarkerFactory(span)))
.withMessage(s"${source.enclosing.value} exit with result {}, start time = ${span.startTime()}, duration ${span.duration()}")
.withMessage(s"${source.enclosing.value} exit with result {}, start time = ${span
.startTime()}, duration ${span.duration()}")
.withArguments(resultValue)
}

private def pushCurrentSpan(spanInfo: SpanInfo): Unit = threadLocalStack.get.push(spanInfo)
private def popCurrentSpan: SpanInfo = threadLocalStack.get().pop()
private def popCurrentSpan: SpanInfo = threadLocalStack.get().pop()
}

object HoneycombFlowBehavior {
val spanMarkerFactory = new SpanMarkerFactory
val spanMarkerFactory = new SpanMarkerFactory
val eventMarkerFactory = new EventMarkerFactory

type SpanStack = mutable.Stack[SpanInfo]
Expand Down
2 changes: 1 addition & 1 deletion app/logging/HoneycombImplicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ trait HoneycombImplicits extends com.tersesystems.blindsight.logstash.Implicits
}
}

object HoneycombImplicits extends HoneycombImplicits
object HoneycombImplicits extends HoneycombImplicits
10 changes: 6 additions & 4 deletions app/logging/LoggingFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import play.api.mvc.{EssentialAction, EssentialFilter}

import scala.concurrent.ExecutionContext

class LoggingFilter @Inject()(implicit ec: ExecutionContext) extends EssentialFilter {
class LoggingFilter @Inject() (implicit ec: ExecutionContext) extends EssentialFilter {

override def apply(next: EssentialAction): EssentialAction = EssentialAction { request =>
import logging.HoneycombFlowBehavior._
Expand All @@ -30,9 +30,11 @@ class LoggingFilter @Inject()(implicit ec: ExecutionContext) extends EssentialFi
next(request).map { result =>
// The root span has to be logged _last_, after the child spans.
val rootSpan = request.attrs(Attrs.spanInfo)
val logger = getLogger(request)
logger.info(spanMarkerFactory(rootSpan),
s"${rootSpan.name()} exit, duration ${rootSpan.duration()}")
val logger = getLogger(request)
logger.info(
spanMarkerFactory(rootSpan),
s"${rootSpan.name()} exit, duration ${rootSpan.duration()}"
)
result
}
}
Expand Down
25 changes: 16 additions & 9 deletions app/logging/LoggingHttpErrorHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@ import play.api.mvc.RequestHeader
import play.api.routing.Router
import play.api.{Configuration, Environment, OptionalSourceMapper, UsefulException}

class LoggingHttpErrorHandler @Inject()(environment: Environment,
configuration: Configuration,
sourceMapper: OptionalSourceMapper,
router: Provider[Router])
extends DefaultHttpErrorHandler(environment, configuration, sourceMapper, router) {
class LoggingHttpErrorHandler @Inject() (
environment: Environment,
configuration: Configuration,
sourceMapper: OptionalSourceMapper,
router: Provider[Router]
) extends DefaultHttpErrorHandler(environment, configuration, sourceMapper, router) {

override protected def logServerError(request: RequestHeader, usefulException: UsefulException): Unit = {
override protected def logServerError(
request: RequestHeader,
usefulException: UsefulException
): Unit = {
import com.tersesystems.logback.tracing.SpanInfo
import HoneycombFlowBehavior.spanMarkerFactory
import HoneycombImplicits._

val logger = getLogger(request)
val logger = getLogger(request)
implicit val rootSpan: SpanInfo = request.attrs(Attrs.spanInfo)
logger.error(spanMarkerFactory(rootSpan),
s"${rootSpan.name()} exception, duration ${rootSpan.duration()}: {}", usefulException.cause)
logger.error(
spanMarkerFactory(rootSpan),
s"${rootSpan.name()} exception, duration ${rootSpan.duration()}: {}",
usefulException.cause
)
}
}
32 changes: 23 additions & 9 deletions app/logging/SpanInfoApplicationLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,36 @@ import play.api.inject.guice.{GuiceApplicationLoader, GuiceableModule}
import play.api.inject.{SimpleModule, bind}
import play.api.libs.typedmap.{TypedKey, TypedMap}
import play.api.mvc.request.{DefaultRequestFactory, RemoteConnection, RequestFactory, RequestTarget}
import play.api.mvc.{CookieHeaderEncoding, FlashCookieBaker, Headers, RequestHeader, SessionCookieBaker}
import play.api.mvc.{
CookieHeaderEncoding,
FlashCookieBaker,
Headers,
RequestHeader,
SessionCookieBaker
}

class SpanInfoApplicationLoader extends GuiceApplicationLoader {
class SpanInfoApplicationLoader extends GuiceApplicationLoader {
override protected def overrides(context: ApplicationLoader.Context): Seq[GuiceableModule] = {
super.overrides(context) :+ GuiceableModule.guiceable(new SpanInfoModule)
}
}

class SpanInfoModule extends SimpleModule(bind[RequestFactory] to classOf[SpanInfoRequestFactory])

class SpanInfoRequestFactory @Inject()(
cookieHeaderEncoding: CookieHeaderEncoding,
sessionBaker: SessionCookieBaker,
flashBaker: FlashCookieBaker
class SpanInfoRequestFactory @Inject() (
cookieHeaderEncoding: CookieHeaderEncoding,
sessionBaker: SessionCookieBaker,
flashBaker: FlashCookieBaker
) extends DefaultRequestFactory(cookieHeaderEncoding, sessionBaker, flashBaker) {

override def createRequestHeader(connection: RemoteConnection, method: String, target: RequestTarget, version: String, headers: Headers, attrs: TypedMap): RequestHeader = {
override def createRequestHeader(
connection: RemoteConnection,
method: String,
target: RequestTarget,
version: String,
headers: Headers,
attrs: TypedMap
): RequestHeader = {
val request = super.createRequestHeader(connection, method, target, version, headers, attrs)
request.addAttr(Attrs.spanInfo, SpanInfoRequestFactory.rootSpan(request))
}
Expand All @@ -46,7 +59,8 @@ object SpanInfoRequestFactory {

private val idgen = new RandomUUIDIdGenerator
private val builder = {
SpanInfo.builder()
SpanInfo
.builder()
.setServiceName("play-blindsight")
.setIdGenerator(() => idgen.generateId())
}
Expand All @@ -59,4 +73,4 @@ object SpanInfoRequestFactory {
.setName(s"${request.method} ${request.path}")
.buildNow
}
}
}
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ libraryDependencies += "com.tersesystems.logback" % "logback-turbomarker" % ters
libraryDependencies += "com.tersesystems.logback" % "logback-honeycomb-appender" % terseLogback
libraryDependencies += "com.tersesystems.logback" %% "logback-honeycomb-playws" % terseLogback

libraryDependencies += "com.tersesystems.blindsight" %% "blindsight-logstash" % "0.1.0"
libraryDependencies += "com.tersesystems.blindsight" %% "blindsight-logstash" % "0.1.35"

0 comments on commit ccfa199

Please sign in to comment.