-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add per-path configurability (#1891)
Allows URIs served by the `AdminServlet` to be individually enabled/disabled.
- Loading branch information
1 parent
7fd7269
commit 32b6e3f
Showing
3 changed files
with
216 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
metrics-servlets/src/test/java/com/codahale/metrics/servlets/AdminServletExclusionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.codahale.metrics.servlets; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.health.HealthCheckRegistry; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.eclipse.jetty.http.HttpHeader; | ||
import org.eclipse.jetty.servlet.ServletTester; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class AdminServletExclusionTest extends AbstractServletTest { | ||
private final MetricRegistry registry = new MetricRegistry(); | ||
private final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry(); | ||
|
||
@Override | ||
protected void setUp(ServletTester tester) { | ||
tester.setContextPath("/context"); | ||
|
||
tester.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry", registry); | ||
tester.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.registry", healthCheckRegistry); | ||
tester.setInitParameter("threads-enabled", "false"); | ||
tester.setInitParameter("cpu-profile-enabled", "false"); | ||
tester.addServlet(AdminServlet.class, "/admin"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
request.setMethod("GET"); | ||
request.setURI("/context/admin"); | ||
request.setVersion("HTTP/1.0"); | ||
} | ||
|
||
@Test | ||
public void returnsA200() throws Exception { | ||
processRequest(); | ||
|
||
assertThat(response.getStatus()) | ||
.isEqualTo(200); | ||
assertThat(response.getContent()) | ||
.isEqualTo(String.format( | ||
"<!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</title>%n" + | ||
"</head>%n" + | ||
"<body>%n" + | ||
" <h1>Operational Menu</h1>%n" + | ||
" <ul>%n" + | ||
" <li><a href=\"/context/admin/metrics?pretty=true\">Metrics</a></li>%n" + | ||
" <li><a href=\"/context/admin/ping\">Ping</a></li>%n" + | ||
" <li><a href=\"/context/admin/healthcheck?pretty=true\">Healthcheck</a></li>%n" + | ||
" </ul>%n" + | ||
"</body>%n" + | ||
"</html>%n" | ||
)); | ||
assertThat(response.get(HttpHeader.CONTENT_TYPE)) | ||
.isEqualTo("text/html;charset=UTF-8"); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
metrics-servlets/src/test/java/com/codahale/metrics/servlets/AdminServletUriTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.codahale.metrics.servlets; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.health.HealthCheckRegistry; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.eclipse.jetty.http.HttpHeader; | ||
import org.eclipse.jetty.servlet.ServletTester; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class AdminServletUriTest extends AbstractServletTest { | ||
private final MetricRegistry registry = new MetricRegistry(); | ||
private final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry(); | ||
|
||
@Override | ||
protected void setUp(ServletTester tester) { | ||
tester.setContextPath("/context"); | ||
|
||
tester.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry", registry); | ||
tester.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.registry", healthCheckRegistry); | ||
tester.setInitParameter("metrics-uri", "/metrics-test"); | ||
tester.setInitParameter("ping-uri", "/ping-test"); | ||
tester.setInitParameter("threads-uri", "/threads-test"); | ||
tester.setInitParameter("healthcheck-uri", "/healthcheck-test"); | ||
tester.setInitParameter("cpu-profile-uri", "/pprof-test"); | ||
tester.addServlet(AdminServlet.class, "/admin"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
request.setMethod("GET"); | ||
request.setURI("/context/admin"); | ||
request.setVersion("HTTP/1.0"); | ||
} | ||
|
||
@Test | ||
public void returnsA200() throws Exception { | ||
processRequest(); | ||
|
||
assertThat(response.getStatus()) | ||
.isEqualTo(200); | ||
assertThat(response.getContent()) | ||
.isEqualTo(String.format( | ||
"<!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</title>%n" + | ||
"</head>%n" + | ||
"<body>%n" + | ||
" <h1>Operational Menu</h1>%n" + | ||
" <ul>%n" + | ||
" <li><a href=\"/context/admin/metrics-test?pretty=true\">Metrics</a></li>%n" + | ||
" <li><a href=\"/context/admin/ping-test\">Ping</a></li>%n" + | ||
" <li><a href=\"/context/admin/threads-test\">Threads</a></li>%n" + | ||
" <li><a href=\"/context/admin/healthcheck-test?pretty=true\">Healthcheck</a></li>%n" + | ||
" <li><a href=\"/context/admin/pprof-test\">CPU Profile</a></li>%n" + | ||
" <li><a href=\"/context/admin/pprof-test?state=blocked\">CPU Contention</a></li>%n" + | ||
" </ul>%n" + | ||
"</body>%n" + | ||
"</html>%n" | ||
)); | ||
assertThat(response.get(HttpHeader.CONTENT_TYPE)) | ||
.isEqualTo("text/html;charset=UTF-8"); | ||
} | ||
} |