17
17
import com .sun .net .httpserver .HttpHandler ;
18
18
import com .sun .net .httpserver .HttpServer ;
19
19
import io .micrometer .core .instrument .composite .CompositeMeterRegistry ;
20
+ import java .io .OutputStream ;
21
+ import java .net .InetAddress ;
20
22
import java .net .InetSocketAddress ;
21
- import java .util .LinkedHashMap ;
22
- import java .util .Map ;
23
- import java .util .Map .Entry ;
23
+ import java .nio .charset .StandardCharsets ;
24
+ import java .util .ArrayList ;
25
+ import java .util .Collection ;
26
+ import java .util .Collections ;
27
+ import java .util .stream .Collectors ;
24
28
import org .slf4j .Logger ;
25
29
import org .slf4j .LoggerFactory ;
26
30
@@ -32,7 +36,7 @@ class MonitoringContext {
32
36
private final CompositeMeterRegistry meterRegistry ;
33
37
private final Environment environment ;
34
38
35
- private final Map < String , HttpHandler > handlers = new LinkedHashMap <>();
39
+ private final Collection < Endpoint > endpoints = Collections . synchronizedList ( new ArrayList <>() );
36
40
37
41
private volatile HttpServer server ;
38
42
@@ -43,21 +47,60 @@ class MonitoringContext {
43
47
this .environment = environment ;
44
48
}
45
49
46
- void addHttpEndpoint (String path , HttpHandler handler ) {
47
- this .handlers . put ( path , handler );
50
+ void addHttpEndpoint (String path , String description , HttpHandler handler ) {
51
+ this .endpoints . add ( new Endpoint ( path , description , handler ) );
48
52
}
49
53
50
54
void start () throws Exception {
51
- if (!handlers .isEmpty ()) {
55
+ if (!endpoints .isEmpty ()) {
52
56
server = HttpServer .create (new InetSocketAddress (this .monitoringPort ), 0 );
53
57
54
- for (Entry <String , HttpHandler > entry : handlers .entrySet ()) {
55
- String path = entry .getKey ().startsWith ("/" ) ? entry .getKey () : "/" + entry .getKey ();
56
- HttpHandler handler = entry .getValue ();
57
- server .createContext (path , handler );
58
+ for (Endpoint handler : endpoints ) {
59
+ server .createContext (handler .path , handler .handler );
58
60
}
59
61
62
+ String hostname = hostname ();
63
+
64
+ StringBuilder builder =
65
+ new StringBuilder (
66
+ "{ \" name\" : \" StreamPerfTest Monitoring Endpoints\" , \" endpoints\" : [" );
67
+ String endpointJson =
68
+ endpoints .stream ()
69
+ .map (
70
+ endpoint ->
71
+ "{\" name\" : \" "
72
+ + endpoint .description
73
+ + "\" , \" href\" : \" "
74
+ + url (hostname , endpoint .path )
75
+ + "\" }" )
76
+ .collect (Collectors .joining ("," ));
77
+ builder .append (endpointJson );
78
+ builder .append ("]}" );
79
+ server .createContext (
80
+ "/" ,
81
+ exchange -> {
82
+ exchange .getResponseHeaders ().set ("Content-Type" , "application/json" );
83
+ byte [] content = builder .toString ().getBytes (StandardCharsets .UTF_8 );
84
+ exchange .sendResponseHeaders (200 , content .length );
85
+ try (OutputStream out = exchange .getResponseBody ()) {
86
+ out .write (content );
87
+ }
88
+ });
89
+
60
90
server .start ();
91
+ System .out .println ("Monitoring endpoints started on http://localhost:" + this .monitoringPort );
92
+ }
93
+ }
94
+
95
+ private String url (String hostname , String endpoint ) {
96
+ return String .format ("http://%s:%d%s" , hostname , this .monitoringPort , endpoint );
97
+ }
98
+
99
+ private static String hostname () {
100
+ try {
101
+ return InetAddress .getLocalHost ().getHostName ();
102
+ } catch (Exception e ) {
103
+ return "localhost" ;
61
104
}
62
105
}
63
106
@@ -77,4 +120,16 @@ CompositeMeterRegistry meterRegistry() {
77
120
Environment environment () {
78
121
return this .environment ;
79
122
}
123
+
124
+ private static class Endpoint {
125
+
126
+ private final String path , description ;
127
+ private final HttpHandler handler ;
128
+
129
+ private Endpoint (String path , String description , HttpHandler handler ) {
130
+ this .path = path .startsWith ("/" ) ? path : "/" + path ;
131
+ this .description = description ;
132
+ this .handler = handler ;
133
+ }
134
+ }
80
135
}
0 commit comments