dave
02/10/2021, 8:02 AMElizabeth Thomas
02/10/2021, 10:59 PMdata class IncomingHttpRequest(val message: String = "", val status: Int = 0, val duration: Long = 0L) : Event
object ReportHttpTransactionFilter {
operator fun invoke(
events: Events = EventFilters.AddTimestamp().then(AutoMarshallingEvents(Jackson)),
event: Event? = null
): Filter = Filter { next ->
{ request ->
next(request).apply {
ResponseFilters.ReportHttpTransaction { httpTransaction ->
events(event ?: IncomingHttpRequest(
httpTransaction.request.toMessage(),
httpTransaction.response.status.code,
httpTransaction.duration.toMillis())
)
}
}
}
}
}
And my test class like this: (which fails)
class ReportHttpTransactionFilterSpec : Spek({
val request = Request(Method.GET, "/bob")
val response = Response(OK)
val recording = RecordingEvents()
test("report http transaction filter emits events with timestamp") {
val events = EventFilters.AddTimestamp().then(AutoMarshallingEvents(Jackson)).then(recording)
val event = IncomingHttpRequest("some message", 200, 10)
val handler = ReportHttpTransactionFilter(events, event).then { response }
expectThat(handler(request)).isEqualTo(response)
expectThat(recording.toList()).isEqualTo(listOf<Event>(MetadataEvent(
event,
mapOf("timestamp" to Clock.systemUTC().instant())))
)
}
})
dave
02/11/2021, 8:37 AMevents(event ?: IncomingHttpRequest(
httpTransaction.request.toMessage(),
httpTransaction.response.status.code,
httpTransaction.duration.toMillis())
)
Razvan
02/11/2021, 8:37 AMval events = EventFilters.AddTimestamp().then(AutoMarshallingEvents(AppJackson))
fun LogIncomingRequests() = ResponseFilters.ReportHttpTransaction {
events(LogHttpTransaction.fromTransaction(it))
}
val app = LogIncomingRequests().then { response }
Elizabeth Thomas
02/11/2021, 8:55 AMRazvan
02/11/2021, 9:00 AMfun LogIncomingRequests() = ResponseFilters.ReportHttpTransaction {
events(
IncomingHttpRequest(
itrequest.toMessage(),
it.response.status.code,
it.duration.toMillis())
)
)
}
if it helps to undersanddave
02/11/2021, 9:01 AMElizabeth Thomas
02/11/2021, 9:08 AMevents
is configured as val events = EventFilters.AddTimestamp().then(AutoMarshallingEvents(Jackson)).then(recording)
in my test class. And after running through the filter, when I check the recording
object, I see it being empty.dave
02/11/2021, 6:00 PMElizabeth Thomas
02/11/2021, 6:48 PMdave
02/11/2021, 6:56 PMElizabeth Thomas
02/11/2021, 8:49 PMobject ReportHttpTransactionFilter {
object Log {
operator fun invoke(
rawEvents: Events,
clock: Clock = Clock.systemUTC()
): Filter = Filter { next ->
{ request ->
val events = AddTimestamp(clock).then(rawEvents)
next(request).apply {
ResponseFilters.ReportHttpTransaction(clock = clock) { httpTransaction ->
events(
IncomingHttpRequest(
httpTransaction.request.method,
httpTransaction.request.uri.path,
httpTransaction.response.status.code,
httpTransaction.duration.toMillis()
)
)
}
}
}
}
}
dave
02/11/2021, 8:50 PMResponseFilters.ReportHttpTransaction(clock = clock) { httpTransaction ->
events(
IncomingHttpRequest(
httpTransaction.request.method,
httpTransaction.request.uri.path,
httpTransaction.response.status.code,
httpTransaction.duration.toMillis()
)
)
}
is a filterfilter.then(filter).then(handler)
Elizabeth Thomas
02/11/2021, 8:52 PMdave
02/11/2021, 8:54 PMElizabeth Thomas
02/11/2021, 8:54 PMobject ReportHttpTransaction {
operator fun invoke(
clock: Clock = Clock.systemUTC(),
transactionLabeler: HttpTransactionLabeler = { it },
recordFn: (HttpTransaction) -> Unit
): Filter = Filter { next ->
{
clock.instant().let { start ->
next(it).apply {
recordFn(transactionLabeler(HttpTransaction(it, this, between(start, clock.instant()))))
}
}
}
}
}
dave
02/11/2021, 8:55 PMapply()
there is actually doing something and not creting another filterElizabeth Thomas
02/11/2021, 8:57 PMLogHttpTransaction.fromTransaction
which @Razvan pointed out, is it in http4k-core? or some other module? I couldnt get the package for it. Again, its probably me doing something wrong.Razvan
02/11/2021, 9:21 PM