Fix not able to override datasource-proxy log-level (#1976)

* Fix datasource-proxy log-level is not working

- Aligned default values of `Query` & `SlowQuery` in
  `TraceJdbcProperties.DataSourceProxyProperties`
  and `DataSourceProxyProperties`
- Add code to copy properties of `Query` and `SlowQuery` in
  `DataSourceProxyConfiguration`
- Add test case to check if `log-level` can be override or not.

Fixes gh-1973

* Disable query log listener by default

- By default `QueryLogListener` and `LoggingQueryLogListener`
  will be disabled, dev need to enable it by using properties
- By default `Slf4j` will be enlabled for query logging
  so by adding below property slow query logging will be enabled
  `spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true`
  this property will register `SLF4JSlowQueryListener`

* Add docs for how-to enable data-proxy query logs
This commit is contained in:
Chintan Radia
2021-06-18 13:06:28 +05:30
committed by GitHub
parent 7c4d8b1479
commit f7eb398917
6 changed files with 67 additions and 12 deletions

View File

@@ -31,6 +31,7 @@
|spring.sleuth.jdbc.datasource-proxy.query.enable-logging | `false` | Enable logging all queries to the log.
|spring.sleuth.jdbc.datasource-proxy.query.log-level | `DEBUG` | Severity of query logger.
|spring.sleuth.jdbc.datasource-proxy.query.logger-name | | Name of query logger.
|spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging | `false` | Enable logging slow queries to the log.
|spring.sleuth.jdbc.datasource-proxy.slow-query.log-level | `WARN` | Severity of slow query logger.
|spring.sleuth.jdbc.datasource-proxy.slow-query.logger-name | | Name of slow query logger.
|spring.sleuth.jdbc.datasource-proxy.slow-query.threshold | `300` | Number of seconds to consider query as slow.

View File

@@ -720,6 +720,9 @@ Please check the <<appendix.adoc#appendix,appendix>> page under `spring.sleuth.j
You can configure P6Spy manually using one of available configuration methods. For more information please refer to the http://p6spy.readthedocs.io/en/latest/configandusage.html[P6Spy Configuration Guide].
By default logging queries will be disabled, set `spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging` to `true` to enable logging slow queries
and set `spring.sleuth.jdbc.datasource-proxy.query.enable-logging` to `true` to enable logging all queries.
In order to disable this instrumentation set `spring.sleuth.jdbc.enabled` to `false`.
[[sleuth-session-integration]]

View File

@@ -50,6 +50,7 @@ import org.springframework.context.annotation.Bean;
* {@link QueryTransformer}.
*
* @author Arthur Gavlyukovskiy
* @author Chintan Radia
*/
@ConditionalOnClass(ProxyDataSource.class)
@ConditionalOnProperty(name = "spring.sleuth.jdbc.datasource-proxy.enabled", havingValue = "true",
@@ -84,6 +85,8 @@ class DataSourceProxyConfiguration {
DataSourceProxyProperties props = new DataSourceProxyProperties();
BeanUtils.copyProperties(originalProxy, props);
props.setLogging(DataSourceProxyProperties.DataSourceProxyLogging.valueOf(originalProxy.getLogging().name()));
BeanUtils.copyProperties(originalProxy.getQuery(), props.getQuery());
BeanUtils.copyProperties(originalProxy.getSlowQuery(), props.getSlowQuery());
return props;
}

View File

@@ -253,7 +253,7 @@ public class TraceJdbcProperties {
*/
private long threshold = 300;
boolean isEnableLogging() {
public boolean isEnableLogging() {
return enableLogging;
}

View File

@@ -29,6 +29,7 @@ import net.ttddyy.dsproxy.listener.logging.CommonsQueryLoggingListener;
import net.ttddyy.dsproxy.listener.logging.CommonsSlowQueryListener;
import net.ttddyy.dsproxy.listener.logging.JULQueryLoggingListener;
import net.ttddyy.dsproxy.listener.logging.JULSlowQueryListener;
import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;
import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener;
import net.ttddyy.dsproxy.listener.logging.SLF4JSlowQueryListener;
import net.ttddyy.dsproxy.listener.logging.SystemOutQueryLoggingListener;
@@ -53,6 +54,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.filter;
class ProxyDataSourceConfigurationTests {
@@ -65,7 +67,24 @@ class ProxyDataSourceConfigurationTests {
.withClassLoader(new FilteredClassLoader("com.p6spy"));
@Test
void testRegisterLogAndSlowQueryLogByDefaultToSlf4j() {
void testNotRegisterLogAndSlowQueryLogByDefaultToSlf4j() {
contextRunner.run(context -> {
DataSource dataSource = context.getBean(DataSource.class);
ProxyDataSource proxyDataSource = (ProxyDataSource) ((DataSourceWrapper) dataSource)
.getDecoratedDataSource();
ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
assertThat(chainListener.getListeners()).extracting("class").doesNotContain(SLF4JSlowQueryListener.class);
assertThat(chainListener.getListeners()).extracting("class")
.doesNotContain(SLF4JQueryLoggingListener.class);
});
}
@Test
void testRegisterLogAndSlowQueryLogByDefaultToSlf4jWhenLoggingIsEnabled() {
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
contextRunner.run(context -> {
DataSource dataSource = context.getBean(DataSource.class);
ProxyDataSource proxyDataSource = (ProxyDataSource) ((DataSourceWrapper) dataSource)
@@ -78,8 +97,10 @@ class ProxyDataSourceConfigurationTests {
@Test
void testRegisterLogAndSlowQueryLogByUsingSlf4j() {
ApplicationContextRunner contextRunner = this.contextRunner
.withPropertyValues("spring.sleuth.jdbc.datasource-proxy.logging=slf4j");
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
"spring.sleuth.jdbc.datasource-proxy.logging=slf4j",
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
contextRunner.run(context -> {
DataSource dataSource = context.getBean(DataSource.class);
@@ -93,8 +114,10 @@ class ProxyDataSourceConfigurationTests {
@Test
void testRegisterLogAndSlowQueryLogUsingSystemOut() {
ApplicationContextRunner contextRunner = this.contextRunner
.withPropertyValues("spring.sleuth.jdbc.datasource-proxy.logging=sysout");
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
"spring.sleuth.jdbc.datasource-proxy.logging=sysout",
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
contextRunner.run(context -> {
DataSource dataSource = context.getBean(DataSource.class);
@@ -108,8 +131,10 @@ class ProxyDataSourceConfigurationTests {
@Test
void testRegisterLogAndSlowQueryLogUsingJUL() {
ApplicationContextRunner contextRunner = this.contextRunner
.withPropertyValues("spring.sleuth.jdbc.datasourceProxy.logging=jul");
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
"spring.sleuth.jdbc.datasourceProxy.logging=jul",
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
contextRunner.run(context -> {
DataSource dataSource = context.getBean(DataSource.class);
@@ -123,8 +148,10 @@ class ProxyDataSourceConfigurationTests {
@Test
void testRegisterLogAndSlowQueryLogUsingApacheCommons() {
ApplicationContextRunner contextRunner = this.contextRunner
.withPropertyValues("spring.sleuth.jdbc.datasourceProxy.logging=commons");
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
"spring.sleuth.jdbc.datasourceProxy.logging=commons",
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
contextRunner.run(context -> {
DataSource dataSource = context.getBean(DataSource.class);
@@ -193,6 +220,27 @@ class ProxyDataSourceConfigurationTests {
});
}
@Test
void testLogLevelIsCustomizableForLogAndSlowQueryInSlf4j() {
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
"spring.sleuth.jdbc.datasource-proxy.logging=slf4j",
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true",
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
"spring.sleuth.jdbc.datasource-proxy.slow-query.log-level=INFO",
"spring.sleuth.jdbc.datasource-proxy.query.log-level=INFO");
contextRunner.run(context -> {
DataSource dataSource = context.getBean(DataSource.class);
ProxyDataSource proxyDataSource = (ProxyDataSource) ((DataSourceWrapper) dataSource)
.getDecoratedDataSource();
ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
assertThat(filter(chainListener.getListeners()).with("class").equalsTo(SLF4JQueryLoggingListener.class)
.with("logLevel").equalsTo(SLF4JLogLevel.INFO).get()).hasSize(1);
assertThat(filter(chainListener.getListeners()).with("class").equalsTo(SLF4JSlowQueryListener.class)
.with("logLevel").equalsTo(SLF4JLogLevel.INFO).get()).hasSize(1);
});
}
@Configuration
static class CustomDataSourceProxyConfiguration {

View File

@@ -113,7 +113,7 @@ public class DataSourceProxyProperties {
/**
* Enable logging all queries to the log.
*/
private boolean enableLogging = true;
private boolean enableLogging = false;
/**
* Name of query logger.
@@ -164,7 +164,7 @@ public class DataSourceProxyProperties {
/**
* Enable logging slow queries to the log.
*/
private boolean enableLogging = true;
private boolean enableLogging = false;
/**
* Name of slow query logger.