Hi all, we're using Ktor client with `HttpCache` p...
# ktor
u
Hi all, we're using Ktor client with
HttpCache
plugin. We've also made a custom logging plugin based on similar code as in the
Logging
plugin. However, one difference from the
Logging
plugin is that we would like to log the response even if nothing is sent over the network, ie the cached item is valid and returned directly (see for example
HttpCache.proceedWithCache
). Logging the response body becomes problematic and I'm unsure how to solve that. In the
Logging
plugin the
ResponseObserver
which is running in the
receivePipeline
is used to split the ByteReadChannel that contains the
rawContent
so that one is used for logging, but when response comes from cache the
receivePipeline
will never run. I can intercept the response in the
responsePipeline
though, but am unsure how to handle it here so that i can both log it and have the channel possible to consume later when reading the body, i've tried to something similar with
split( )
as in
ResponseObserver
but so far failed... (the response body is there but hidden as
responseBody
in
SavedHttpCall
, so if one could access it directly things would be quite simple...)
a
I can see that the only way to log the cached response is to execute a callback function in the HttpCache plugin, passed through a specific attribute right before finishing the send pipeline. Unfortunately, this requires modification of the HttpCache plugin's code.
u
yeah - I considered using reflection to access the responseBody in SavedHttpCall but then realized that in the
proceedWithWarning
case it's not even a SavedHttpCall
a
The main problem is that it's impossible to observe the cached call by intercepting the pipelines.
u
indeed, right now i'm using the HttpResponseFromCache monitor which works fine for logging both request + response except the body
a
Which pipeline do you intercept?
u
since the sendPipeline will finish early and the receive pipeline will never run i currently have something like this:
where the LoggedRequest attribute would have been set if the receive pipeline has run
so this works ok for everything except the body as mentioned, but maybe it's a bit hacky as well
(oh and inside the if (logResponseBody) is just an explanation why we can't 😄 )
But I tried to change all this to intercept the
responsePipeline
instead as mentioned, checked if the subject was a ByteReadChannel, tried splitting it as in
ResponseObserver
, and passing one part further down the pipelines and keeping one for logging.. But couldn't get that to work...
a
I think there is a bug that the
SavedHttpResponse.isSaved
returns false when the HttpCache plugin proceeds with the cached response. For the
proceedWithWarning
case, you can use the
ByteReadChannel.split
method to log the response body while leaving the original one unconsumed. I've filed an issue to support logging the cached calls with the OkHttp logging format. I assume there should be an option to enable/disable logging of the cached calls. Do you think the cached calls should be logged by default?
u
hmm, i have no specific opinion if it should be logged by default - i guess it depends on if you're only interested in logging what's going over the wire or not. So having it configurable is probably good