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

Add support to jakarta servlet #647

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Expand Up @@ -8,6 +8,13 @@
.idea
*.iml

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings

target/
simpleclient_pushgateway/mockserver.log
simpleclient_pushgateway/mockserver_request.log
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -57,6 +57,7 @@
<module>simpleclient_logback</module>
<module>simpleclient_pushgateway</module>
<module>simpleclient_servlet</module>
<module>simpleclient_servlet_jakarta</module>
<module>simpleclient_spring_web</module>
<module>simpleclient_spring_boot</module>
<module>simpleclient_jetty</module>
Expand Down
55 changes: 55 additions & 0 deletions simpleclient_servlet/pom.xml
Expand Up @@ -80,4 +80,59 @@
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>updateSource</id>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/../simpleclient_servlet_jakarta/src</outputDirectory>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<replace dir="${basedir}/../simpleclient_servlet_jakarta/src" token="javax.servlet" value="jakarta.servlet"/>
<replace file="${basedir}/../simpleclient_servlet_jakarta/src/test/java/io/prometheus/client/exporter/ExampleBenchmark.java"
token="Connector connector = (Connector)"
value="ServerConnector connector = (ServerConnector)"/>
<replace file="${basedir}/../simpleclient_servlet_jakarta/src/test/java/io/prometheus/client/exporter/ExampleBenchmark.java"
token="server.Connector"
value="server.ServerConnector"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
@@ -1,6 +1,8 @@
package io.prometheus.client.exporter;

