Skip to content

Commit

Permalink
feat(debug): Add logging in debug for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ElPicador committed Sep 21, 2017
1 parent 0e749a1 commit 22ea59c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ coverageEnabled := false

val asyncHttpClientVersion = "2.0.33"
val json4sVersion = "3.5.2"
val logbackVersion = "1.2.3"
val slf4jVersion = "1.7.25"
val scalaUriVersion = "0.4.16"

val logbackVersion = "1.2.3"
val scalaTestVersion = "3.0.1"
val scalaMockVersion = "3.4.2"
val scalacheckVersion = "1.12.6"
Expand All @@ -27,14 +28,15 @@ libraryDependencies += "org.json4s" %% "json4s-ast" % json4sVersion
libraryDependencies += "org.json4s" %% "json4s-core" % json4sVersion
libraryDependencies += "org.json4s" %% "json4s-native" % json4sVersion

libraryDependencies += "ch.qos.logback" % "logback-classic" % logbackVersion
libraryDependencies += "org.slf4j" % "slf4j-api" % slf4jVersion

libraryDependencies += "io.lemonlabs" %% "scala-uri" % scalaUriVersion

//Testing
libraryDependencies += "org.scalatest" %% "scalatest" % scalaTestVersion % "test"
libraryDependencies += "org.scalacheck" %% "scalacheck" % scalacheckVersion % "test"
libraryDependencies += "org.scalamock" %% "scalamock-scalatest-support" % scalaMockVersion % "test"
libraryDependencies += "ch.qos.logback" % "logback-classic" % logbackVersion

