Skip to content

Commit 4a974ce

Browse files
committed
Use Java HttpServer to expose metrics and monitoring
References #143 Fixes #152
1 parent 085e4be commit 4a974ce

18 files changed

+98
-193
lines changed

pom.xml

-55
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<micrometer.version>1.9.1</micrometer.version>
5858
<swiftmq-client.version>12.2.2</swiftmq-client.version>
5959
<picocli.version>4.6.3</picocli.version>
60-
<jetty.version>11.0.11</jetty.version>
6160
<guava.version>31.1-jre</guava.version>
6261
<commons-compress.version>1.21</commons-compress.version>
6362
<zstd-jni.version>1.5.2-3</zstd-jni.version>
@@ -187,12 +186,6 @@
187186
<optional>true</optional>
188187
</dependency>
189188

190-
<dependency>
191-
<groupId>org.eclipse.jetty</groupId>
192-
<artifactId>jetty-server</artifactId>
193-
<version>${jetty.version}</version>
194-
<optional>true</optional>
195-
</dependency>
196189
<!-- end of dependencies for performance tool -->
197190

198191
<dependency>
@@ -580,48 +573,6 @@
580573

581574
<profiles>
582575

583-
<profile>
584-
<id>perf</id>
585-
<activation>
586-
<jdk>[11,)</jdk>
587-
</activation>
588-
<build>
589-
<plugins>
590-
<plugin>
591-
<groupId>org.codehaus.mojo</groupId>
592-
<artifactId>build-helper-maven-plugin</artifactId>
593-
<version>3.3.0</version>
594-
<executions>
595-
<execution>
596-
<id>add-source</id>
597-
<phase>generate-sources</phase>
598-
<goals>
599-
<goal>add-source</goal>
600-
</goals>
601-
<configuration>
602-
<sources>
603-
<source>src/performance-tool/java</source>
604-
</sources>
605-
</configuration>
606-
</execution>
607-
<execution>
608-
<id>add-test-source</id>
609-
<phase>generate-test-sources</phase>
610-
<goals>
611-
<goal>add-test-source</goal>
612-
</goals>
613-
<configuration>
614-
<sources>
615-
<source>src/performance-tool/test</source>
616-
</sources>
617-
</configuration>
618-
</execution>
619-
</executions>
620-
</plugin>
621-
</plugins>
622-
</build>
623-
</profile>
624-
625576
<profile>
626577
<id>performance-tool</id>
627578
<dependencies>
@@ -663,12 +614,6 @@
663614
<version>${guava.version}</version>
664615
</dependency>
665616

666-
<dependency>
667-
<groupId>org.eclipse.jetty</groupId>
668-
<artifactId>jetty-server</artifactId>
669-
<version>${jetty.version}</version>
670-
</dependency>
671-
672617
</dependencies>
673618
<build>
674619
<finalName>${finalName}</finalName>

src/docs/asciidoc/performance-tool.adoc

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
== The Performance Tool
22

3-
NOTE: The performance tool requires *Java 11 or more*.
4-
53
The library contains also a performance tool to test the RabbitMQ Stream plugin.
64
It is usable as an uber JAR
75
https://github.com/rabbitmq/rabbitmq-stream-java-client/releases[downloadable from GitHub Release] or as a https://hub.docker.com/r/pivotalrabbitmq/stream-perf-test[Docker image].

src/performance-tool/java/com/rabbitmq/stream/perf/DebugEndpointMonitoring.java renamed to src/main/java/com/rabbitmq/stream/perf/DebugEndpointMonitoring.java

+12-20
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
1414
package com.rabbitmq.stream.perf;
1515

16-
import jakarta.servlet.http.HttpServletRequest;
17-
import jakarta.servlet.http.HttpServletResponse;
16+
import com.sun.net.httpserver.HttpExchange;
17+
import com.sun.net.httpserver.HttpHandler;
1818
import java.io.IOException;
19+
import java.io.OutputStream;
1920
import java.io.PrintWriter;
2021
import java.io.StringWriter;
2122
import java.lang.management.LockInfo;
2223
import java.lang.management.ManagementFactory;
2324
import java.lang.management.MonitorInfo;
2425
import java.lang.management.RuntimeMXBean;
2526
import java.lang.management.ThreadInfo;
27+
import java.nio.charset.StandardCharsets;
2628
import java.time.LocalDateTime;
2729
import java.time.format.DateTimeFormatter;
2830
import java.util.List;
2931
import java.util.stream.Collectors;
3032
import java.util.stream.Stream;
31-
import org.eclipse.jetty.server.Request;
32-
import org.eclipse.jetty.server.handler.AbstractHandler;
3333
import picocli.CommandLine.Option;
3434

