-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from BenWhitehead/finch-0.9
Refactor to support Finch 0.9.x
- Loading branch information
Showing
14 changed files
with
186 additions
and
419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/scala/io/github/benwhitehead/finch/filters/AccessLog.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.github.benwhitehead.finch.filters | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
|
||
import com.twitter.finagle.http.Version.{Http10, Http11} | ||
import com.twitter.finagle.{Filter, Service, SimpleFilter} | ||
import com.twitter.finagle.http.{Version, Response, Request} | ||
import com.twitter.util.Future | ||
|
||
class AccessLog(combined: Boolean) extends SimpleFilter[Request, Response] { | ||
lazy val accessLog = org.slf4j.LoggerFactory.getLogger("access-log") | ||
def apply(request: Request, service: Service[Request, Response]) = { | ||
if (accessLog.isTraceEnabled) { | ||
service(request) flatMap { case resp => | ||
val reqHeaders = request.headerMap | ||
val remoteHost = request.remoteHost | ||
val identd = "-" | ||
val user = "-" | ||
val requestEndTime = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss Z").format(new Date()) | ||
val reqResource = s"${request.method.toString.toUpperCase} ${request.uri} ${versionString(request.version)}" | ||
val statusCode = resp.statusCode | ||
val responseBytes = resp.headerMap.getOrElse("Content-Length", "-") | ||
|
||
if (!combined) { | ||
accessLog.trace(f"""$remoteHost%s $identd%s $user%s [$requestEndTime%s] "$reqResource%s" $statusCode%d $responseBytes%s""") | ||
} else { | ||
val referer = reqHeaders.getOrElse("Referer", "-") | ||
val userAgent = reqHeaders.getOrElse("User-Agent", "-") | ||
accessLog.trace(f"""$remoteHost%s $identd%s $user%s [$requestEndTime%s] "$reqResource%s" $statusCode%d $responseBytes%s "$referer%s" "$userAgent%s"""") | ||
} | ||
Future.value(resp) | ||
} | ||
} else { | ||
service(request) | ||
} | ||
} | ||
|
||
def versionString(v: Version) = v match { | ||
case Http11 => "HTTP/1.1" | ||
case Http10 => "HTTP/1.0" | ||
} | ||
} | ||
object AccessLog { | ||
lazy val accessLog = org.slf4j.LoggerFactory.getLogger("access-log") | ||
type httpFilter = Filter[Request, Response, Request, Response] | ||
def apply(preFilter: httpFilter, logType: String): httpFilter = { | ||
logType match { | ||
case "access-log" => preFilter andThen new AccessLog(false) | ||
case "access-log-combined" => preFilter andThen new AccessLog(true) | ||
case _ => | ||
accessLog.trace("access log disabled") | ||
preFilter | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/scala/io/github/benwhitehead/finch/filters/StatsFilter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.github.benwhitehead.finch.filters | ||
|
||
import com.twitter.finagle.{Service, SimpleFilter} | ||
import com.twitter.finagle.http.{Response, Request} | ||
import com.twitter.finagle.stats.{Stat, StatsReceiver} | ||
import com.twitter.util.Future | ||
|
||
class StatsFilter(baseScope: String = "")(implicit statsReceiver: StatsReceiver) extends SimpleFilter[Request, Response] { | ||
val stats = { | ||
if (baseScope.nonEmpty) statsReceiver.scope(s"$baseScope/route") | ||
else statsReceiver.scope("route") | ||
} | ||
def apply(request: Request, service: Service[Request, Response]): Future[Response] = { | ||
val label = s"${request.method.toString.toUpperCase}/ROOT/${request.path.stripPrefix("/")}" | ||
Stat.timeFuture(stats.stat(label)) { | ||
val f = service(request) | ||
stats.counter(label).incr() | ||
f | ||
} | ||
} | ||
} |
Oops, something went wrong.