Skip to content

Commit

Permalink
Add profile to allow update of code source when modifying servlet via…
Browse files Browse the repository at this point in the history
… profile updateSource

Signed-off-by: Nicolas Trangosi <nicolas.trangosi@dcbrain.com>
  • Loading branch information
mmadoo committed Apr 2, 2021
1 parent 46469e9 commit 80d27e1
Show file tree
Hide file tree
Showing 6 changed files with 441 additions and 392 deletions.
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

0 comments on commit 80d27e1

Please sign in to comment.