Skip to content

Commit

Permalink
fix(#2234): Metrics content type. (#2247)
Browse files Browse the repository at this point in the history
We used to have non-deterministic behavior when no Accept header was
included, or it was set to */* (presumably depending on the order in
which the jakarta annotations were loaded). We now always default to
openmetrics.
  • Loading branch information
bgrozev authored Nov 5, 2024
1 parent 7245c6a commit d011ddf
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package org.jitsi.videobridge.rest.prometheus;

import io.prometheus.client.exporter.common.*;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import kotlin.*;
import org.jetbrains.annotations.NotNull;
import org.jitsi.metrics.*;

import java.util.*;
import java.util.stream.*;

/**
* A REST endpoint exposing JVB stats for Prometheus.
* Any scraper supporting Prometheus' text-based formats ({@code text/plain; version=0.0.4} or OpenMetrics)
Expand All @@ -35,6 +38,13 @@
@Path("/metrics")
public class Prometheus
{
static private final Comparator<MediaType> comparator = Comparator.comparing(Prometheus::getQValue).reversed();

private static double getQValue(MediaType m)
{
return m.getParameters().get("q") == null ? 1.0 : Double.parseDouble(m.getParameters().get("q"));
}

@NotNull
private final MetricsContainer metricsContainer;

Expand All @@ -44,23 +54,16 @@ public Prometheus(@NotNull MetricsContainer metricsContainer)
}

@GET
@Produces(TextFormat.CONTENT_TYPE_004)
public String getPrometheusPlainText()
{
return metricsContainer.getPrometheusMetrics(TextFormat.CONTENT_TYPE_004);
}

@GET
@Produces(TextFormat.CONTENT_TYPE_OPENMETRICS_100)
public String getPrometheusOpenMetrics()
public Response x(@HeaderParam("Accept") String accept)
{
return metricsContainer.getPrometheusMetrics(TextFormat.CONTENT_TYPE_OPENMETRICS_100);
}
List<String> acceptMediaTypes
= Arrays.stream(accept.split(","))
.map(MediaType::valueOf)
.sorted(comparator)
.map(m -> m.getType() + "/" + m.getSubtype())
.collect(Collectors.toList());
Pair<String, String> m = metricsContainer.getMetrics(acceptMediaTypes);

@GET
@Produces(MediaType.APPLICATION_JSON)
public String getJsonString()
{
return metricsContainer.getJsonString();
return Response.ok(m.getFirst(), m.getSecond()).build();
}
}

0 comments on commit d011ddf

Please sign in to comment.