Reduce noise in /end actuator docs

Update the `/env` sample used in the actuator docs to use only a limited
set of keys.

Fixes gh-5849
This commit is contained in:
Phillip Webb 2016-07-19 09:41:12 -07:00
parent 1080b990e8
commit 05cde789e1
3 changed files with 124 additions and 3 deletions

View File

@ -0,0 +1,99 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.hypermedia;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
* {@link EnvironmentEndpoint} with reduced output to look better in the documentation.
*
* @author Phillip Webb
*/
public class LimitedEnvironmentEndpoint extends EnvironmentEndpoint {
private static final MultiValueMap<String, String> INCLUDES;
static {
Map<String, List<String>> includes = new LinkedHashMap<String, List<String>>();
List<String> systemProperties = new ArrayList<String>();
systemProperties.add("java.runtime.name");
systemProperties.add("sun.boot.library.path");
systemProperties.add("java.vendor.url");
systemProperties.add("path.separator");
systemProperties.add("sun.java.launcher");
systemProperties.add("java.runtime.version");
systemProperties.add("os.arch");
systemProperties.add("line.separator");
systemProperties.add("os.name");
systemProperties.add("user.timezone");
systemProperties.add("file.encoding");
systemProperties.add("java.vm.specification.version");
systemProperties.add("sun.java.command");
systemProperties.add("sun.arch.data.model");
systemProperties.add("user.language");
systemProperties.add("awt.toolkit");
systemProperties.add("java.awt.headless");
systemProperties.add("java.vendor");
systemProperties.add("file.separator");
includes.put("systemProperties", systemProperties);
List<String> systemEnvironment = new ArrayList<String>();
systemEnvironment.add("SHELL");
systemEnvironment.add("TMPDIR");
systemEnvironment.add("DISPLAY");
systemEnvironment.add("LOGNAME");
includes.put("systemEnvironment", systemEnvironment);
INCLUDES = new LinkedMultiValueMap<>(Collections.unmodifiableMap(includes));
}
@Override
public Object sanitize(String name, Object object) {
System.out.println(name);
if (name.equals("gopherProxySet")) {
return object;
}
return null;
}
@Override
protected Map<String, Object> postProcessSourceProperties(String sourceName,
Map<String, Object> properties) {
List<String> sourceIncludes = INCLUDES.get(sourceName);
if (sourceIncludes != null) {
Set<Entry<String, Object>> entries = properties.entrySet();
Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
while (iterator.hasNext()) {
if (!sourceIncludes.contains(iterator.next().getKey())) {
iterator.remove();
}
}
}
return properties;
}
}

View File

@ -20,6 +20,7 @@ import groovy.text.GStringTemplateEngine;
import groovy.text.TemplateEngine; import groovy.text.TemplateEngine;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
@ -36,6 +37,11 @@ public class SpringBootHypermediaApplication {
return new GStringTemplateEngine(); return new GStringTemplateEngine();
} }
@Bean
public EnvironmentEndpoint environmentEndpoint() {
return new LimitedEnvironmentEndpoint();
}
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SpringBootHypermediaApplication.class, args); SpringApplication.run(SpringBootHypermediaApplication.class, args);
} }

View File

@ -61,11 +61,14 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> {
String sourceName = entry.getKey(); String sourceName = entry.getKey();
if (source instanceof EnumerablePropertySource) { if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source; EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
Map<String, Object> map = new LinkedHashMap<String, Object>(); Map<String, Object> properties = new LinkedHashMap<String, Object>();
for (String name : enumerable.getPropertyNames()) { for (String name : enumerable.getPropertyNames()) {
map.put(name, sanitize(name, enumerable.getProperty(name))); properties.put(name, sanitize(name, enumerable.getProperty(name)));
}
properties = postProcessSourceProperties(sourceName, properties);
if (properties != null) {
result.put(sourceName, properties);
} }
result.put(sourceName, map);
} }
} }
return result; return result;
@ -104,4 +107,17 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> {
return this.sanitizer.sanitize(name, object); return this.sanitizer.sanitize(name, object);
} }
/**
* Apply any post processing to source data before it is added.
* @param sourceName the source name
* @param properties the properties
* @return the post-processed properties or {@code null} if the source should not be
* added
* @since 1.4.0
*/
protected Map<String, Object> postProcessSourceProperties(String sourceName,
Map<String, Object> properties) {
return properties;
}
} }