Skip to content

Commit

Permalink
add fastly request id to the dcr request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
marjisound committed Jan 24, 2025
1 parent a0c5539 commit 57c8226
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 72 deletions.
27 changes: 1 addition & 26 deletions common/app/common/ImplicitControllerExecutionContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,5 @@ import scala.concurrent.ExecutionContext

trait ImplicitControllerExecutionContext {
self: BaseController =>
implicit val executionContext: ExecutionContext = MDCExecutionContext.withMDC(controllerComponents.executionContext)
}

object MDCExecutionContext {
def withMDC(delegate: ExecutionContext): ExecutionContext = new ExecutionContext {
override def execute(runnable: Runnable): Unit = {
// Capture the current MDC context
val mdcContext = Option(MDC.getCopyOfContextMap)
delegate.execute(() => {
// Restore MDC context in the new thread
mdcContext match {
case Some(context) => {
println(s"updating MDC in MDCExecutionContext to ${mdcContext}")
MDC.setContextMap(context)
}
case None => {
println("clearing MDC in MDCExecutionContext")
MDC.clear()
}
}
runnable.run()
})
}

override def reportFailure(cause: Throwable): Unit = delegate.reportFailure(cause)
}
implicit val executionContext: ExecutionContext = controllerComponents.executionContext
}
6 changes: 5 additions & 1 deletion common/app/common/RequestLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ case class RequestLoggerFields(request: RequestHeader, response: Option[Result],
val guardianSpecificHeaders = allHeadersFields.view.filterKeys(_.toUpperCase.startsWith("X-GU-")).toMap

(allowListedHeaders ++ guardianSpecificHeaders).toList.map { case (headerName, headerValue) =>
LogFieldString(s"req.header.$headerName", headerValue)
if (headerName == "x-gu-xid") {
LogFieldString(s"fastlyRequestId", headerValue)
} else {
LogFieldString(s"req.header.$headerName", headerValue)
}
}
}
private lazy val customFields: List[LogField] = {
Expand Down
1 change: 0 additions & 1 deletion common/app/http/Filters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ object Filters {
executionContext: ExecutionContext,
): List[EssentialFilter] =
List(
new RequestIdFilter,
new RequestLoggingFilter,
new PanicSheddingFilter,
new JsonVaryHeadersFilter,
Expand Down
25 changes: 0 additions & 25 deletions common/app/http/RequestIdFilter.scala

This file was deleted.

22 changes: 16 additions & 6 deletions common/app/renderers/DotcomRenderingService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ class DotcomRenderingService extends GuLogging with ResultWithPreconnectPreload
ws: WSClient,
payload: JsValue,
endpoint: String,
fastlyRequestId: Option[String],
timeout: Duration = Configuration.rendering.timeout,
)(implicit request: RequestHeader): Future[WSResponse] = {

val start = currentTimeMillis()

val resp = ws
val request = ws
.url(endpoint)
.withRequestTimeout(timeout)
.addHttpHeaders("Content-Type" -> "application/json")
.post(payload)

val resp = fastlyRequestId match {
case Some(id) => request.addHttpHeaders("x-gu-xid" -> id).post(payload)
case None => request.post(payload)
}

resp.foreach(_ => {
DCRMetrics.DCRLatencyMetric.recordDuration(currentTimeMillis() - start)
Expand Down Expand Up @@ -99,6 +104,7 @@ class DotcomRenderingService extends GuLogging with ResultWithPreconnectPreload
cacheTime: CacheTime,
timeout: Duration = Configuration.rendering.timeout,
)(implicit request: RequestHeader): Future[Result] = {
val fastlyRequestId = request.headers.get("x-gu-xid")
def handler(response: WSResponse): Result = {
response.status match {
case 200 =>
Expand Down Expand Up @@ -133,9 +139,11 @@ class DotcomRenderingService extends GuLogging with ResultWithPreconnectPreload
}

if (CircuitBreakerDcrSwitch.isSwitchedOn) {
circuitBreaker.withCircuitBreaker(postWithoutHandler(ws, payload, endpoint, timeout)).map(handler)
circuitBreaker
.withCircuitBreaker(postWithoutHandler(ws, payload, endpoint, fastlyRequestId, timeout))
.map(handler)
} else {
postWithoutHandler(ws, payload, endpoint, timeout).map(handler)
postWithoutHandler(ws, payload, endpoint, fastlyRequestId, timeout).map(handler)
}
}

Expand Down Expand Up @@ -224,8 +232,9 @@ class DotcomRenderingService extends GuLogging with ResultWithPreconnectPreload
)(implicit request: RequestHeader): Future[String] = {
val dataModel = DotcomBlocksRenderingDataModel(page, request, blocks)
val json = DotcomBlocksRenderingDataModel.toJson(dataModel)
val fastlyRequestId = request.headers.get("x-gu-xid")

postWithoutHandler(ws, json, Configuration.rendering.articleBaseURL + "/Blocks")
postWithoutHandler(ws, json, Configuration.rendering.articleBaseURL + "/Blocks", fastlyRequestId)
.flatMap(response => {
if (response.status == 200)
Future.successful(response.body)
Expand All @@ -245,8 +254,9 @@ class DotcomRenderingService extends GuLogging with ResultWithPreconnectPreload
)(implicit request: RequestHeader): Future[String] = {
val dataModel = DotcomBlocksRenderingDataModel(page, request, blocks)
val json = DotcomBlocksRenderingDataModel.toJson(dataModel)
val fastlyRequestId = request.headers.get("x-gu-xid")

postWithoutHandler(ws, json, Configuration.rendering.articleBaseURL + "/AppsBlocks")
postWithoutHandler(ws, json, Configuration.rendering.articleBaseURL + "/AppsBlocks", fastlyRequestId)
.flatMap(response => {
if (response.status == 200)
Future.successful(response.body)
Expand Down
15 changes: 2 additions & 13 deletions common/app/utils/DotcomponentsLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package utils

import common.GuLogging
import common.LoggingField._
import model.{ContentPage, ContentType, PageWithStoryPackage}
import model.ContentType
import model.liveblog.InteractiveBlockElement
import play.api.mvc.RequestHeader

Expand All @@ -18,6 +18,7 @@ case class DotcomponentsLoggerFields(request: Option[RequestHeader]) {
"req.method" -> r.method,
"req.url" -> r.uri,
"req.id" -> Random.nextInt(Integer.MAX_VALUE).toString,
"FastlyRequestId" -> r.headers.get("x-gu-xid").getOrElse("fastly-id-not-provided"),
)
}
.getOrElse(Nil)
Expand Down Expand Up @@ -91,18 +92,6 @@ case class DotcomponentsLogger(request: Option[RequestHeader]) extends GuLogging
logInfoWithCustomFields(message, customFields ++ fieldsFromResults(results) ++ elementsLogFieldFromPage(page))
}

def info(message: String): Unit = {
logInfoWithCustomFields(message, customFields)
}

def warn(message: String, error: Throwable): Unit = {
logWarningWithCustomFields(message, error, customFields)
}

def error(message: String, error: Throwable): Unit = {
logErrorWithCustomFields(message, error, customFields)
}

}

object DotcomponentsLogger {
Expand Down

0 comments on commit 57c8226

Please sign in to comment.