Measuring the request-response cycle

25 views
Skip to first unread message

Bardur Arantsson

unread,
Sep 6, 2014, 12:17:01 AM9/6/14
to unfilter...@googlegroups.com
Hi all,

Is it possible to somehow hook into the request-response cycle at the Unfiltered "level" and gain access to both the request and the response? More concretely, I'd like to get access to

* request URL
* response status code

and to be able to *somehow* have enough state to calculate the time taken from the start of request processing until the end of request processing.

I briefly looked into the "kit" thing, but I couldn't tell if it would let me do something like this. I suppose it might be possible to do something with a custom ResponseFilter, but it seems a bit... excessive.

Is there some simple way to do this?

Joaquin

unread,
Sep 6, 2014, 7:11:42 AM9/6/14
to unfilter...@googlegroups.com
Hi Bardur,

You can use an Intent for that. I answered something similar at SO
http://stackoverflow.com/questions/24724185/how-to-log-all-requests-with-unfiltered/24877695#24877695

The code:

object RequestLogging {
def apply[A, B](intent: Cycle.Intent[A, B]) =
Cycle.Intent[A, B] {
case req =>
val startTime = System.currentTimeMillis()
Cycle.Intent.complete(intent)(req) ~> new ResponseFunction[Any]() {
override def apply[C <: Any](resp: HttpResponse[C]) = {
val responseTime = System.currentTimeMillis() - startTime
println(s"${req.remoteAddr} ${new Date()}
${req.method} ${req.uri}
${resp.underlying.asInstanceOf[Response].getStatus} $responseTime")
resp
}
}
}
}

...

val plan = new unfiltered.filter.Plan {
def intent = RequestLogging {
case GET(Path("/record/1")) => ...
}
}

Best regards,

--Joaquín
> --
> You received this message because you are subscribed to the Google Groups
> "Unfiltered" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to unfiltered-sca...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bardur Arantsson

unread,
Sep 8, 2014, 3:34:08 AM9/8/14
to unfilter...@googlegroups.com
On Saturday, September 6, 2014 1:11:42 PM UTC+2, Joaquin Caraballo wrote:
Hi Bardur,

You can use an Intent for that. I answered something similar at SO
http://stackoverflow.com/questions/24724185/how-to-log-all-requests-with-unfiltered/24877695#24877695

The code:

    object RequestLogging {
      def apply[A, B](intent: Cycle.Intent[A, B]) =
        Cycle.Intent[A, B] {
          case req =>
            val startTime = System.currentTimeMillis()
            Cycle.Intent.complete(intent)(req) ~> new ResponseFunction[Any]() {
              override def apply[C <: Any](resp: HttpResponse[C]) = {
                val responseTime = System.currentTimeMillis() - startTime
                println(s"${req.remoteAddr} ${new Date()}
${req.method} ${req.uri}
${resp.underlying.asInstanceOf[Response].getStatus} $responseTime")
                resp
              }
            }
        }
    }


Ah, thanks. I was tinkering with something similar with ~>, but was missing the Cycle.Intent.complete bit.

A shame about the need for a downcast to the concrete response type, I've lodged a pull request to hopefully address that.

Reply all
Reply to author
Forward
0 new messages