3535
class DebugEndpointMonitoring implements Monitoring {
@@ -46,25 +46,17 @@ public void configure(MonitoringContext context) {
4646
PlainTextThreadDumpFormatter formatter = new PlainTextThreadDumpFormatter();
4747
context.addHttpEndpoint(
4848
"threaddump",
49-
new AbstractHandler() {
49+
new HttpHandler() {
5050
@Override
51-
public void handle(
52-
String target,
53-
Request baseRequest,
54-
HttpServletRequest request,
55-
HttpServletResponse response)
56-
throws IOException {
51+
public void handle(HttpExchange exchange) throws IOException {
5752
ThreadInfo[] threadInfos =
5853
ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
59-
String content = formatter.format(threadInfos);
60-
61-
response.setStatus(HttpServletResponse.SC_OK);
62-
response.setContentLength(content.length());
63-
response.setContentType("text/plain");
64-
65-
response.getWriter().print(content);
66-
67-
baseRequest.setHandled(true);
54+
exchange.getResponseHeaders().set("Content-Type", "text/plain");
55+
byte[] content = formatter.format(threadInfos).getBytes(StandardCharsets.UTF_8);
56+
exchange.sendResponseHeaders(200, content.length);
57+
try (OutputStream out = exchange.getResponseBody()) {
58+
out.write(content);
59+
}
6860
}
6961
});
7062
}

src/performance-tool/java/com/rabbitmq/stream/perf/Monitoring.java renamed to src/main/java/com/rabbitmq/stream/perf/Monitoring.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2021-2022 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2021-2022 VMware, Inc. or its affiliates. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
4+
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
5+
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
6+
// please see LICENSE-APACHE2.
7+
//
8+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
9+
// either express or implied. See the LICENSE file for specific language governing
10+
// rights and limitations of this software.
11+
//
12+
// If you have any questions regarding licensing, please contact us at
13+
14+
package com.rabbitmq.stream.perf;
15+
16+
import com.sun.net.httpserver.HttpHandler;
17+
import com.sun.net.httpserver.HttpServer;
18+
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
19+
import java.net.InetSocketAddress;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
import java.util.Map.Entry;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
class MonitoringContext {
27+
28+
private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringContext.class);
29+
30+
private final int monitoringPort;
31+
private final CompositeMeterRegistry meterRegistry;
32+
33+
private final Map<String, HttpHandler> handlers = new LinkedHashMap<>();
34+
35+
private volatile HttpServer server;
36+
37+
MonitoringContext(int monitoringPort, CompositeMeterRegistry meterRegistry) {
38+
this.monitoringPort = monitoringPort;
39+
this.meterRegistry = meterRegistry;
40+
}
41+
42+
void addHttpEndpoint(String path, HttpHandler handler) {
43+
this.handlers.put(path, handler);
44+
}
45+
46+
void start() throws Exception {
47+
if (!handlers.isEmpty()) {
48+
server = HttpServer.create(new InetSocketAddress(this.monitoringPort), 0);
49+
50+
for (Entry<String, HttpHandler> entry : handlers.entrySet()) {
51+
String path = entry.getKey().startsWith("/") ? entry.getKey() : "/" + entry.getKey();
52+
HttpHandler handler = entry.getValue();
53+
server.createContext(path, handler);
54+
}
55+
56+
server.start();
57+
}
58+
}
59+
60+
void close() {
61+
if (server != null) {
62+
LOGGER.debug("Closing HTTP server");
63+
long start = System.currentTimeMillis();
64+
server.stop(0);
65+
LOGGER.debug("Closed HTTP server in {} ms", (System.currentTimeMillis() - start));
66+
}
67+
}
68+
69+
CompositeMeterRegistry meterRegistry() {
70+
return meterRegistry;
71+
}
72+
}

src/performance-tool/java/com/rabbitmq/stream/perf/PrometheusEndpointMonitoring.java renamed to src/main/java/com/rabbitmq/stream/perf/PrometheusEndpointMonitoring.java

+12-20
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
1414
package com.rabbitmq.stream.perf;
1515

16+
import com.sun.net.httpserver.HttpExchange;
17+
import com.sun.net.httpserver.HttpHandler;
1618
import io.micrometer.prometheus.PrometheusConfig;
1719
import io.micrometer.prometheus.PrometheusMeterRegistry;
18-
import jakarta.servlet.http.HttpServletRequest;
19-
import jakarta.servlet.http.HttpServletResponse;
2020
import java.io.IOException;
21-
import org.eclipse.jetty.server.Request;
22-
import org.eclipse.jetty.server.handler.AbstractHandler;
21+
import java.io.OutputStream;
22+
import java.nio.charset.StandardCharsets;
2323
import picocli.CommandLine.Option;
2424

2525
class PrometheusEndpointMonitoring implements Monitoring {
@@ -39,23 +39,15 @@ public void configure(MonitoringContext context) {
3939
context.meterRegistry().add(registry);
4040
context.addHttpEndpoint(
4141
"metrics",
42-
new AbstractHandler() {
42+
new HttpHandler() {
4343
@Override
44-
public void handle(
45-
String target,
46-
Request baseRequest,
47-
HttpServletRequest request,
48-
HttpServletResponse response)
49-
throws IOException {
50-
String scraped = registry.scrape();
51-
52-
response.setStatus(HttpServletResponse.SC_OK);
53-
response.setContentLength(scraped.length());
54-
response.setContentType("text/plain");
55-
56-
response.getWriter().print(scraped);
57-
58-
baseRequest.setHandled(true);
44+
public void handle(HttpExchange exchange) throws IOException {
45+
exchange.getResponseHeaders().set("Content-Type", "text/plain");
46+
byte[] content = registry.scrape().getBytes(StandardCharsets.UTF_8);
47+
exchange.sendResponseHeaders(200, content.length);
48+
try (OutputStream out = exchange.getResponseBody()) {
49+
out.write(content);
50+
}
5951
}
6052
});
6153
}

src/performance-tool/java/com/rabbitmq/stream/perf/MonitoringContext.java

-94
This file was deleted.

src/performance-tool/test/com/rabbitmq/stream/perf/LogUtilsTest.java renamed to src/test/java/com/rabbitmq/stream/perf/LogUtilsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2021-2022 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").

0 commit comments

Comments
 (0)