Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork95
Spring Boot integration with p6spy, datasource-proxy, flexy-pool and spring-cloud-sleuth
License
gavlyukovskiy/spring-boot-data-source-decorator
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Spring Boot auto-configuration for integration with
- P6Spy - adds ability to intercept and log sql queries, including interception of a most
Connection,StatementandResultSetmethods invocations - Datasource Proxy - adds ability to intercept all queries and
Connection,StatementandResultSetmethod calls - FlexyPool - adds connection pool metrics (jmx, codahale, dropwizard) and flexible strategies for adjusting pool size on demand
Instead of using the library you can manually wrap yourDataSource, but this library also provides
- ability to use
@ConfigurationPropertiesprovided by Spring Boot (spring.datasource.hikari.*,spring.datasource.dbcp2.*) - disabling decorating by deployment property
decorator.datasource.enabled=true/false - configure proxies through spring properties
application.properties/ymland customize proxies by defining beans in the spring context
Add one of the starters to the classpath of a Spring Boot 4 application and your datasources (auto-configured or custom) will be wrapped into one of a datasource proxy providers below.
Spring Boot compatibility
| Spring Boot Version | Starter Version |
|---|---|
| 4.x | |
| 3.x | 1.12.1 |
If you want to useP6Spy
implementation("com.github.gavlyukovskiy:p6spy-spring-boot-starter:${version}")<dependency> <groupId>com.github.gavlyukovskiy</groupId> <artifactId>p6spy-spring-boot-starter</artifactId> <version>${version}</version></dependency>
implementation("com.github.gavlyukovskiy:datasource-proxy-spring-boot-starter:${version}")<dependency> <groupId>com.github.gavlyukovskiy</groupId> <artifactId>datasource-proxy-spring-boot-starter</artifactId> <version>${version}</version></dependency>
To use FlexyPool with connection pool other than HikariCP you must add
PoolAdapterfor yourparticular connection pool.
implementation("com.github.gavlyukovskiy:flexy-pool-spring-boot-starter:${version}")<dependency> <groupId>com.github.gavlyukovskiy</groupId> <artifactId>flexy-pool-spring-boot-starter</artifactId> <version>${version}</version></dependency>
You can use all decorators at the same time if you need, if so decorating order will be:
P6DataSource -> ProxyDataSource -> FlexyPoolDataSource -> DataSource
After adding p6spy starter you'll start getting all sql queries in the logs:
2017-06-07 21:42:08 INFO p6spy: #1496860928120 | took 0ms | statement | connection 0|SELECT NOW()2017-06-07 21:51:07 INFO p6spy: #1496861467802 | took 0ms | statement | connection 1|SELECT NOW()2017-06-07 21:51:07 INFO p6spy: #1496861467803 | took 0ms | statement | connection 2|SELECT NOW()2017-06-07 21:51:08 INFO p6spy: #1496861468806 | took 0ms | statement | connection 3|SELECT NOW()All beans of typeJdbcEventListener are registered in P6Spy:
@BeanpublicJdbcEventListenermyListener() {returnnewJdbcEventListener() {@OverridepublicvoidonAfterGetConnection(ConnectionInformationconnectionInformation,SQLExceptione) {System.out.println("got connection"); }@OverridepublicvoidonAfterConnectionClose(ConnectionInformationconnectionInformation,SQLExceptione) {System.out.println("connection closed"); } };}
This done by addingRuntimeListenerSupportFactory into P6Spymodulelist, overriding this property will cause to not registering factory thus listeners will not be applied
You can configure small set of parameters in yourapplication.properties:
Note
Configuration below indicates al possible parameters together with their default values anddoes not need to be set explicitly
# Register P6LogFactory to log JDBC eventsdecorator.datasource.p6spy.enable-logging=true# Use com.p6spy.engine.spy.appender.MultiLineFormat instead of com.p6spy.engine.spy.appender.SingleLineFormatdecorator.datasource.p6spy.multiline=true# Use logging for default listeners [slf4j, sysout, file, custom]decorator.datasource.p6spy.logging=slf4j# Log file to use (only with logging=file)decorator.datasource.p6spy.log-file=spy.log# Class file to use (only with logging=custom). The class must implement com.p6spy.engine.spy.appender.FormattedLoggerdecorator.datasource.p6spy.custom-appender-class=my.custom.LoggerClass# Custom log format, if specified com.p6spy.engine.spy.appender.CustomLineFormat will be used with this log format# see https://p6spy.readthedocs.io/en/latest/configandusage.html#customlogmessageformatdecorator.datasource.p6spy.log-format=# Use regex pattern to filter log messages. If specified only matched messages will be logged.decorator.datasource.p6spy.log-filter.pattern=# Exclude certain categories from logging. If specified only matched messages will be logged.# see https://p6spy.readthedocs.io/en/latest/configandusage.html#excludecategoriesdecorator.datasource.p6spy.exclude-categories=
Also you can configure P6Spy manually using one of available configuration methods. For more information please refer to theP6Spy Configuration Guide
After adding datasource-proxy starter you'll start getting all sql queries in the logs with levelDEBUG and slow sql queries with levelWARN:
2017-06-07 21:58:06 DEBUG n.t.d.l.l.SLF4JQueryLoggingListener:Name:, Time:0, Success:TrueType:Statement, Batch:False, QuerySize:1, BatchSize:0Query:["SELECT NOW()"]Params:[]2017-06-07 21:58:06 DEBUG n.t.d.l.l.SLF4JQueryLoggingListener:Name:, Time:0, Success:TrueType:Statement, Batch:False, QuerySize:1, BatchSize:0Query:["SELECT NOW()"]Params:[]2017-06-07 21:58:06.630 DEBUG 8492 --- [ool-1-worker-50] n.t.d.l.l.SLF4JQueryLoggingListener :Name:, Time:0, Success:TrueType:Statement, Batch:False, QuerySize:1, BatchSize:0Query:["SELECT NOW()"]Params:[]2017-06-07 22:10:50 WARN n.t.d.l.logging.SLF4JSlowQueryListener:Name:, Time:0, Success:FalseType:Statement, Batch:False, QuerySize:1, BatchSize:0Query:["SELECT SLEEP(301000)"]Params:[]You can add customQueryExecutionListener by registering them in the context, as well you can overrideParameterTransformer,QueryTransformer andConnectionIdManager:
@BeanpublicQueryExecutionListenerqueryExecutionListener() {returnnewQueryExecutionListener() {@OverridepublicvoidbeforeQuery(ExecutionInfoexecInfo,List<QueryInfo>queryInfoList) {System.out.println("beforeQuery"); }@OverridepublicvoidafterQuery(ExecutionInfoexecInfo,List<QueryInfo>queryInfoList) {System.out.println("afterQuery"); } };}@BeanpublicParameterTransformerparameterTransformer() {returnnewMyParameterTransformer();}@BeanpublicQueryTransformerqueryTransformer() {returnnewMyQueryTransformer();}@BeanpublicConnectionIdManagerProviderconnectionIdManagerProvider() {returnMyConnectionIdManager::new;}
You can configure logging, query/slow query listeners and more using yourapplication.properties:
Note
Configuration below indicates all possible parameters together with their default values anddoes not need to be set explicitly
# One of logging libraries (slf4j, jul, common, sysout)decorator.datasource.datasource-proxy.logging=slf4jdecorator.datasource.datasource-proxy.query.enable-logging=truedecorator.datasource.datasource-proxy.query.log-level=debug# Logger name to log all queries, default depends on chosen logging, e.g. net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListenerdecorator.datasource.datasource-proxy.query.logger-name=decorator.datasource.datasource-proxy.slow-query.enable-logging=truedecorator.datasource.datasource-proxy.slow-query.log-level=warndecorator.datasource.datasource-proxy.slow-query.logger-name=# Number of seconds to consider query as slow and log itdecorator.datasource.datasource-proxy.slow-query.threshold=300decorator.datasource.datasource-proxy.multiline=true# Formats the SQL for better readability. Uses Hibernate's formatter if present on the class path. If you opted in for a different JPA provider you need to add https://github.com/vertical-blank/sql-formatter as a runtime dependency to your app to enable this.# Mutually exclusive with json-format=truedecorator.datasource.datasource-proxy.format-sql=falsedecorator.datasource.datasource-proxy.json-format=false# Enable Query Metricsdecorator.datasource.datasource-proxy.count-query=false
Optionally, configure aLoggingFilter to control the output of query logging:
@BeanpublicLoggingFilterloggingFilter() {returnMyLoggingFilter();}
If theflexy-pool-spring-boot-starter is added to the classpath your datasource will be wrapped to theFlexyPoolDataSource.
Important
If you are not relying on Spring Boot autoconfiguration and instead declare a customDataSource bean, you need to usea concreteDataSource type as the return value (e.g.,@Bean HikariDataSource dataSource()) to match the FlexyPool adapter.
With default setting you will start getting messages about acquiring and leasing connections:
2017-07-13 01:31:02 INFO c.v.flexypool.FlexyPoolDataSource: Connection leased for 1500 millis, while threshold is set to 1000 in dataSource FlexyPoolDataSource2017-07-13 01:31:03 WARN PoolOnTimeoutConnectionAcquisitionStrategy: Connection was acquired in 1502 millis, timeoutMillis is set to 5002017-07-13 01:31:03 INFO PoolOnTimeoutConnectionAcquisitionStrategy: Pool size changed from previous value 10 to 11All beans of typeConnectionAcquisitionStrategyFactory are used to provideConnectionAcquisitionStrategy for the pool.
MetricsFactory andConnectionProxyFactory beans can be used to customize metrics and connection decorators.
EventListener<? extends Event> beans can be registered to subscribe on events of flexy-pool (e.g.ConnectionAcquisitionTimeThresholdExceededEvent,ConnectionLeaseTimeThresholdExceededEvent).
You can configure yourFlexyPoolDataSource by using beanFlexyPoolConfigurationBuilderCustomizer or properties:
Note
Configuration below indicates al possible parameters together with their default values anddoes not need to be set explicitly
# Increments pool size if connection acquisition request has timed outdecorator.datasource.flexy-pool.acquisition-strategy.increment-pool.max-overgrow-pool-size=15decorator.datasource.flexy-pool.acquisition-strategy.increment-pool.timeout-millis=500# Retries on getting connectiondecorator.datasource.flexy-pool.acquisition-strategy.retry.attempts=2# Enable metrics exporting to the JMXdecorator.datasource.flexy-pool.metrics.reporter.jmx.enabled=truedecorator.datasource.flexy-pool.metrics.reporter.jmx.auto-start=false# Millis between two consecutive log reportsdecorator.datasource.flexy-pool.metrics.reporter.log.millis=300000# Enable logging and publishing ConnectionAcquisitionTimeThresholdExceededEvent when a connection acquisition request has timed outdecorator.datasource.flexy-pool.threshold.connection.acquisition=50# Enable logging and publishing ConnectionLeaseTimeThresholdExceededEvent when a connection lease has exceeded the given time thresholddecorator.datasource.flexy-pool.threshold.connection.lease=1000
Nothing has changed, this project is continued to be supported and maintained, and can be used to enable JDBC loggingand provide auto-configuration of P6Spy, Datasource-Proxy and FlexyPool.
As of release 1.8.0 Spring Cloud Sleuth integration was deprecated in favor ofSpring Cloud Sleuth: Spring JDBCwhich provides JDBC instrumentation out of the box.
As of release 1.9.0 Spring Cloud Sleuth integration was removed.
Spring Cloud Sleuth JDBC was based on this project and keeps all functionality including logging, tracing, configuration and customizations.
- If you are using Spring Cloud Sleuth you can migrate all properties from
decorator.datasource.*tospring.sleuth.jdbc.*with minimal changes. - If you have query logging enabled (default state) then you need to explicitly enable logging using:
- P6Spy:
spring.sleuth.jdbc.p6spy.enable-logging=true - Datasource-Proxy:
spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true
- P6Spy:
- If you were using decoration customizers please consult with Spring Cloud Sleuth documentation and migrate usage of those to appropriate alternatives in Spring Cloud Sleuth
- (Optional) Replace dependency on this starter with the particular library
- P6Spy: replace
com.github.gavlyukovskiy:p6spy-spring-boot-starterwithp6spy:p6spy - Datasource-Proxy: replace
com.github.gavlyukovskiy:datasource-proxy-spring-boot-starterwithnet.ttddyy:datasource-proxy
- P6Spy: replace
- Any issues can be raised in Spring Cloud Sleuth project on GitHub, you may tag me (@gavlyukovskiy) and I'll try to help.
- Enjoy using JDBC instrumentation, and thank you for using this library :)
Due to similarities in implementation, using starters from this library together with Spring Cloud Sleuth 3.1.0 is possible, although decoration will be automatically disabled in favor of Spring Cloud Sleuth to avoid duplicated logging, tracing or any other potential issues.
Custom data source decorators are supported through declaring beans of typeDataSourceDecorator
@BeanpublicDataSourceDecoratorcustomDecorator() {return (beanName,dataSource) ->newDataSourceWrapper(dataSource);}
If you want to disable decorating setdecorator.datasource.exclude-beans with bean names you want to exclude.Also, you can disable decorating forAbstractRoutingDataSource setting propertydecorator.datasource.ignore-routing-data-sources totrueSetdecorator.datasource.enabled tofalse if you want to disable all decorators for all datasources.
About
Spring Boot integration with p6spy, datasource-proxy, flexy-pool and spring-cloud-sleuth
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.