Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#2234): Metrics content type. #2247

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading