Skip to content

Commit 981e304

Browse files
committed
chore: logging
1 parent 7516b37 commit 981e304

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/main/java/spp/demo/Main.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
package spp.demo;
22

3+
import com.codahale.metrics.ConsoleReporter;
4+
import com.codahale.metrics.MetricRegistry;
5+
import com.codahale.metrics.Timer;
36
import io.micronaut.runtime.Micronaut;
47
import spp.demo.command.AddBreakpoint;
58
import spp.demo.command.AddLog;
69
import spp.demo.command.TailLogs;
710

811
import java.net.HttpURLConnection;
12+
import java.net.MalformedURLException;
913
import java.net.URL;
10-
import java.util.Map;
1114
import java.util.concurrent.Executor;
1215
import java.util.concurrent.Executors;
1316

1417
public class Main {
1518

1619
private static final Executor executor = Executors.newCachedThreadPool();
20+
private static final MetricRegistry metricRegistry = new MetricRegistry();
1721

1822
public static void main(String[] args) throws Exception {
1923
Micronaut.run(Main.class, args);
2024

25+
ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();
26+
2127
while (true) {
2228
executeDemos();
2329
Thread.sleep(1000);
2430

25-
Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
26-
for (Map.Entry<Thread, StackTraceElement[]> entry : threads.entrySet()) {
27-
Thread thread = entry.getKey();
28-
StackTraceElement[] stackTraceElements = entry.getValue();
29-
System.out.println("Thread: " + thread.getName());
30-
for (StackTraceElement stackTraceElement : stackTraceElements) {
31-
System.out.println(" " + stackTraceElement);
32-
}
33-
}
31+
reporter.report();
3432

3533
int threadCount = Thread.activeCount();
3634
System.out.println("Thread count: " + threadCount);
@@ -92,15 +90,27 @@ public static void triggerEndpoints() {
9290
}
9391

9492
private static void callEndpoint(String endpoint) {
93+
Timer.Context timer = metricRegistry.timer(endpoint).time();
94+
URL url;
95+
try {
96+
url = new URL("http://localhost:8080" + endpoint);
97+
} catch (MalformedURLException e) {
98+
throw new RuntimeException(e);
99+
}
100+
95101
executor.execute(() -> {
102+
HttpURLConnection connection = null;
96103
try {
97-
URL url = new URL("http://localhost:8080" + endpoint);
98-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
104+
connection = (HttpURLConnection) url.openConnection();
99105
connection.setConnectTimeout(5000);
100106
connection.setReadTimeout(5000);
101107
connection.getResponseCode();
102-
connection.disconnect();
103108
} catch (Exception ignore) {
109+
} finally {
110+
if (connection != null) {
111+
connection.disconnect();
112+
}
113+
timer.close();
104114
}
105115
});
106116
}

0 commit comments

Comments
 (0)