-
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 #6 from wireapp/staging
0.2.4
Showing
3 changed files
with
82 additions
and
19 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
72 changes: 72 additions & 0 deletions
72
src/main/kotlin/com/wire/bots/polls/utils/ClientRequestMetric.kt
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,72 @@ | ||
package com.wire.bots.polls.utils | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.features.HttpClientFeature | ||
import io.ktor.client.statement.HttpReceivePipeline | ||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.client.statement.request | ||
import io.ktor.util.AttributeKey | ||
import mu.KLogging | ||
|
||
/** | ||
* [ClientRequestMetric] callback. | ||
*/ | ||
typealias RequestMetricHandler = suspend (RequestMetric) -> Unit | ||
|
||
|
||
/** | ||
* Enables callback after HttpClient sends a request with [RequestMetric]. | ||
*/ | ||
class ClientRequestMetric(private val metricHandler: RequestMetricHandler) { | ||
|
||
class Config { | ||
internal var metricHandler: RequestMetricHandler = {} | ||
|
||
/** | ||
* Set [RequestMetricHandler] called at the end of the request. | ||
*/ | ||
fun onResponse(block: RequestMetricHandler) { | ||
metricHandler = block | ||
} | ||
} | ||
|
||
companion object Feature : HttpClientFeature<Config, ClientRequestMetric>, KLogging() { | ||
|
||
override val key: AttributeKey<ClientRequestMetric> = | ||
AttributeKey("ClientRequestMetric") | ||
|
||
override fun prepare(block: Config.() -> Unit) = | ||
ClientRequestMetric(Config().apply(block).metricHandler) | ||
|
||
override fun install(feature: ClientRequestMetric, scope: HttpClient) { | ||
// synchronous response pipeline hook | ||
// instead of ResponseObserver - which spawns a new coroutine | ||
scope.receivePipeline.intercept(HttpReceivePipeline.After) { response -> | ||
// WARNING: Do not consume HttpResponse.content here, | ||
// or you will corrupt the client response. | ||
runCatching { feature.metricHandler(response.toRequestMetric()) } | ||
.onFailure { logger.error(it) { "Error during metering!" } } | ||
} | ||
} | ||
|
||
// does not touch the content | ||
private fun HttpResponse.toRequestMetric() = RequestMetric( | ||
requestTime = requestTime.timestamp, | ||
responseTime = responseTime.timestamp, | ||
method = request.method.value, | ||
url = request.url.toString(), | ||
responseCode = status.value | ||
) | ||
} | ||
} | ||
|
||
|
||
data class RequestMetric( | ||
// number of epoch milliseconds | ||
val requestTime: Long, | ||
val responseTime: Long, | ||
val method: String, | ||
val url: String, | ||
val responseCode: Int | ||
) | ||
|
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