Skip to content

Commit

Permalink
fix: Respect the client prefefence.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrozev committed Oct 15, 2024
1 parent 7ad6c46 commit 07c2614
Showing 1 changed file with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>): Pair<String, String> {
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<String>
): Pair<String, String> {
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"
)
}

/**
Expand Down Expand Up @@ -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"
}
}

0 comments on commit 07c2614

Please sign in to comment.