This PR brings the ability to report response size in Ktor instrumentation by reading Content-Size header.
However, it may be missing for the chunked response or for the streams (in that case we cannot report response size anyway), or engines like OkHttp may drop it compression-like Content-Encoding is applied.
In Android SDK in that case we try to read response body manually, but it seems it is somewhat complicated / dangerous to do with Ktor, because:
Generally speaking HttpResponse.body doesn’t allow being read twice (see doc), meaning that if we read it in our instrumentation, client will later get an exception trying to read it as well.
This exception is not thrown if read is done from the HttpCall obtained from HttpResponse.call.save() (doc), because SavedHttpCall allows multiple reads of the body, but we cannot propagate this call to the client and doc also says that save() will close the origin.
There is HttpResponse.content: ByteReadChannel, but it is marked as InternalApi and doesn’t exist anymore in Ktor 3, meaning there will be a crash if we try to access this property in a runtime using Ktor 3 (in Ktor 3 this property got renamed to rawContent). Also I’m not sure if client can still read the body after we read the content channel.
Review checklist (to be filled by reviewers)
[ ] Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
[ ] Make sure you discussed the feature or bugfix with the maintaining team in an Issue
[ ] Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)
What does this PR do?
This PR brings the ability to report response size in Ktor instrumentation by reading
Content-Size
header.However, it may be missing for the chunked response or for the streams (in that case we cannot report response size anyway), or engines like OkHttp may drop it compression-like
Content-Encoding
is applied.In Android SDK in that case we try to read response body manually, but it seems it is somewhat complicated / dangerous to do with Ktor, because:
Generally speaking
HttpResponse.body
doesn’t allow being read twice (see doc), meaning that if we read it in our instrumentation, client will later get an exception trying to read it as well.This exception is not thrown if read is done from the
HttpCall
obtained fromHttpResponse.call.save()
(doc), becauseSavedHttpCall
allows multiple reads of the body, but we cannot propagate this call to the client and doc also says thatsave()
will close the origin.There is
HttpResponse.content: ByteReadChannel
, but it is marked asInternalApi
and doesn’t exist anymore in Ktor 3, meaning there will be a crash if we try to access this property in a runtime using Ktor 3 (in Ktor 3 this property got renamed torawContent
). Also I’m not sure if client can still read the body after we read the content channel.Review checklist (to be filled by reviewers)