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

chore: Update jicoco to 1.1-142-gfed0320. #560

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<smack.version>4.4.8</smack.version>
<jxmppVersion>1.0.3</jxmppVersion>
<!-- Match jicoco's jetty version. -->
<jicoco.version>1.1-140-g8f45a9f</jicoco.version>
<jetty.version>11.0.20</jetty.version>
<jicoco.version>1.1-142-gfed0320</jicoco.version>
<jetty.version>11.0.21</jetty.version>
<oci-sdk.version>3.45.0</oci-sdk.version>
</properties>

Expand Down
60 changes: 40 additions & 20 deletions src/main/java/org/jitsi/jigasi/rest/MetricsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
*/
package org.jitsi.jigasi.rest;

import io.prometheus.client.exporter.common.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import org.eclipse.jetty.server.*;
import jakarta.ws.rs.core.*;
import kotlin.*;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.*;
import org.jitsi.jigasi.metrics.*;
import org.jitsi.jigasi.stats.*;

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

public class MetricsHandler extends AbstractHandler
{
Expand All @@ -37,27 +40,44 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques

Statistics.updateMetrics();

String responseBody;
if (accept != null && accept.startsWith("application/openmetrics-text"))
{
responseBody = JigasiMetricsContainer.INSTANCE.getPrometheusMetrics(
TextFormat.CONTENT_TYPE_OPENMETRICS_100);
response.setContentType(TextFormat.CONTENT_TYPE_OPENMETRICS_100);
}
else if (accept != null && accept.startsWith("text/plain"))
{
responseBody = JigasiMetricsContainer.INSTANCE.getPrometheusMetrics(TextFormat.CONTENT_TYPE_004);
response.setContentType(TextFormat.CONTENT_TYPE_004);
}
else
{
responseBody = JigasiMetricsContainer.INSTANCE.getJsonString();
response.setContentType(RESTUtil.JSON_CONTENT_TYPE_WITH_CHARSET);
}
Pair<String, String> metricsAndContentType
= JigasiMetricsContainer.INSTANCE.getMetrics(getMediaTypes(accept));
response.setContentType(metricsAndContentType.getSecond());

Writer writer = response.getWriter();
writer.write(responseBody);
writer.write(metricsAndContentType.getFirst());
response.setStatus(200);
}
}

private List<String> getMediaTypes(String accept)
{
if (accept == null)
{
return Collections.emptyList();
}

if (accept.startsWith("Accept: "))
{
accept = accept.substring("Accept: ".length());
}

return Arrays.stream(accept.split(","))
.map(String::trim)
.map(MediaType::valueOf)
.sorted(new MediaTypeComparator())
.map(m -> m.getType() + "/" + m.getSubtype())
.collect(Collectors.toList());
}

private static class MediaTypeComparator implements Comparator<MediaType>
{
@Override
public int compare(MediaType o1, MediaType o2)
{
double q1 = Double.parseDouble(o1.getParameters().getOrDefault("q", "1.0"));
double q2 = Double.parseDouble(o2.getParameters().getOrDefault("q", "1.0"));
return Double.compare(q1, q2);
}
}
}
Loading