diff --git a/jicoco-metrics/src/main/kotlin/org/jitsi/metrics/MetricsContainer.kt b/jicoco-metrics/src/main/kotlin/org/jitsi/metrics/MetricsContainer.kt index c4a25b3..6ecf2e3 100644 --- a/jicoco-metrics/src/main/kotlin/org/jitsi/metrics/MetricsContainer.kt +++ b/jicoco-metrics/src/main/kotlin/org/jitsi/metrics/MetricsContainer.kt @@ -76,16 +76,24 @@ open class MetricsContainer @JvmOverloads constructor( * Gets metrics in a format based on the `Accept` header. Returns the content type as the second element of the * pair. Defaults to OpenMetrics. */ - fun getMetrics(accepts: List): Pair { - return when { - accepts.isEmpty() or accepts.contains("*/*") or accepts.contains(OPENMETRICS) -> - getPrometheusMetrics(TextFormat.CONTENT_TYPE_OPENMETRICS_100) to TextFormat.CONTENT_TYPE_OPENMETRICS_100 - accepts.contains(TEXT) -> - getPrometheusMetrics(TextFormat.CONTENT_TYPE_004) to TextFormat.CONTENT_TYPE_004 - accepts.contains(JSON) -> jsonString to "application/json" - else -> - throw NoSupportedMediaTypeException("Supported media types are $OPENMETRICS, $TEXT, and $JSON") + fun getMetrics( + /** List of accepted media types in order of preference */ + accepts: List + ): Pair { + accepts.forEach { + when(it) { + "application/openmetrics-text" -> return getPrometheusMetrics( + TextFormat.CONTENT_TYPE_OPENMETRICS_100) to TextFormat.CONTENT_TYPE_OPENMETRICS_100 + "text/plain" -> return getPrometheusMetrics( + TextFormat.CONTENT_TYPE_004) to TextFormat.CONTENT_TYPE_004 + "application/json" -> return jsonString to "application/json" + "*/*" -> return getPrometheusMetrics( + TextFormat.CONTENT_TYPE_OPENMETRICS_100) to TextFormat.CONTENT_TYPE_OPENMETRICS_100 + } } + throw NoSupportedMediaTypeException( + "Supported media types are application/openmetrics-text, text/plain and application/json" + ) } /** @@ -235,10 +243,4 @@ open class MetricsContainer @JvmOverloads constructor( } class NoSupportedMediaTypeException(message: String) : Exception(message) - - companion object { - const val OPENMETRICS = "application/openmetrics-text" - const val TEXT = "text/plain" - const val JSON = "application/json" - } }