@@ -7,7 +7,8 @@ import akka.http.scaladsl.Http
77import akka .http .scaladsl .model ._
88import akka .http .scaladsl .model .headers .{`Retry-After` , RetryAfterDateTime , RetryAfterDuration }
99import akka .stream .ActorMaterializer
10- import com .typesafe .scalalogging .StrictLogging
10+ import com .typesafe .scalalogging .{Logger , LoggerTakingImplicit }
11+ import org .slf4j .LoggerFactory
1112
1213import scala .collection .immutable
1314import scala .concurrent .duration ._
@@ -18,25 +19,50 @@ import scala.util.{Failure, Success, Try}
1819class HttpClient (
1920 config : HttpClientConfig ,
2021 gatewayType : String ,
21- httpMetrics : HttpMetrics ,
22+ httpMetrics : HttpMetrics [ String ] ,
2223 retryConfig : RetryConfig ,
2324 clock : Clock ,
2425 awsRequestSigner : Option [AwsRequestSigner ]
25- )(implicit system : ActorSystem )
26+ )(
27+ implicit system : ActorSystem
28+ ) extends LoggingHttpClient [String ](config, gatewayType, httpMetrics, retryConfig, clock, awsRequestSigner)(
29+ system,
30+ Logger .takingImplicit(LoggerFactory .getLogger(getClass.getName))((msg : String , _ : String ) => msg)
31+ ) {
32+ override def request (
33+ method : HttpMethod ,
34+ entity : MessageEntity ,
35+ path : String ,
36+ headers : immutable.Seq [HttpHeader ],
37+ deadline : Deadline ,
38+ queryString : Option [String ]
39+ )(implicit executionContext : ExecutionContext , ctx : String = " " ): Future [HttpClientResponse ] =
40+ super .request(method, entity, path, headers, deadline, queryString)
41+ }
42+
43+ class LoggingHttpClient [LoggingContext ](
44+ config : HttpClientConfig ,
45+ gatewayType : String ,
46+ httpMetrics : HttpMetrics [LoggingContext ],
47+ retryConfig : RetryConfig ,
48+ clock : Clock ,
49+ awsRequestSigner : Option [AwsRequestSigner ]
50+ )(implicit system : ActorSystem , logger : LoggerTakingImplicit [LoggingContext ])
2651 extends HttpLayer (config, gatewayType, httpMetrics, retryConfig, clock, awsRequestSigner) {
2752 override protected def sendRequest : HttpRequest => Future [HttpResponse ] = Http ().singleRequest(_)
2853}
2954
30- abstract class HttpLayer (
55+ abstract class HttpLayer [ LoggingContext ] (
3156 config : HttpClientConfig ,
3257 gatewayType : String ,
33- httpMetrics : HttpMetrics ,
58+ httpMetrics : HttpMetrics [ LoggingContext ] ,
3459 retryConfig : RetryConfig ,
3560 clock : Clock ,
3661 awsRequestSigner : Option [AwsRequestSigner ] = None
3762)(
38- implicit system : ActorSystem
39- ) extends StrictLogging {
63+ implicit system : ActorSystem ,
64+ logger : LoggerTakingImplicit [LoggingContext ]
65+ ) {
4066
4167 protected def sendRequest : HttpRequest => Future [HttpResponse ]
4268
@@ -48,7 +74,8 @@ abstract class HttpLayer(
4874 deadline : Deadline ,
4975 queryString : Option [String ] = None
5076 )(
51- implicit executionContext : ExecutionContext
77+ implicit executionContext : ExecutionContext ,
78+ ctx : LoggingContext
5279 ): Future [HttpClientResponse ] =
5380 if (deadline.isOverdue()) {
5481 Future .successful(DeadlineExpired ())
@@ -73,7 +100,8 @@ abstract class HttpLayer(
73100 }
74101
75102 private [this ] def executeRequest (request : HttpRequest , tryNumber : Int , deadline : Deadline )(
76- implicit ec : ExecutionContext
103+ implicit ec : ExecutionContext ,
104+ ctx : LoggingContext
77105 ): Future [HttpClientResponse ] =
78106 Future
79107 .successful(request)
@@ -87,7 +115,7 @@ abstract class HttpLayer(
87115
88116 private [this ] def handleResponse (tryNumber : Int , deadline : Deadline , httpRequest : HttpRequest )(
89117 response : HttpResponse
90- )(implicit ec : ExecutionContext ): Future [HttpClientResponse ] = response match {
118+ )(implicit ec : ExecutionContext , ctx : LoggingContext ): Future [HttpClientResponse ] = response match {
91119 case response @ HttpResponse (StatusCodes .Success (_), _, _, _) => Future .successful(HttpClientSuccess (response))
92120 case response @ HttpResponse (StatusCodes .BadRequest , _, HttpEntity .Empty , _) => Future .successful(HttpClientError (response))
93121 case response @ HttpResponse (StatusCodes .BadRequest , _, _, _) => Future .successful(DomainError (response))
@@ -113,7 +141,8 @@ abstract class HttpLayer(
113141 }
114142
115143 private [this ] def retryWithConfig (tryNum : Int , request : HttpRequest , response : HttpResponse , deadline : Deadline )(
116- implicit ec : ExecutionContext
144+ implicit ec : ExecutionContext ,
145+ ctx : LoggingContext
117146 ): Future [HttpClientResponse ] =
118147 if (deadline.isOverdue()) {
119148 logger.info(s " [ $gatewayType] Try # $tryNum: Deadline has expired. " )
@@ -145,7 +174,8 @@ abstract class HttpLayer(
145174 }
146175
147176 private [this ] def handleErrors (tryNum : Int , deadline : Deadline , request : HttpRequest )(
148- implicit ec : ExecutionContext
177+ implicit ec : ExecutionContext ,
178+ ctx : LoggingContext
149179 ): PartialFunction [Throwable , Future [HttpClientResponse ]] = {
150180 case NonFatal (e) if tryNum <= retryConfig.retriesException =>
151181 val delay : FiniteDuration = calculateDelay(None , tryNum)
@@ -156,16 +186,16 @@ abstract class HttpLayer(
156186 Future .successful(DeadlineExpired (None ))
157187 }
158188
159- private [this ] def logRequest [T ]: PartialFunction [Try [HttpRequest ], Unit ] = {
189+ private [this ] def logRequest [T ]( implicit ctx : LoggingContext ) : PartialFunction [Try [HttpRequest ], Unit ] = {
160190 case Success (request) => logger.debug(s " [ $gatewayType] Sending request to ${request.method.value} ${request.uri}. " )
161191 }
162192
163- private [this ] def logRetryAfter : PartialFunction [Try [HttpResponse ], Unit ] = {
193+ private [this ] def logRetryAfter ( implicit ctx : LoggingContext ) : PartialFunction [Try [HttpResponse ], Unit ] = {
164194 case Success (response) if response.header[`Retry-After`].isDefined =>
165195 logger.info(s " [ $gatewayType] Received retry-after header with value ${response.header[`Retry-After`]}" )
166196 }
167197
168- private [this ] def logResponse (request : HttpRequest ): PartialFunction [Try [HttpResponse ], Unit ] = {
198+ private [this ] def logResponse (request : HttpRequest )( implicit ctx : LoggingContext ) : PartialFunction [Try [HttpResponse ], Unit ] = {
169199 case Success (response) =>
170200 httpMetrics.meterResponse(request.method, request.uri.path, response)
171201 logger.debug(s " [ $gatewayType] Received response ${response.status} from ${request.method.value} ${request.uri}. " )
0 commit comments