Skip to content

Commit

Permalink
Replace javax by jakarta + adapt tests to jetty 11
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Trangosi <nicolas.trangosi@dcbrain.com>
  • Loading branch information
mmadoo committed Mar 30, 2021
1 parent 2e3d636 commit 46469e9
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 43 deletions.
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
14 changes: 7 additions & 7 deletions simpleclient_servlet_jakarta/pom.xml
Expand Up @@ -9,12 +9,12 @@
</parent>

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

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

<licenses>
Expand Down Expand Up @@ -49,9 +49,9 @@
<version>0.10.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<!-- Test Dependencies Follow -->
Expand All @@ -70,7 +70,7 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.1.7.v20120910</version>
<version>11.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -3,10 +3,10 @@
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
Expand Down
Expand Up @@ -3,14 +3,14 @@
import io.prometheus.client.Counter;
import io.prometheus.client.Histogram;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
Expand Down
Expand Up @@ -2,6 +2,7 @@

import io.prometheus.client.Gauge;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

Expand All @@ -27,9 +28,9 @@ public static void main(String[] args) throws Exception {
server.setHandler(context);
server.start();
Thread.sleep(1000);

ServerConnector connector = (ServerConnector) 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 @@ -4,9 +4,9 @@
import io.prometheus.client.Gauge;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down
Expand Up @@ -2,16 +2,18 @@

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;

import org.eclipse.jetty.http.HttpMethod;
import org.junit.After;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Enumeration;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -48,7 +50,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(HttpMethod.GET.asString());

HttpServletResponse res = mock(HttpServletResponse.class);
FilterChain c = mock(FilterChain.class);
Expand All @@ -60,7 +62,7 @@ public void init() throws Exception {
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(
metricName + "_count",
new String[] { "path", "method" },
new String[] { "/foo/bar/baz/bang", HttpMethods.GET });
new String[] { "/foo/bar/baz/bang", HttpMethod.GET.asString() });
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
Expand All @@ -71,7 +73,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(HttpMethod.GET.asString());

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

final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(name + "_count",
new String[] { "path", "method" },
new String[] { path, HttpMethods.GET });
new String[] { path, HttpMethod.GET.asString() });
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
Expand All @@ -98,7 +100,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(HttpMethod.POST.asString());

FilterChain c = mock(FilterChain.class);
doAnswer(new Answer<Void>() {
Expand All @@ -119,7 +121,7 @@ public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
final Double sum = CollectorRegistry.defaultRegistry.getSampleValue(
"foobar_baz_filter_duration_seconds_sum",
new String[] { "path", "method" },
new String[] { path, HttpMethods.POST });
new String[] { path, HttpMethod.POST.asString() });
assertNotNull(sum);
assertEquals(0.1, sum, 0.01);
}
Expand All @@ -129,7 +131,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(HttpMethod.POST.asString());

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

final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foo_sum",
new String[] { "path", "method" },
new String[] { "/foo", HttpMethods.POST });
new String[] { "/foo", HttpMethod.POST.asString() });
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" });
new String[] { "/foo", HttpMethod.POST.asString(), "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" });
new String[] { "/foo", HttpMethod.POST.asString(), "0.15" });
assertNotNull(le15);
assertEquals(1, le15, 0.01);

Expand Down Expand Up @@ -193,7 +195,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(HttpMethod.GET.asString());

HttpServletResponse res = mock(HttpServletResponse.class);
when(res.getStatus()).thenReturn(200);
Expand All @@ -209,7 +211,7 @@ public void testStatusCode() throws Exception {
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(
"foobar_filter_status_total",
new String[] { "path", "method", "status" },
new String[] { "/foo/bar", HttpMethods.GET, "200" });
new String[] { "/foo/bar", HttpMethod.GET.asString(), "200" });
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
Expand All @@ -218,7 +220,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(HttpMethod.GET.asString());

ServletResponse res = mock(ServletResponse.class);

Expand All @@ -235,7 +237,7 @@ public void testStatusCodeWithNonHttpServletResponse() throws Exception {
new String[] { "path", "method", "status" },
new String[] {
"/foo/bar",
HttpMethods.GET,
HttpMethod.GET.asString(),
MetricsFilter.UNKNOWN_HTTP_STATUS_CODE });
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
Expand Down

0 comments on commit 46469e9

Please sign in to comment.