Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add thread_pool and tcp connections to /varz default output

Test with GET /varz, should contain Catalina with thread_pool data.
Also added data_source.

[Fixes #37688901] Add thread_pool and tcp connections to /varz default output

Change-Id: I8d6cb1ad1bcd88e1878f145d8ec8e63ee2049bbc
  • Loading branch information...
commit af4c14044b50f2b3ed63a254fe31155d42f89290 1 parent 2b9cd74
Dave Syer dsyer authored
104 common/src/main/java/org/cloudfoundry/identity/uaa/varz/VarzEndpoint.java
View
@@ -10,11 +10,14 @@
package org.cloudfoundry.identity.uaa.varz;
import java.io.IOException;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@@ -26,6 +29,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cloudfoundry.identity.uaa.util.UaaStringUtils;
+import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.expression.MapAccessor;
import org.springframework.core.env.Environment;
@@ -56,6 +60,8 @@
private Properties buildProperties = new Properties();
+ private ObjectMapper objectMapper = new ObjectMapper();
+
public VarzEndpoint() {
try {
buildProperties = PropertiesLoaderUtils.loadAllProperties("build.properties");
@@ -106,6 +112,10 @@ public void setServer(MBeanServerConnection server) {
result.put("mem", getValueFromMap(memory, "memory.heap_memory_usage.used", Long.class) / 1024);
result.put("memory", getValueFromMap(memory, "memory"));
+ Map<String, ?> tomcat = getDomain("Catalina", "*");
+ putIfNotNull(result, "thread_pool", tomcat.get("thread_pool"));
+ putIfNotNull(result, "global_request_processor", tomcat.get("global_request_processor"));
+
if (!buildProperties.isEmpty()) {
result.put("app", UaaStringUtils.getMapFromProperties(buildProperties, "build."));
}
@@ -122,13 +132,15 @@ public void setServer(MBeanServerConnection server) {
// Information about audit (counts)
putIfNotNull(result, "audit_service",
getValueFromMap(spring, "#this['logging_audit_service']?.logging_audit_service"));
+ // Information about data source
+ putIfNotNull(result, "data_source", getValueFromMap(spring, "#this['data_source']?.data_source"));
}
// Application config properties
putIfNotNull(result, "config", environmentProperties);
if (environment != null) {
result.put("spring.profiles.active", environment.getActiveProfiles());
}
- return result;
+ return sanitize(result);
}
private Map<String, String> getLinks(String baseUrl, Collection<String> paths) {
@@ -213,30 +225,94 @@ private Object getValueFromMap(Map<String, ?> map, String path) throws Exception
// Prevent known stack overflows introspecting tomcat mbeans
if (domain.equals("Catalina") || domain.equals("*") || domain.equals("tomcat")) {
- if (!pattern.contains("type=")) {
- pattern = "type=GlobalRequestProcessor," + pattern;
+
+ // Restrict the types that can be used
+ Map<String, Object> beans = new LinkedHashMap<String, Object>();
+ List<String> types = Arrays.asList("GlobalRequestProcessor", "ThreadPool");
+ if (pattern.contains("type=GlobalRequestProcessor")) {
+ types = Arrays.asList("GlobalRequestProcessor");
}
- result.putAll(getMBeans("Catalina", "*"));
- Map<String, ?> tomcat = getMBeans("*", "type=GlobalRequestProcessor,*");
- if (!tomcat.isEmpty()) {
- if (tomcat.size() == 1) {
- // tomcat 6.0.23, 6.0.35 have different default domains so normalize...
- @SuppressWarnings("unchecked")
- Map<String, ?> map = (Map<String, ?>) tomcat.values().iterator().next();
- result.putAll(map);
- }
- else {
- result.putAll(tomcat);
+ else if (pattern.contains("type=ThreadPool")) {
+ types = Arrays.asList("ThreadPool");
+ }
+ else if (pattern.contains("type=")) {
+ beans.put("ignored_pattern", pattern);
+ beans.put("message",
+ "Tomcat MBeans are not available except 'type=GlobalRequestProcessor,*' or 'type=ThreadPool,*'");
+ pattern = "*"; // ignore other types
+ types = Collections.emptyList();
+ }
+
+ for (String type : types) {
+ Map<String, ?> tomcat = getMBeans("*", pattern.contains("type=") ? pattern : "type=" + type + ","
+ + pattern);
+ if (!tomcat.isEmpty()) {
+ if (tomcat.size() == 1) {
+ // tomcat 6.0.23, 6.0.35 have different default domains so normalize...
+ @SuppressWarnings("unchecked")
+ Map<String, ?> map = (Map<String, ?>) tomcat.values().iterator().next();
+ beans.putAll(map);
+ }
+ else {
+ beans.putAll(tomcat);
+ }
}
+
}
+
+ result.put("Catalina", beans);
}
else {
result.putAll(getMBeans(domain, pattern));
}
+ // Don't need the key if there's only the domain (normally the case)
+ if (result.size() == 1) {
+ result = getMap(result, domain);
+ }
+
+ return sanitize(result);
+
+ }
+
+ private Map<String, ?> sanitize(Map<String, Object> input) {
+ Map<String, Object> result = new LinkedHashMap<String, Object>(input);
+ doSanitize(result);
return result;
}
+ private void doSanitize(Map<String, Object> result) {
+ LinkedHashSet<String> keys = new LinkedHashSet<String>(result.keySet());
+ for (String key : keys) {
+ Object value = result.remove(key);
+ key = unquote(key);
+ if (value instanceof Map) {
+ @SuppressWarnings("unchecked")
+ Map<String, Object> map = new LinkedHashMap<String, Object>((Map<String, Object>) value);
+ doSanitize(map);
+ result.put(key, map);
+ }
+ else {
+ try {
+ result.put(key, objectMapper.readValue(objectMapper.writeValueAsString(value), Object.class));
+ }
+ catch (Exception e) {
+ result.put(key, "error:<" + e.getMessage() + ">");
+ }
+ }
+ }
+ }
+
+ private String unquote(String key) {
+ if (key.startsWith("\"")) {
+ key = key.substring(1);
+ }
+ if (key.endsWith("\"")) {
+ key = key.substring(0,key.length()-1);
+ }
+ return key;
+ }
+
private Map<String, ?> getMBeans(String domain, String pattern) throws Exception {
Set<ObjectName> names = server.queryNames(ObjectName.getInstance(domain + ":" + pattern), null);
2  common/src/test/java/org/cloudfoundry/identity/uaa/varz/VarzEndpointTests.java
View
@@ -76,7 +76,7 @@ public void testDefaultVarz() throws Exception {
Map<String, ?> varz = endpoint.getVarz("http://uua.vcap.me");
// System.err.println(varz);
assertNotNull(varz.get("mem"));
- assertTrue((Long)varz.get("mem")<1024*1024); // kB, so less than 1GB
+ assertTrue((Integer)varz.get("mem")<1024*1024); // kB, so less than 1GB
}
@Test
1  uaa/src/test/java/org/cloudfoundry/identity/uaa/integration/VarzEndpointIntegrationTests.java
View
@@ -53,6 +53,7 @@ public void testHappyDay() throws Exception {
String map = response.getBody();
assertTrue(map.contains("spring.application"));
+ assertTrue(map.contains("Catalina"));
}
Please sign in to comment.
Something went wrong with that request. Please try again.