Commit 0d62b0cb authored by Stephane Nicoll's avatar Stephane Nicoll

Move configuration of TraceEndpoint

See gh-10263
parent 9234801c
/*
* Copyright 2012-2017 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.autoconfigure.trace;
import java.util.HashSet;
import java.util.Set;
import org.springframework.boot.actuate.trace.Include;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for tracing.
*
* @author Wallace Wadge
* @author Phillip Webb
* @author Venil Noronha
* @author Madhura Bhave
* @author Stephane Nicoll
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "management.trace")
public class TraceEndpointProperties {
/**
* Items to be included in the trace. Defaults to request/response headers (including
* cookies) and errors.
*/
private Set<Include> include = new HashSet<>(Include.defaultIncludes());
public Set<Include> getInclude() {
return this.include;
}
public void setInclude(Set<Include> include) {
this.include = include;
}
}
...@@ -19,9 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.trace; ...@@ -19,9 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.trace;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.ServletRegistration; import javax.servlet.ServletRegistration;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.trace.TraceProperties;
import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.boot.actuate.trace.WebRequestTraceFilter; import org.springframework.boot.actuate.trace.WebRequestTraceFilter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
...@@ -46,28 +44,28 @@ import org.springframework.web.servlet.DispatcherServlet; ...@@ -46,28 +44,28 @@ import org.springframework.web.servlet.DispatcherServlet;
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, ServletRegistration.class }) @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, ServletRegistration.class })
@AutoConfigureAfter(TraceRepositoryAutoConfiguration.class) @AutoConfigureAfter(TraceRepositoryAutoConfiguration.class)
@ConditionalOnProperty(prefix = "management.trace.filter", name = "enabled", matchIfMissing = true) @ConditionalOnProperty(prefix = "management.trace.filter", name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(TraceProperties.class) @EnableConfigurationProperties(TraceEndpointProperties.class)
public class TraceWebFilterAutoConfiguration { public class TraceWebFilterAutoConfiguration {
private final TraceRepository traceRepository; private final TraceRepository traceRepository;
private final TraceProperties traceProperties; private final TraceEndpointProperties endpointProperties;
private final ErrorAttributes errorAttributes; private final ErrorAttributes errorAttributes;
public TraceWebFilterAutoConfiguration(TraceRepository traceRepository, public TraceWebFilterAutoConfiguration(TraceRepository traceRepository,
TraceProperties traceProperties, TraceEndpointProperties endpointProperties,
ObjectProvider<ErrorAttributes> errorAttributes) { ObjectProvider<ErrorAttributes> errorAttributes) {
this.traceRepository = traceRepository; this.traceRepository = traceRepository;
this.traceProperties = traceProperties; this.endpointProperties = endpointProperties;
this.errorAttributes = errorAttributes.getIfAvailable(); this.errorAttributes = errorAttributes.getIfAvailable();
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public WebRequestTraceFilter webRequestLoggingFilter(BeanFactory beanFactory) { public WebRequestTraceFilter webRequestLoggingFilter() {
WebRequestTraceFilter filter = new WebRequestTraceFilter(this.traceRepository, WebRequestTraceFilter filter = new WebRequestTraceFilter(this.traceRepository,
this.traceProperties); this.endpointProperties.getInclude());
if (this.errorAttributes != null) { if (this.errorAttributes != null) {
filter.setErrorAttributes(this.errorAttributes); filter.setErrorAttributes(this.errorAttributes);
} }
......
...@@ -21,7 +21,6 @@ import java.util.Map; ...@@ -21,7 +21,6 @@ import java.util.Map;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.trace.TraceProperties;
import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.boot.actuate.trace.WebRequestTraceFilter; import org.springframework.boot.actuate.trace.WebRequestTraceFilter;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
...@@ -91,7 +90,7 @@ public class TraceWebFilterAutoConfigurationTests { ...@@ -91,7 +90,7 @@ public class TraceWebFilterAutoConfigurationTests {
@Bean @Bean
public TestWebRequestTraceFilter testWebRequestTraceFilter( public TestWebRequestTraceFilter testWebRequestTraceFilter(
TraceRepository repository, TraceProperties properties) { TraceRepository repository, TraceEndpointProperties properties) {
return new TestWebRequestTraceFilter(repository, properties); return new TestWebRequestTraceFilter(repository, properties);
} }
...@@ -100,8 +99,8 @@ public class TraceWebFilterAutoConfigurationTests { ...@@ -100,8 +99,8 @@ public class TraceWebFilterAutoConfigurationTests {
static class TestWebRequestTraceFilter extends WebRequestTraceFilter { static class TestWebRequestTraceFilter extends WebRequestTraceFilter {
TestWebRequestTraceFilter(TraceRepository repository, TestWebRequestTraceFilter(TraceRepository repository,
TraceProperties properties) { TraceEndpointProperties properties) {
super(repository, properties); super(repository, properties.getInclude());
} }
@Override @Override
......
...@@ -17,23 +17,96 @@ ...@@ -17,23 +17,96 @@
package org.springframework.boot.actuate.trace; package org.springframework.boot.actuate.trace;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
* Configuration properties for tracing. * Include options for tracing.
* *
* @author Wallace Wadge * @author Wallace Wadge
* @author Phillip Webb * @since 2.0.0
* @author Venil Noronha
* @author Madhura Bhave
* @since 1.3.0
*/ */
@ConfigurationProperties(prefix = "management.trace") public enum Include {
public class TraceProperties {
/**
* Include request headers.
*/
REQUEST_HEADERS,
/**
* Include response headers.
*/
RESPONSE_HEADERS,
/**
* Include "Cookie" in request and "Set-Cookie" in response headers.
*/
COOKIES,
/**
* Include authorization header (if any).
*/
AUTHORIZATION_HEADER,
/**
* Include errors (if any).
*/
ERRORS,
/**
* Include path info.
*/
PATH_INFO,
/**
* Include the translated path.
*/
PATH_TRANSLATED,
/**
* Include the context path.
*/
CONTEXT_PATH,
/**
* Include the user principal.
*/
USER_PRINCIPAL,
/**
* Include the parameters.
*/
PARAMETERS,
/**
* Include the query string.
*/
QUERY_STRING,
/**
* Include the authentication type.
*/
AUTH_TYPE,
/**
* Include the remote address.
*/
REMOTE_ADDRESS,
/**
* Include the session ID.
*/
SESSION_ID,
/**
* Include the remote user.
*/
REMOTE_USER,
/**
* Include the time taken to service the request in milliseconds.
*/
TIME_TAKEN;
private static final Set<Include> DEFAULT_INCLUDES; private static final Set<Include> DEFAULT_INCLUDES;
...@@ -48,104 +121,11 @@ public class TraceProperties { ...@@ -48,104 +121,11 @@ public class TraceProperties {
} }
/** /**
* Items to be included in the trace. Defaults to request/response headers (including * Return the default {@link Include}.
* cookies) and errors. * @return the default include.
*/ */
private Set<Include> include = new HashSet<>(DEFAULT_INCLUDES); public static Set<Include> defaultIncludes() {
return DEFAULT_INCLUDES;
public Set<Include> getInclude() {
return this.include;
}
public void setInclude(Set<Include> include) {
this.include = include;
}
/**
* Include options for tracing.
*/
public enum Include {
/**
* Include request headers.
*/
REQUEST_HEADERS,
/**
* Include response headers.
*/
RESPONSE_HEADERS,
/**
* Include "Cookie" in request and "Set-Cookie" in response headers.
*/
COOKIES,
/**
* Include authorization header (if any).
*/
AUTHORIZATION_HEADER,
/**
* Include errors (if any).
*/
ERRORS,
/**
* Include path info.
*/
PATH_INFO,
/**
* Include the translated path.
*/
PATH_TRANSLATED,
/**
* Include the context path.
*/
CONTEXT_PATH,
/**
* Include the user principal.
*/
USER_PRINCIPAL,
/**
* Include the parameters.
*/
PARAMETERS,
/**
* Include the query string.
*/
QUERY_STRING,
/**
* Include the authentication type.
*/
AUTH_TYPE,
/**
* Include the remote address.
*/
REMOTE_ADDRESS,
/**
* Include the session ID.
*/
SESSION_ID,
/**
* Include the remote user.
*/
REMOTE_USER,
/**
* Include the time taken to service the request in milliseconds.
*/
TIME_TAKEN
} }
} }
...@@ -38,7 +38,6 @@ import javax.servlet.http.HttpSession; ...@@ -38,7 +38,6 @@ import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.trace.TraceProperties.Include;
import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -68,16 +67,26 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order ...@@ -68,16 +67,26 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order
private ErrorAttributes errorAttributes; private ErrorAttributes errorAttributes;
private final TraceProperties properties; private final Set<Include> includes;
/** /**
* Create a new {@link WebRequestTraceFilter} instance. * Create a new {@link WebRequestTraceFilter} instance.
* @param repository the trace repository * @param repository the trace repository
* @param properties the trace properties * @param includes the {@link Include} to apply
*/ */
public WebRequestTraceFilter(TraceRepository repository, TraceProperties properties) { public WebRequestTraceFilter(TraceRepository repository, Set<Include> includes) {
this.repository = repository; this.repository = repository;
this.properties = properties; this.includes = includes;
}
/**
* Create a new {@link WebRequestTraceFilter} instance with the default
* {@link Include} to apply.
* @param repository the trace repository
* @see Include#defaultIncludes()
*/
public WebRequestTraceFilter(TraceRepository repository) {
this(repository, Include.defaultIncludes());
} }
/** /**
...@@ -247,7 +256,7 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order ...@@ -247,7 +256,7 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order
} }
private boolean isIncluded(Include include) { private boolean isIncluded(Include include) {
return this.properties.getInclude().contains(include); return this.includes.contains(include);
} }
public void setErrorAttributes(ErrorAttributes errorAttributes) { public void setErrorAttributes(ErrorAttributes errorAttributes) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment