2424import java .util .Collection ;
2525import java .util .List ;
2626import java .util .Map ;
27- import java .util .concurrent .CompletionStage ;
27+ import java .util .concurrent .CompletableFuture ;
2828import java .util .concurrent .ConcurrentLinkedQueue ;
2929import java .util .concurrent .TimeUnit ;
30+ import java .util .function .BiConsumer ;
3031import java .util .function .Function ;
3132import java .util .function .Supplier ;
3233import java .util .stream .Collectors ;
3334import org .astraea .app .argument .DurationField ;
3435import org .astraea .app .argument .IntegerMapField ;
3536import org .astraea .app .argument .NonNegativeIntegerField ;
37+ import org .astraea .common .Configuration ;
3638import org .astraea .common .Utils ;
3739import org .astraea .common .admin .Admin ;
40+ import org .astraea .common .admin .Broker ;
3841import org .astraea .common .admin .NodeInfo ;
3942import org .astraea .common .metrics .JndiClient ;
4043import org .astraea .common .metrics .MBeanClient ;
4144import org .astraea .common .metrics .collector .MetricSensor ;
4245import org .astraea .common .metrics .collector .MetricStore ;
4346
4447public class WebService implements AutoCloseable {
48+ public static final String METRIC_STORE_KEY = "metric.store" ;
49+ public static final String METRIC_STORE_LOCAL = "local" ;
50+ public static final String METRIC_STORE_TOPIC = "topic" ;
51+ public static final String BOOTSTRAP_SERVERS_KEY = "bootstrap.servers" ;
4552
4653 private final HttpServer server ;
4754 private final Admin admin ;
@@ -51,32 +58,48 @@ public WebService(
5158 Admin admin ,
5259 int port ,
5360 Function <Integer , Integer > brokerIdToJmxPort ,
54- Duration beanExpiration ) {
61+ Duration beanExpiration ,
62+ Configuration config ) {
5563 this .admin = admin ;
56- Supplier <CompletionStage < Map <Integer , MBeanClient >>> clientSupplier =
64+ Supplier <Map <MetricSensor , BiConsumer < Integer , Exception >>> sensorsSupplier =
5765 () ->
58- admin
59- .brokers ()
60- .thenApply (
61- brokers ->
62- brokers .stream ()
63- .collect (
64- Collectors .toUnmodifiableMap (
65- NodeInfo ::id ,
66- b ->
67- JndiClient .of (b .host (), brokerIdToJmxPort .apply (b .id ())))));
66+ sensors .metricSensors ().stream ()
67+ .distinct ()
68+ .collect (
69+ Collectors .toUnmodifiableMap (Function .identity (), ignored -> (id , ee ) -> {}));
70+
71+ List <MetricStore .Receiver > receivers =
72+ switch (config .string (METRIC_STORE_KEY ).orElse (METRIC_STORE_LOCAL )) {
73+ case METRIC_STORE_LOCAL -> {
74+ Function <List <Broker >, Map <Integer , MBeanClient >> asBeanClientMap =
75+ brokers ->
76+ brokers .stream ()
77+ .collect (
78+ Collectors .toUnmodifiableMap (
79+ NodeInfo ::id ,
80+ b -> JndiClient .of (b .host (), brokerIdToJmxPort .apply (b .id ()))));
81+ yield List .of (
82+ MetricStore .Receiver .local (() -> admin .brokers ().thenApply (asBeanClientMap )));
83+ }
84+ case METRIC_STORE_TOPIC -> List .of (
85+ MetricStore .Receiver .topic (config .requireString (BOOTSTRAP_SERVERS_KEY )),
86+ MetricStore .Receiver .local (
87+ () -> CompletableFuture .completedStage (Map .of (-1 , JndiClient .local ()))));
88+ default -> throw new IllegalArgumentException (
89+ "unknown metric store type: "
90+ + config .string (METRIC_STORE_KEY )
91+ + ". use "
92+ + METRIC_STORE_LOCAL
93+ + " or "
94+ + METRIC_STORE_TOPIC );
95+ };
6896 var metricStore =
6997 MetricStore .builder ()
7098 .beanExpiration (beanExpiration )
71- .receivers (List .of (MetricStore .Receiver .local (clientSupplier )))
72- .sensorsSupplier (
73- () ->
74- sensors .metricSensors ().stream ()
75- .distinct ()
76- .collect (
77- Collectors .toUnmodifiableMap (
78- Function .identity (), ignored -> (id , ee ) -> {})))
99+ .receivers (receivers )
100+ .sensorsSupplier (sensorsSupplier )
79101 .build ();
102+
80103 server = Utils .packException (() -> HttpServer .create (new InetSocketAddress (port ), 0 ));
81104 server .createContext ("/topics" , to (new TopicHandler (admin )));
82105 server .createContext ("/groups" , to (new GroupHandler (admin )));
@@ -109,7 +132,11 @@ public static void main(String[] args) throws Exception {
109132 throw new IllegalArgumentException ("you must define either --jmx.port or --jmx.ports" );
110133 try (var service =
111134 new WebService (
112- Admin .of (arg .configs ()), arg .port , arg ::jmxPortMapping , arg .beanExpiration )) {
135+ Admin .of (arg .configs ()),
136+ arg .port ,
137+ arg ::jmxPortMapping ,
138+ arg .beanExpiration ,
139+ new Configuration (arg .configs ()))) {
113140 if (arg .ttl == null ) {
114141 System .out .println ("enter ctrl + c to terminate web service" );
115142 TimeUnit .MILLISECONDS .sleep (Long .MAX_VALUE );
0 commit comments