import io.prometheus.client.Gauge;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
Expand Down Expand Up @@ -28,8 +30,9 @@ public static void main(String[] args) throws Exception {
server.start();
Thread.sleep(1000);

Connector connector = (Connector) server.getConnectors()[0];
byte[] bytes = new byte[8192];
URL url = new URL("http", "localhost", server.getConnectors()[0].getLocalPort(), "/metrics");
URL url = new URL("http", "localhost", connector.getLocalPort(), "/metrics");

long start = System.nanoTime();
for (int i = 0; i < 100; i++) {
Expand Down
Expand Up @@ -2,7 +2,6 @@

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import org.eclipse.jetty.http.HttpMethods;
import org.junit.After;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
Expand All @@ -25,6 +24,10 @@
import static org.mockito.Mockito.when;

public class MetricsFilterTest {

public static final String GET = "GET";
public static final String POST = "POST";

MetricsFilter f = new MetricsFilter();

@After
Expand All @@ -49,7 +52,7 @@ public void init() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);

when(req.getRequestURI()).thenReturn("/foo/bar/baz/bang/zilch/zip/nada");
when(req.getMethod()).thenReturn(HttpMethods.GET);
when(req.getMethod()).thenReturn(GET);

HttpServletResponse res = mock(HttpServletResponse.class);
FilterChain c = mock(FilterChain.class);
Expand All @@ -58,7 +61,7 @@ public void init() throws Exception {

verify(c).doFilter(req, res);

final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(metricName + "_count", new String[]{"path", "method"}, new String[]{"/foo/bar/baz/bang", HttpMethods.GET});
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(metricName + "_count", new String[]{"path", "method"}, new String[]{"/foo/bar/baz/bang", GET});
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
Expand All @@ -69,7 +72,7 @@ public void doFilter() throws Exception {
final String path = "/foo/bar/baz/bang/zilch/zip/nada";

when(req.getRequestURI()).thenReturn(path);
when(req.getMethod()).thenReturn(HttpMethods.GET);
when(req.getMethod()).thenReturn(GET);

HttpServletResponse res = mock(HttpServletResponse.class);
FilterChain c = mock(FilterChain.class);
Expand All @@ -85,7 +88,7 @@ public void doFilter() throws Exception {
verify(c).doFilter(req, res);


final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(name + "_count", new String[]{"path", "method"}, new String[]{path, HttpMethods.GET});
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(name + "_count", new String[]{"path", "method"}, new String[]{path, GET});
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
Expand All @@ -95,7 +98,7 @@ public void testConstructor() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
final String path = "/foo/bar/baz/bang";
when(req.getRequestURI()).thenReturn(path);
when(req.getMethod()).thenReturn(HttpMethods.POST);
when(req.getMethod()).thenReturn(POST);

FilterChain c = mock(FilterChain.class);
doAnswer(new Answer<Void>() {
Expand All @@ -117,7 +120,7 @@ public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
HttpServletResponse res = mock(HttpServletResponse.class);
constructed.doFilter(req, res, c);

final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foobar_baz_filter_duration_seconds_sum", new String[]{"path", "method"}, new String[]{path, HttpMethods.POST});
final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foobar_baz_filter_duration_seconds_sum", new String[]{"path", "method"}, new String[]{path, POST});
assertNotNull(sum);
assertEquals(0.1, sum, 0.01);
}
Expand All @@ -127,7 +130,7 @@ public void testBucketsAndName() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
final String path = "/foo/bar/baz/bang";
when(req.getRequestURI()).thenReturn(path);
when(req.getMethod()).thenReturn(HttpMethods.POST);
when(req.getMethod()).thenReturn(POST);

FilterChain c = mock(FilterChain.class);
doAnswer(new Answer<Void>() {
Expand All @@ -149,13 +152,13 @@ public Void answer(InvocationOnMock invocationOnMock) throws Throwable {

f.doFilter(req, res, c);

final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foo_sum", new String[]{"path", "method"}, new String[]{"/foo", HttpMethods.POST});
final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foo_sum", new String[]{"path", "method"}, new String[]{"/foo", POST});
assertEquals(0.1, sum, 0.01);

final Double le05 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", HttpMethods.POST, "0.05"});
final Double le05 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", POST, "0.05"});
assertNotNull(le05);
assertEquals(0, le05, 0.01);
final Double le15 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", HttpMethods.POST, "0.15"});
final Double le15 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", POST, "0.15"});
assertNotNull(le15);
assertEquals(1, le15, 0.01);

Expand Down Expand Up @@ -185,7 +188,7 @@ public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
public void testStatusCode() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/foo/bar/baz/bang");
when(req.getMethod()).thenReturn(HttpMethods.GET);
when(req.getMethod()).thenReturn(GET);

HttpServletResponse res = mock(HttpServletResponse.class);
when(res.getStatus()).thenReturn(200);
Expand All @@ -202,7 +205,7 @@ public void testStatusCode() throws Exception {

constructed.doFilter(req, res, c);

final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue("foobar_filter_status_total", new String[]{"path", "method", "status"}, new String[]{"/foo/bar", HttpMethods.GET, "200"});
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue("foobar_filter_status_total", new String[]{"path", "method", "status"}, new String[]{"/foo/bar", GET, "200"});
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
Expand All @@ -211,7 +214,7 @@ public void testStatusCode() throws Exception {
public void testStatusCodeWithNonHttpServletResponse() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/foo/bar/baz/bang");
when(req.getMethod()).thenReturn(HttpMethods.GET);
when(req.getMethod()).thenReturn(GET);

ServletResponse res = mock(ServletResponse.class);

Expand All @@ -227,7 +230,7 @@ public void testStatusCodeWithNonHttpServletResponse() throws Exception {

constructed.doFilter(req, res, c);

final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue("foobar_filter_status_total", new String[]{"path", "method", "status"}, new String[]{"/foo/bar", HttpMethods.GET, MetricsFilter.UNKNOWN_HTTP_STATUS_CODE});
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue("foobar_filter_status_total", new String[]{"path", "method", "status"}, new String[]{"/foo/bar", GET, MetricsFilter.UNKNOWN_HTTP_STATUS_CODE});
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
Expand Down
83 changes: 83 additions & 0 deletions simpleclient_servlet_jakarta/pom.xml
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.prometheus</groupId>
<artifactId>parent</artifactId>
<version>0.10.1-SNAPSHOT</version>
</parent>

<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet_jakarta</artifactId>
<packaging>bundle</packaging>

<name>Prometheus Java Simpleclient jakarta Servlet</name>
<description>
HTTP jakarta servlet exporter for the simpleclient.
</description>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<developers>
<developer>
<id>brian-brazil</id>
<name>Brian Brazil</name>
<email>brian.brazil@boxever.com</email>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.10.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_common</artifactId>
<version>0.10.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<!-- Test Dependencies Follow -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>11.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.28.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,72 @@
package io.prometheus.client.exporter;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
* The MetricsServlet class exists to provide a simple way of exposing the metrics values.
*
*/
public class MetricsServlet extends HttpServlet {

private CollectorRegistry registry;

/**
* Construct a MetricsServlet for the default registry.
*/
public MetricsServlet() {
this(CollectorRegistry.defaultRegistry);
}

/**
* Construct a MetricsServlet for the given registry.
* @param registry collector registry
*/
public MetricsServlet(CollectorRegistry registry) {
this.registry = registry;
}

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
resp.setStatus(HttpServletResponse.SC_OK);
String contentType = TextFormat.chooseContentType(req.getHeader("Accept"));
resp.setContentType(contentType);

Writer writer = new BufferedWriter(resp.getWriter());
try {
TextFormat.writeFormat(contentType, writer, registry.filteredMetricFamilySamples(parse(req)));
writer.flush();
} finally {
writer.close();
}
}

private Set<String> parse(HttpServletRequest req) {
String[] includedParam = req.getParameterValues("name[]");
if (includedParam == null) {
return Collections.emptySet();
} else {
return new HashSet<String>(Arrays.asList(includedParam));
}
}

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}

}