diff options
Diffstat (limited to 'src/main/scala/xyz/driver')
-rw-r--r-- | src/main/scala/xyz/driver/core/app.scala | 35 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/core/rest.scala | 19 |
2 files changed, 40 insertions, 14 deletions
diff --git a/src/main/scala/xyz/driver/core/app.scala b/src/main/scala/xyz/driver/core/app.scala index e66fe73..dd44295 100644 --- a/src/main/scala/xyz/driver/core/app.scala +++ b/src/main/scala/xyz/driver/core/app.scala @@ -35,7 +35,8 @@ import scalaz.syntax.equal._ object app { - class DriverApp(version: String, + class DriverApp(appName: String, + version: String, gitHash: String, modules: Seq[Module], time: TimeProvider = new SystemTimeProvider(), @@ -82,14 +83,11 @@ object app { { ctx => val trackingId = rest.extractTrackingId(ctx.request) MDC.put("trackingId", trackingId) - MDC.put("origin", origin) - MDC.put("xForwardedFor", - extractHeader(ctx.request)("x-forwarded-for") - .orElse(extractHeader(ctx.request)("x_forwarded_for")) - .getOrElse("unknown")) - MDC.put("remoteAddress", extractHeader(ctx.request)("remote-address").getOrElse("unknown")) - MDC.put("userAgent", extractHeader(ctx.request)("user-agent").getOrElse("unknown")) - MDC.put("ip", ip.toOption.map(_.getHostAddress).getOrElse("unknown")) + + val updatedStacktrace = (rest.extractStacktrace(ctx.request) ++ Array(appName)).mkString("->") + MDC.put("stack", updatedStacktrace) + + storeRequestContextToMdc(ctx.request, origin, ip) def requestLogging: Future[Unit] = Future { log.info( @@ -97,7 +95,10 @@ object app { } val contextWithTrackingId = - ctx.withRequest(ctx.request.addHeader(RawHeader(ContextHeaders.TrackingIdHeader, trackingId))) + ctx.withRequest( + ctx.request + .addHeader(RawHeader(ContextHeaders.TrackingIdHeader, trackingId)) + .addHeader(RawHeader(ContextHeaders.StacktraceHeader, updatedStacktrace))) handleExceptions(ExceptionHandler(exceptionHandler))({ c => requestLogging.flatMap { _ => @@ -115,6 +116,20 @@ object app { } } + private def storeRequestContextToMdc(request: HttpRequest, origin: String, ip: RemoteAddress) = { + + MDC.put("origin", origin) + MDC.put("ip", ip.toOption.map(_.getHostAddress).getOrElse("unknown")) + MDC.put("remoteHost", ip.toOption.map(_.getHostName).getOrElse("unknown")) + + MDC.put("xForwardedFor", + extractHeader(request)("x-forwarded-for") + .orElse(extractHeader(request)("x_forwarded_for")) + .getOrElse("unknown")) + MDC.put("remoteAddress", extractHeader(request)("remote-address").getOrElse("unknown")) + MDC.put("userAgent", extractHeader(request)("user-agent").getOrElse("unknown")) + } + protected def swaggerOverride(apiTypes: Seq[Type]) = { new Swagger(baseUrl, Scheme.forValue(scheme), version, actorSystem, apiTypes, config) { override def generateSwaggerJson: String = { diff --git a/src/main/scala/xyz/driver/core/rest.scala b/src/main/scala/xyz/driver/core/rest.scala index 6944cf7..15c6d83 100644 --- a/src/main/scala/xyz/driver/core/rest.scala +++ b/src/main/scala/xyz/driver/core/rest.scala @@ -21,6 +21,7 @@ import io.swagger.models.Scheme import pdi.jwt.{Jwt, JwtAlgorithm} import xyz.driver.core.auth._ import xyz.driver.core.time.provider.TimeProvider +import org.slf4j.MDC import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success} @@ -44,10 +45,13 @@ package rest { .fold(java.util.UUID.randomUUID.toString)(_.value()) } + def extractStacktrace(request: HttpRequest): Array[String] = + request.headers.find(_.name == ContextHeaders.StacktraceHeader).fold("")(_.value()).split("->") + def extractContextHeaders(request: HttpRequest): Map[String, String] = { request.headers.filter { h => h.name === ContextHeaders.AuthenticationTokenHeader || h.name === ContextHeaders.TrackingIdHeader || - h.name === ContextHeaders.PermissionsTokenHeader + h.name === ContextHeaders.PermissionsTokenHeader || h.name === ContextHeaders.StacktraceHeader } map { header => if (header.name === ContextHeaders.AuthenticationTokenHeader) { header.name -> header.value.stripPrefix(ContextHeaders.AuthenticationHeaderPrefix).trim @@ -155,6 +159,7 @@ package rest { val PermissionsTokenHeader = "Permissions" val AuthenticationHeaderPrefix = "Bearer" val TrackingIdHeader = "X-Trace" + val StacktraceHeader = "X-Stacktrace" } object AuthProvider { @@ -358,9 +363,15 @@ package rest { val requestTime = time.currentTime() val request = requestStub - .withHeaders(RawHeader(ContextHeaders.TrackingIdHeader, context.trackingId)) - .withHeaders(context.contextHeaders.toSeq.map { h => - RawHeader(h._1, h._2): HttpHeader + .withHeaders(context.contextHeaders.toSeq.map { + case (ContextHeaders.TrackingIdHeader, headerValue) => + RawHeader(ContextHeaders.TrackingIdHeader, context.trackingId) + case (ContextHeaders.StacktraceHeader, headerValue) => + RawHeader(ContextHeaders.StacktraceHeader, + Option(MDC.get("stack")) + .orElse(context.contextHeaders.get(ContextHeaders.StacktraceHeader)) + .getOrElse("")) + case (header, headerValue) => RawHeader(header, headerValue) }: _*) log.info(s"Sending request to ${request.method} ${request.uri}") |