-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMetricsConsolePage.java
95 lines (83 loc) · 3.79 KB
/
MetricsConsolePage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package in.yunyul.vertx.console.metrics;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import in.yunyul.prometheus.extras.AdditionalJVMExports;
import in.yunyul.prometheus.extras.DropwizardTimerRateExports;
import in.yunyul.vertx.console.base.ConsolePage;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.client.hotspot.DefaultExports;
import io.prometheus.client.vertx.MetricsHandler;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
@SuppressWarnings("unused")
public class MetricsConsolePage implements ConsolePage {
private static final String JSON_CONTENT_TYPE = "application/json";
private CollectorRegistry registry;
// Dropwizard helper function
public static MetricsConsolePage create(MetricRegistry dropwizardRegistry) {
CollectorRegistry defaultRegistry = CollectorRegistry.defaultRegistry;
defaultRegistry.register(new DropwizardExports(dropwizardRegistry));
DefaultExports.initialize();
new AdditionalJVMExports().register();
new DropwizardTimerRateExports(dropwizardRegistry).register();
return create(defaultRegistry);
}
public static MetricsConsolePage create() {
return new MetricsConsolePage(CollectorRegistry.defaultRegistry);
}
public static MetricsConsolePage create(CollectorRegistry registry) {
return new MetricsConsolePage(registry);
}
public MetricsConsolePage(CollectorRegistry registry) {
this.registry = registry;
}
@Override
public void mount(Vertx vertx, Router router, String basePath) {
router.route(basePath + "/metrics").handler(new MetricsHandler(registry));
router.route(basePath + "/verticle*").handler(BodyHandler.create());
router.route(basePath + "/verticle/deploy")
.consumes(JSON_CONTENT_TYPE).produces(JSON_CONTENT_TYPE)
.handler(ctx -> {
JsonObject body = ctx.getBodyAsJson();
String name = body.getString("name");
boolean isWorker = body.getBoolean("isWorker");
boolean ha = body.getBoolean("ha");
int instances = body.getInteger("instances");
JsonObject deployConfig = body.getJsonObject("deployConfig");
DeploymentOptions deploymentOptions = new DeploymentOptions()
.setWorker(isWorker)
.setHa(ha)
.setInstances(instances);
if (deployConfig != null && deployConfig.size() > 0) {
deploymentOptions.setConfig(deployConfig);
}
// TODO: Completion handler
vertx.deployVerticle(name, deploymentOptions,
result -> {
JsonObject response = new JsonObject();
if (result.succeeded()) {
response.put("status", 200);
} else {
response.put("status", 400);
}
ctx.response().putHeader("content-type", JSON_CONTENT_TYPE).end(response.toString());
});
});
}
@Override
public String getLoaderFileName() {
return "/js/metrics.js";
}
private static boolean classExists(String name) {
try {
Class.forName(name);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}