All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.niebes.retrofit.metrics.RetrofitCallMetricsCollector.kt Maven / Gradle / Ivy

The newest version!
package net.niebes.retrofit.metrics

import okhttp3.Request
import retrofit2.Response
import java.time.Duration

class RetrofitCallMetricsCollector(
    private val baseUrl: String,
    private val uri: String,
    private val metricsRecorder: MetricsRecorder,
) {

    fun measureRequestDuration(
        duration: Duration,
        request: Request,
        response: Response,
        async: Boolean,
    ) = metricsRecorder.recordTiming(
        mapOf(
            "base_url" to baseUrl,
            "uri" to uri,
            "method" to request.method,
            "async" to async.toString(),
            "series" to (HttpSeries.fromHttpStatus(response.code())?.name ?: "UNKNOWN"),
            "status" to response.code().toString(),
            "exception" to "None"
        ),
        duration
    )

    fun measureRequestException(
        duration: Duration,
        request: Request,
        throwable: Throwable,
        async: Boolean,
    ) = metricsRecorder.recordTiming(
        mapOf(
            "base_url" to baseUrl,
            "uri" to uri,
            "method" to request.method,
            "async" to async.toString(),
            "exception" to throwable.javaClass.simpleName,
            "series" to "EXCEPTION",
            "status" to "Exception"
        ),
        duration
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy