|
1 | 1 | package io.prometheus.client.jetty; |
2 | 2 |
|
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.hamcrest.Matchers.greaterThan; |
| 5 | +import static org.hamcrest.core.IsNull.notNullValue; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | + |
3 | 8 | import io.prometheus.client.CollectorRegistry; |
| 9 | +import java.io.FileNotFoundException; |
| 10 | +import java.net.HttpURLConnection; |
| 11 | +import java.net.URL; |
| 12 | +import org.eclipse.jetty.server.Handler; |
4 | 13 | import org.eclipse.jetty.server.Server; |
5 | 14 | import org.eclipse.jetty.server.ServerConnector; |
6 | 15 | import org.eclipse.jetty.server.handler.HandlerCollection; |
7 | 16 | import org.eclipse.jetty.server.handler.StatisticsHandler; |
8 | 17 | import org.eclipse.jetty.servlet.ServletContextHandler; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
9 | 20 | import org.junit.Test; |
10 | 21 |
|
11 | | -import java.io.FileNotFoundException; |
12 | | -import java.net.HttpURLConnection; |
13 | | -import java.net.URL; |
| 22 | +public class JettyStatisticsCollectorTest { |
14 | 23 |
|
15 | | -import static org.hamcrest.CoreMatchers.is; |
16 | | -import static org.hamcrest.Matchers.greaterThan; |
17 | | -import static org.hamcrest.core.IsNull.notNullValue; |
18 | | -import static org.junit.Assert.assertThat; |
| 24 | + private final Server server = new Server(); |
| 25 | + private final ServerConnector connector = new ServerConnector(server); |
19 | 26 |
|
20 | | -public class JettyStatisticsCollectorTest { |
21 | | - @Test |
22 | | - public void collect() throws Exception { |
23 | | - Server server = new Server(0); |
| 27 | + @Before |
| 28 | + public void setUp() throws Exception { |
| 29 | + server.addConnector(connector); |
| 30 | + HandlerCollection handlers = new HandlerCollection(); |
24 | 31 |
|
25 | 32 | ServletContextHandler context = new ServletContextHandler(); |
26 | 33 | context.setContextPath("/"); |
27 | | - server.setHandler(context); |
28 | | - |
29 | | - HandlerCollection handlers = new HandlerCollection(); |
| 34 | + handlers.setHandlers(new Handler[]{context}); |
30 | 35 |
|
31 | | - StatisticsHandler statisticsHandler = new StatisticsHandler(); |
32 | | - statisticsHandler.setServer(server); |
33 | | - handlers.addHandler(statisticsHandler); |
| 36 | + StatisticsHandler stats = new StatisticsHandler(); |
| 37 | + stats.setHandler(handlers); |
| 38 | + server.setHandler(stats); |
34 | 39 |
|
35 | 40 | // register collector |
36 | | - new JettyStatisticsCollector(statisticsHandler).register(); |
37 | | - |
38 | | - server.setHandler(handlers); |
| 41 | + new JettyStatisticsCollector(stats).register(); |
39 | 42 |
|
| 43 | + server.setHandler(stats); |
40 | 44 | server.start(); |
| 45 | + } |
41 | 46 |
|
42 | | - ServerConnector connector = (ServerConnector) server.getConnectors()[0]; |
43 | | - int port = connector.getLocalPort(); |
| 47 | + @After |
| 48 | + public void tearDown() throws Exception { |
| 49 | + server.stop(); |
| 50 | + } |
44 | 51 |
|
| 52 | + @Test |
| 53 | + public void collect() throws Exception { |
45 | 54 | // send GET request |
46 | 55 | try { |
47 | | - HttpURLConnection urlConnection = (HttpURLConnection) new URL("http://127.0.0.1:" + port).openConnection(); |
| 56 | + final String spec = "http://127.0.0.1:" + connector.getLocalPort(); |
| 57 | + final HttpURLConnection urlConnection = (HttpURLConnection) new URL(spec).openConnection(); |
48 | 58 | urlConnection.getInputStream().close(); |
49 | 59 | urlConnection.disconnect(); |
50 | 60 | } catch (FileNotFoundException ignored) { |
51 | 61 | } |
52 | 62 |
|
53 | 63 | assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_requests_total"), is(1.0)); |
54 | 64 | assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_requests_active"), is(0.0)); |
55 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_requests_active_max"), is(1.0)); |
56 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_request_time_max_seconds"), is(notNullValue())); |
57 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_request_time_seconds_total"), is(notNullValue())); |
| 65 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_requests_active_max"), |
| 66 | + is(1.0)); |
| 67 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_request_time_max_seconds"), |
| 68 | + is(notNullValue())); |
| 69 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_request_time_seconds_total"), |
| 70 | + is(notNullValue())); |
58 | 71 | assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_total"), is(1.0)); |
59 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_active"), is(0.0)); |
60 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_active_max"), is(greaterThan(0.0))); |
61 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_time_max"), is(notNullValue())); |
62 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_time_seconds_total"), is(notNullValue())); |
63 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_async_requests_total"), is(0.0)); |
64 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_async_requests_waiting"), is(0.0)); |
65 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_async_requests_waiting_max"), is(0.0)); |
66 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_async_dispatches_total"), is(0.0)); |
| 72 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_active"), |
| 73 | + is(0.0)); |
| 74 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_active_max"), |
| 75 | + is(greaterThan(0.0))); |
| 76 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_time_max"), |
| 77 | + is(notNullValue())); |
| 78 | + assertThat( |
| 79 | + CollectorRegistry.defaultRegistry.getSampleValue("jetty_dispatched_time_seconds_total"), |
| 80 | + is(notNullValue())); |
| 81 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_async_requests_total"), |
| 82 | + is(0.0)); |
| 83 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_async_requests_waiting"), |
| 84 | + is(0.0)); |
| 85 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_async_requests_waiting_max"), |
| 86 | + is(0.0)); |
| 87 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_async_dispatches_total"), |
| 88 | + is(0.0)); |
67 | 89 | assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_expires_total"), is(0.0)); |
68 | 90 |
|
69 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses", |
70 | | - new String[]{"code"}, new String[]{"1xx"}), is(0.0)); |
71 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses", |
72 | | - new String[]{"code"}, new String[]{"2xx"}), is(0.0)); |
73 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses", |
74 | | - new String[]{"code"}, new String[]{"3xx"}), is(0.0)); |
75 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses", |
76 | | - new String[]{"code"}, new String[]{"4xx"}), is(1.0)); |
77 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses", |
78 | | - new String[]{"code"}, new String[]{"5xx"}), is(0.0)); |
79 | | - |
80 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_stats_seconds"), is(notNullValue())); |
81 | | - assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses_bytes_total"), is(notNullValue())); |
| 91 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses_total", |
| 92 | + new String[]{"code"}, new String[]{"1xx"}), is(0.0)); |
| 93 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses_total", |
| 94 | + new String[]{"code"}, new String[]{"2xx"}), is(0.0)); |
| 95 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses_total", |
| 96 | + new String[]{"code"}, new String[]{"3xx"}), is(0.0)); |
| 97 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses_total", |
| 98 | + new String[]{"code"}, new String[]{"4xx"}), is(1.0)); |
| 99 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses_total", |
| 100 | + new String[]{"code"}, new String[]{"5xx"}), is(0.0)); |
| 101 | + |
| 102 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_stats_seconds"), |
| 103 | + is(notNullValue())); |
| 104 | + assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_responses_bytes_total"), |
| 105 | + is(notNullValue())); |
82 | 106 | } |
83 | | - |
84 | 107 | } |
0 commit comments