Skip to content

Commit

Permalink
feat: Add a prometheus /metrics endpoint. (#517)
Browse files Browse the repository at this point in the history
* chore: Update jicoco.
* feat: Port some metrics to prometheus.
* chore: Bump jetty.
* feat: Add a /metrics endpoint for prometheus.
* feat: Port most stats to metrics.
  • Loading branch information
bgrozev authored Feb 15, 2024
1 parent 9ba581c commit 0a047ff
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 84 deletions.
10 changes: 8 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.6</smack.version>
<jxmppVersion>1.0.3</jxmppVersion>
<!-- Match jicoco's jetty version. -->
<jicoco.version>1.1-107-gfb316f8</jicoco.version>
<jetty.version>11.0.10</jetty.version>
<jicoco.version>1.1-133-g768ef2e</jicoco.version>
<jetty.version>11.0.14</jetty.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -435,6 +435,12 @@
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jitsi</groupId>
<artifactId>jicoco-metrics</artifactId>
<version>${jicoco.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/jitsi/jigasi/metrics/JigasiMetricsContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Jigasi, the JItsi GAteway to SIP.
*
* Copyright @ 2024 - present 8x8, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.jigasi.metrics;

import io.prometheus.client.*;
import org.jitsi.metrics.*;

public class JigasiMetricsContainer extends MetricsContainer
{
public final static JigasiMetricsContainer INSTANCE = new JigasiMetricsContainer();

private JigasiMetricsContainer()
{
super(CollectorRegistry.defaultRegistry, "jitsi_jigasi");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,16 @@ protected Server initializeServer(BundleContext bundleContext)
server.addConnector(connector);

Handler handler = initializeHandler(bundleContext, server);
Handler metricsHandler = new MetricsHandler();

HandlerCollection handlers = new HandlerCollection();
if (handler != null)
server.setHandler(handler);
{
handlers.addHandler(handler);
}
handlers.addHandler(metricsHandler);

server.setHandler(handlers);

return server;
}
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/org/jitsi/jigasi/rest/MetricsHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright @ 2024 - present, 8x8 Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.jigasi.rest;

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

import java.io.*;

public class MetricsHandler extends AbstractHandler
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
if ("/metrics".equals(target))
{
String accept = request.getHeader("Accept");

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);
}

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

0 comments on commit 0a047ff

Please sign in to comment.