forked from dropwizard/metrics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdminServlet.java
executable file
·127 lines (113 loc) · 5.7 KB
/
AdminServlet.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.yammer.metrics.reporting;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;
public class AdminServlet extends HttpServlet {
private static final long serialVersionUID = 1363903248255082791L;
private static final String TEMPLATE = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" +
" \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html>\n" +
"<head>\n" +
" <title>Metrics{8}</title>\n" +
"</head>\n" +
"<body>\n" +
" <h1>Operational Menu{8}</h1>\n" +
" <ul>\n" +
" <li><a href=\"{0}{1}?pretty=true\">Metrics</a></li>\n" +
" <li><a href=\"{2}{3}\">Ping</a></li>\n" +
" <li><a href=\"{4}{5}\">Threads</a></li>\n" +
" <li><a href=\"{6}{7}\">Healthcheck</a></li>\n" +
" </ul>\n" +
"</body>\n" +
"</html>";
public static final String DEFAULT_HEALTHCHECK_URI = "/healthcheck";
public static final String DEFAULT_METRICS_URI = "/metrics";
public static final String DEFAULT_PING_URI = "/ping";
public static final String DEFAULT_THREADS_URI = "/threads";
private static final String CONTENT_TYPE = "text/html";
private final HealthCheckServlet healthCheckServlet;
private final MetricsServlet metricsServlet;
private final PingServlet pingServlet;
private final ThreadDumpServlet threadDumpServlet;
private String metricsUri;
private String pingUri;
private String threadsUri;
private String healthcheckUri;
private String serviceName;
public AdminServlet() {
this(new HealthCheckServlet(), new MetricsServlet(), new PingServlet(),
new ThreadDumpServlet(), DEFAULT_HEALTHCHECK_URI, DEFAULT_METRICS_URI,
DEFAULT_PING_URI, DEFAULT_THREADS_URI);
}
public AdminServlet(HealthCheckServlet healthCheckServlet,
MetricsServlet metricsServlet,
PingServlet pingServlet,
ThreadDumpServlet threadDumpServlet,
String healthcheckUri,
String metricsUri,
String pingUri,
String threadsUri) {
this.healthCheckServlet = healthCheckServlet;
this.metricsServlet = metricsServlet;
this.pingServlet = pingServlet;
this.threadDumpServlet = threadDumpServlet;
this.metricsUri = metricsUri;
this.pingUri = pingUri;
this.threadsUri = threadsUri;
this.healthcheckUri = healthcheckUri;
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
healthCheckServlet.init(config);
metricsServlet.init(config);
pingServlet.init(config);
threadDumpServlet.init(config);
//final ServletContext context = config.getServletContext();
this.metricsUri = getParam(config.getInitParameter("metrics-uri"), this.metricsUri);
this.pingUri = getParam(config.getInitParameter("ping-uri"), this.pingUri);
this.threadsUri = getParam(config.getInitParameter("threads-uri"), this.threadsUri);
this.healthcheckUri = getParam(config.getInitParameter("healthcheck-uri"), this.healthcheckUri);
this.serviceName = getParam(config.getInitParameter("service-name"), this.serviceName);
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
final String uri = req.getPathInfo();
final String path = req.getContextPath() + req.getServletPath();
if (uri == null || uri.equals("/")) {
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(CONTENT_TYPE);
final PrintWriter writer = resp.getWriter();
try {
writer.println(MessageFormat.format(TEMPLATE, path, metricsUri, path, pingUri, path,
threadsUri, path, healthcheckUri,
serviceName == null ? "" : " (" + serviceName + ")"));
} finally {
writer.close();
}
} else if (uri.equals(healthcheckUri)) {
healthCheckServlet.service(req, resp);
} else if (uri.startsWith(metricsUri)) {
metricsServlet.service(req, resp);
} else if (uri.equals(pingUri)) {
pingServlet.service(req, resp);
} else if (uri.equals(threadsUri)) {
threadDumpServlet.service(req, resp);
} else {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
private static String getParam(String initParam, String defaultValue) {
return initParam == null ? defaultValue : initParam;
}
}