scalacOptions ++= Seq(
"-deprecation",
Expand Down
9 changes: 8 additions & 1 deletion src/main/scala/algolia/AlgoliaClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import javax.crypto.spec.SecretKeySpec

import algolia.http.HttpPayload
import algolia.objects.Query
import org.slf4j.{Logger, LoggerFactory}

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
Expand Down Expand Up @@ -119,6 +120,9 @@ class AlgoliaClient(applicationId: String,
def close(): Unit = httpClient.close()

private val failedStart: Future[Nothing] = Future.failed(StartException())

val logger: Logger = LoggerFactory.getLogger("algoliasearch")

private[algolia] def request[T: Manifest](payload: HttpPayload)(
implicit executor: ExecutionContext): Future[T] = {
val hosts = if (payload.isSearch) {
Expand All @@ -140,6 +144,7 @@ class AlgoliaClient(applicationId: String,
val result = hosts.foldLeft[Future[T]](failedStart) { (future, host) =>
future.recoverWith {
case f: `4XXAPIException` =>
logger.debug("Got 4XX, no retry", f)
Future.failed(f) //No retry if 4XX
case _ =>
makeRequest(host)
Expand All @@ -148,9 +153,11 @@ class AlgoliaClient(applicationId: String,

result.recoverWith {
case e: `4XXAPIException` =>
logger.debug("Got 4XX, no retry", e)
Future.failed(new AlgoliaClientException(e.getMessage, e))
case e =>
Future.failed(new AlgoliaClientException("Failed on last retry", e))
logger.debug("All retries failed", e)
Future.failed(new AlgoliaClientException("All retries failed", e))
}
}
}
Expand Down
28 changes: 19 additions & 9 deletions src/main/scala/algolia/AlgoliaHttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import io.netty.resolver.dns.{DnsNameResolver, DnsNameResolverBuilder}
import org.asynchttpclient._
import org.json4s._
import org.json4s.native.JsonMethods._
import org.slf4j.{Logger, LoggerFactory}

import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.util.{Failure, Success}
Expand All @@ -55,6 +56,8 @@ case class AlgoliaHttpClient(
.maxQueriesPerResolve(2)
.build

val logger: Logger = LoggerFactory.getLogger("algoliasearch")

val _httpClient = new DefaultAsyncHttpClient(asyncClientConfig)

implicit val formats: Formats = AlgoliaDsl.formats
Expand All @@ -64,24 +67,27 @@ case class AlgoliaHttpClient(
def request[T: Manifest](host: String, headers: Map[String, String], payload: HttpPayload)(
implicit executor: ExecutionContext): Future[T] = {
val request = payload(host, headers, dnsNameResolver)
makeRequest(request, responseHandler)
logger.debug(s"Trying $host")
logger.debug(s"Query ${payload.toString(host)}")
makeRequest(host, request, responseHandler)
}

def responseHandler[T: Manifest]: AsyncCompletionHandler[T] = new AsyncCompletionHandler[T] {
override def onCompleted(response: Response): T =
def responseHandler[T: Manifest]: AsyncCompletionHandler[T] =
(response: Response) =>
response.getStatusCode / 100 match {
case 2 => toJson(response).extract[T]
case 4 =>
throw `4XXAPIException`(response.getStatusCode,
(toJson(response) \ "message").extract[String])
case _ => throw UnexpectedResponseException(response.getStatusCode)
}
}
case _ =>
logger.debug(s"Got HTTP code ${response.getStatusCode}, no retry")
throw UnexpectedResponseException(response.getStatusCode)
}

def toJson(r: Response): JValue =
parse(StringInput(r.getResponseBody), useBigDecimalForDouble = true)

def makeRequest[T](request: Request, handler: AsyncHandler[T])(
def makeRequest[T](host: String, request: Request, handler: AsyncHandler[T])(
implicit executor: ExecutionContext): Future[T] = {
val javaFuture = _httpClient.executeRequest(request, handler)
val promise = Promise[T]()
Expand All @@ -90,8 +96,12 @@ case class AlgoliaHttpClient(
try {
promise.complete(Success(javaFuture.get()))
} catch {
case e: ExecutionException => promise.complete(Failure(e.getCause))
case f: Throwable => promise.complete(Failure(f))
case e: ExecutionException =>
logger.debug(s"Failing to query $host", e)
promise.complete(Failure(e.getCause))
case f: Throwable =>
logger.debug(s"Failing to query $host", f)
promise.complete(Failure(f))
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/scala/algolia/HostsStatuses.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ package algolia

import java.util.concurrent.ConcurrentHashMap

import org.slf4j.{Logger, LoggerFactory}

case class HostsStatuses(configuration: AlgoliaClientConfiguration,
utils: AlgoliaUtils,
queryHosts: Seq[String],
Expand All @@ -35,11 +37,17 @@ case class HostsStatuses(configuration: AlgoliaClientConfiguration,
private[algolia] val hostStatuses: ConcurrentHashMap[String, HostStatus] =
new ConcurrentHashMap[String, HostStatus](5)

def markHostAsUp(host: String): Unit =
val logger: Logger = LoggerFactory.getLogger("algoliasearch")

def markHostAsUp(host: String): Unit = {
logger.debug("Marking {} as `up`", host)
hostStatuses.put(host, HostStatus.up(utils.now()))
}

def markHostAsDown(host: String): Unit =
def markHostAsDown(host: String): Unit = {
logger.debug("Marking {} as `down`", host)
hostStatuses.put(host, HostStatus.down(utils.now()))
}

def indexingHostsThatAreUp(): Seq[String] = hostsThatAreUp(indexingHosts)

Expand Down
7 changes: 7 additions & 0 deletions src/main/scala/algolia/http/HttpPayload.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ private[algolia] case class HttpPayload(verb: HttpVerb,
builder.setNameResolver(dnsNameResolver).build()
}

def toString(host: String): String = {
val _path = path.foldLeft("")(_ + "/" + _)
val _query = queryParameters.fold("")(_.foldLeft("") { case (acc, (k, v)) => s"$acc&$k=$v" })

s"$verb https://$host${_path}${_query}"
}

}
8 changes: 4 additions & 4 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
<configuration>

<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>

<logger name="com.ning.http" level="ERROR"/>
<logger name="com.ning.http" level="error"/>

<!-- By default, the level of the root level is set to DEBUG -->
<root level="INFO">
<appender-ref ref="STDOUT"/>
<root level="info">
<appender-ref ref="stdout"/>
</root>
</configuration>

0 comments on commit 22ea59c

Please sign in to comment.