Skip to content

Commit 26156b5

Browse files
cenkakinbrian-brazil
authored andcommitted
add instrumented QueuedThreadPool (#247)
using counters instead of gauges in JettyStatisticsCollector
1 parent af8d033 commit 26156b5

File tree

7 files changed

+325
-66
lines changed

7 files changed

+325
-66
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Table of Contents
2020
* [Logging](#logging)
2121
* [Caches](#caches)
2222
* [Hibernate](#hibernate)
23+
* [Jetty](#jetty)
2324
* [Exporting](#exporting)
2425
* [HTTP](#http)
2526
* [Exporting to a Pushgateway](#exporting-to-a-pushgateway)
@@ -399,6 +400,35 @@ or `EntityManagerFactory`, you can use this code to access the underlying `Sessi
399400
```java
400401
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
401402
```
403+
### Jetty
404+
405+
There is a collector for recording various Jetty server metrics. You can do it by registering the collector like this:
406+
407+
```java
408+
// Configure StatisticsHandler.
409+
StatisticsHandler stats = new StatisticsHandler();
410+
stats.setHandler(server.getHandler());
411+
server.setHandler(stats);
412+
// Register collector.
413+
new JettyStatisticsCollector(stats).register();
414+
415+
```
416+
417+
Also, you can collect `QueuedThreadPool` metrics. If there is a single `QueuedThreadPool`
418+
to keep track of, use the following:
419+
420+
```java
421+
new QueuedThreadPoolStatisticsCollector(queuedThreadPool, "myapp").register();
422+
```
423+
424+
If you want to collect multiple `QueuedThreadPool` metrics, also you can achieve it like this:
425+
426+
```java
427+
new QueuedThreadPoolStatisticsCollector()
428+
.add(queuedThreadPool1, "myapp1")
429+
.add(queuedThreadPool2, "myapp2")
430+
.register();
431+
```
402432

403433
#### Servlet Filter
404434

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@
5959
<module>simpleclient_spring_web</module>
6060
<module>simpleclient_spring_boot</module>
6161
<module>simpleclient_jetty</module>
62+
<module>simpleclient_jetty_jdk8</module>
6263
<module>simpleclient_vertx</module>
6364
<module>benchmark</module>
6465
</modules>
65-
66+
6667
<properties>
6768
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
68-
69+
6970
<maven.compiler.source>1.6</maven.compiler.source>
7071
<maven.compiler.target>1.6</maven.compiler.target>
7172
</properties>

simpleclient_jetty/src/main/java/io/prometheus/client/jetty/JettyStatisticsCollector.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package io.prometheus.client.jetty;
22

33
import io.prometheus.client.Collector;
4-
import org.eclipse.jetty.server.handler.StatisticsHandler;
5-
64
import java.util.ArrayList;
75
import java.util.Arrays;
86
import java.util.Collections;
97
import java.util.List;
8+
import org.eclipse.jetty.server.handler.StatisticsHandler;
109

1110
/**
1211
* Collect metrics from jetty's org.eclipse.jetty.server.handler.StatisticsHandler.
1312
* <p>
1413
* <pre>{@code
15-
* Server server = new Server(0);
14+
* Server server = new Server(8080);
1615
*
1716
* ServletContextHandler context = new ServletContextHandler();
1817
* context.setContextPath("/");
@@ -24,7 +23,7 @@
2423
* statisticsHandler.setServer(server);
2524
* handlers.addHandler(statisticsHandler);
2625
*
27-
* // register collector
26+
* // Register collector.
2827
* new JettyStatisticsCollector(statisticsHandler).register();
2928
*
3029
* server.setHandler(handlers);
@@ -43,24 +42,24 @@ public JettyStatisticsCollector(StatisticsHandler statisticsHandler) {
4342
@Override
4443
public List<MetricFamilySamples> collect() {
4544
return Arrays.asList(
46-
buildGauge("jetty_requests_total", "Number of requests", statisticsHandler.getRequests()),
45+
buildCounter("jetty_requests_total", "Number of requests", statisticsHandler.getRequests()),
4746
buildGauge("jetty_requests_active", "Number of requests currently active", statisticsHandler.getRequestsActive()),
4847
buildGauge("jetty_requests_active_max", "Maximum number of requests that have been active at once", statisticsHandler.getRequestsActiveMax()),
4948
buildGauge("jetty_request_time_max_seconds", "Maximum time spent handling requests", statisticsHandler.getRequestTimeMax() / 1000.0),
50-
buildGauge("jetty_request_time_seconds_total", "Total time spent in all request handling", statisticsHandler.getRequestTimeTotal() / 1000.0),
51-
buildGauge("jetty_dispatched_total", "Number of dispatches", statisticsHandler.getDispatched()),
49+
buildCounter("jetty_request_time_seconds_total", "Total time spent in all request handling", statisticsHandler.getRequestTimeTotal() / 1000.0),
50+
buildCounter("jetty_dispatched_total", "Number of dispatches", statisticsHandler.getDispatched()),
5251
buildGauge("jetty_dispatched_active", "Number of dispatches currently active", statisticsHandler.getDispatchedActive()),
5352
buildGauge("jetty_dispatched_active_max", "Maximum number of active dispatches being handled", statisticsHandler.getDispatchedActiveMax()),
5453
buildGauge("jetty_dispatched_time_max", "Maximum time spent in dispatch handling", statisticsHandler.getDispatchedTimeMax()),
55-
buildGauge("jetty_dispatched_time_seconds_total", "Total time spent in dispatch handling", statisticsHandler.getDispatchedTimeTotal() / 1000.0),
56-
buildGauge("jetty_async_requests_total", "Total number of async requests", statisticsHandler.getAsyncRequests()),
54+
buildCounter("jetty_dispatched_time_seconds_total", "Total time spent in dispatch handling", statisticsHandler.getDispatchedTimeTotal() / 1000.0),
55+
buildCounter("jetty_async_requests_total", "Total number of async requests", statisticsHandler.getAsyncRequests()),
5756
buildGauge("jetty_async_requests_waiting", "Currently waiting async requests", statisticsHandler.getAsyncRequestsWaiting()),
5857
buildGauge("jetty_async_requests_waiting_max", "Maximum number of waiting async requests", statisticsHandler.getAsyncRequestsWaitingMax()),
59-
buildGauge("jetty_async_dispatches_total", "Number of requested that have been asynchronously dispatched", statisticsHandler.getAsyncDispatches()),
60-
buildGauge("jetty_expires_total", "Number of async requests requests that have expired", statisticsHandler.getExpires()),
61-
buildStatusGauge(),
58+
buildCounter("jetty_async_dispatches_total", "Number of requested that have been asynchronously dispatched", statisticsHandler.getAsyncDispatches()),
59+
buildCounter("jetty_expires_total", "Number of async requests requests that have expired", statisticsHandler.getExpires()),
60+
buildStatusCounter(),
6261
buildGauge("jetty_stats_seconds", "Time in seconds stats have been collected for", statisticsHandler.getStatsOnMs() / 1000.0),
63-
buildGauge("jetty_responses_bytes_total", "Total number of bytes across all responses", statisticsHandler.getResponsesBytesTotal())
62+
buildCounter("jetty_responses_bytes_total", "Total number of bytes across all responses", statisticsHandler.getResponsesBytesTotal())
6463
);
6564
}
6665

@@ -72,11 +71,19 @@ private static MetricFamilySamples buildGauge(String name, String help, double v
7271
Collections.singletonList(new MetricFamilySamples.Sample(name, EMPTY_LIST, EMPTY_LIST, value)));
7372
}
7473

75-
private MetricFamilySamples buildStatusGauge() {
76-
String name = "jetty_responses";
74+
private static MetricFamilySamples buildCounter(String name, String help, double value) {
75+
return new MetricFamilySamples(
76+
name,
77+
Type.COUNTER,
78+
help,
79+
Collections.singletonList(new MetricFamilySamples.Sample(name, EMPTY_LIST, EMPTY_LIST, value)));
80+
}
81+
82+
private MetricFamilySamples buildStatusCounter() {
83+
String name = "jetty_responses_total";
7784
return new MetricFamilySamples(
7885
name,
79-
Type.GAUGE,
86+
Type.COUNTER,
8087
"Number of requests with response status",
8188
Arrays.asList(
8289
buildStatusSample(name, "1xx", statisticsHandler.getResponses1xx()),
Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,107 @@
11
package io.prometheus.client.jetty;
22

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+
38
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;
413
import org.eclipse.jetty.server.Server;
514
import org.eclipse.jetty.server.ServerConnector;
615
import org.eclipse.jetty.server.handler.HandlerCollection;
716
import org.eclipse.jetty.server.handler.StatisticsHandler;
817
import org.eclipse.jetty.servlet.ServletContextHandler;
18+
import org.junit.After;
19+
import org.junit.Before;
920
import org.junit.Test;
1021

11-
import java.io.FileNotFoundException;
12-
import java.net.HttpURLConnection;
13-
import java.net.URL;
22+
public class JettyStatisticsCollectorTest {
1423

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

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();
2431

2532
ServletContextHandler context = new ServletContextHandler();
2633
context.setContextPath("/");
27-
server.setHandler(context);
28-
29-
HandlerCollection handlers = new HandlerCollection();
34+
handlers.setHandlers(new Handler[]{context});
3035

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

3540
// register collector
36-
new JettyStatisticsCollector(statisticsHandler).register();
37-
38-
server.setHandler(handlers);
41+
new JettyStatisticsCollector(stats).register();
3942

43+
server.setHandler(stats);
4044
server.start();
45+
}
4146

42-
ServerConnector connector = (ServerConnector) server.getConnectors()[0];
43-
int port = connector.getLocalPort();
47+
@After
48+
public void tearDown() throws Exception {
49+
server.stop();
50+
}
4451

52+
@Test
53+
public void collect() throws Exception {
4554
// send GET request
4655
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();
4858
urlConnection.getInputStream().close();
4959
urlConnection.disconnect();
5060
} catch (FileNotFoundException ignored) {
5161
}
5262

5363
assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_requests_total"), is(1.0));
5464
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()));
5871
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));
6789
assertThat(CollectorRegistry.defaultRegistry.getSampleValue("jetty_expires_total"), is(0.0));
6890

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()));
82106
}
83-
84107
}

simpleclient_jetty_jdk8/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>io.prometheus</groupId>
7+
<artifactId>parent</artifactId>
8+
<version>0.0.24-SNAPSHOT</version>
9+
</parent>
10+
11+
<groupId>io.prometheus</groupId>
12+
<artifactId>simpleclient_jetty_jdk8</artifactId>
13+
<packaging>bundle</packaging>
14+
15+
<name>Prometheus Java Simpleclient Jetty JDK 8</name>
16+
<description>
17+
Collector of data from Jetty Statistics for Jetty versions which require JDK 8.
18+
</description>
19+
20+
<properties>
21+
<maven.compiler.source>1.8</maven.compiler.source>
22+
<maven.compiler.target>1.8</maven.compiler.target>
23+
</properties>
24+
25+
<licenses>
26+
<license>
27+
<name>The Apache Software License, Version 2.0</name>
28+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
29+
<distribution>repo</distribution>
30+
</license>
31+
</licenses>
32+
<developers>
33+
<developer>
34+
<id>cenkakin</id>
35+
<name>Cenk Akin</name>
36+
<email>[email protected]</email>
37+
</developer>
38+
</developers>
39+
<dependencies>
40+
<dependency>
41+
<groupId>io.prometheus</groupId>
42+
<artifactId>simpleclient</artifactId>
43+
<version>0.0.24-SNAPSHOT</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.prometheus</groupId>
47+
<artifactId>simpleclient_jetty</artifactId>
48+
<version>0.0.24-SNAPSHOT</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.eclipse.jetty</groupId>
52+
<artifactId>jetty-server</artifactId>
53+
<version>9.4.4.v20170414</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.eclipse.jetty</groupId>
57+
<artifactId>jetty-servlet</artifactId>
58+
<version>9.4.4.v20170414</version>
59+
</dependency>
60+
<!-- Test Dependencies Follow -->
61+
<dependency>
62+
<groupId>junit</groupId>
63+
<artifactId>junit</artifactId>
64+
<version>4.11</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.hamcrest</groupId>
69+
<artifactId>hamcrest-all</artifactId>
70+
<version>1.1</version>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
</project>

0 commit comments

Comments
 (0)