-
Notifications
You must be signed in to change notification settings - Fork 74
Getting Started
Tadaya Tsuyukubo edited this page Oct 13, 2017
·
2 revisions
Documentation has moved to own html from wiki.
Leaving old wiki just for reference.
Use Case:
- log all queries to System.out as JSON
- log query metrics to commons logger at the end of each http request using servlet-filter
Install datasource-proxy
and commons-logging
since we chose to use commons logger for query metrics.
<dependency>
<groupId>net.ttddyy</groupId>
<artifactId>datasource-proxy</artifactId>
<version>[LATEST-VERSION]</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>[VERSION]</version>
</dependency>
add CommonsQueryCountLoggingServletFilter
for query metrics
<filter>
<filter-name>queryCountFilter</filter-name>
<filter-class>net.ttddyy.dsproxy.support.CommonsQueryCountLoggingServletFilter</filter-class>
<!-- Default to DEBUG level, use "logLevel" filter param to change the log level -->
</filter>
<filter-mapping>
<filter-name>queryCountFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
Create a datasource with configuration:
- log query execution to System.out as json
- collect query metrics
Java based
@Bean
public DataSource dataSource(DataSource actualDataSource) {
return ProxyDataSourceBuilder
.create(actualDataSource)
.name("dataSource")
.logQueryToSysOut() // log query execution to System.out
.asJson()
.countQuery() // collect query metrics
.build();
}
XML based(applicationContext.xml)
<bean id="dataSource" class="net.ttddyy.dsproxy.support.BeanNameProxyDataSource">
<property name="dataSource" ref="[ACTUAL_DATASOURCE_BEAN]"/>
<property name="listener" ref="listeners"/>
</bean>
<bean id="listeners" class="net.ttddyy.dsproxy.listener.ChainListener">
<property name="listeners">
<list>
<bean class="net.ttddyy.dsproxy.listener.SystemOutQueryLoggingListener">
<property name="writeAsJson" value="true" />
</bean>
<bean class="net.ttddyy.dsproxy.listener.DataSourceQueryCountListener"/>
</list>
</property>
</bean>
Write a simple controller that returns query metrics info as json. (using spring-boot)
@RestController
public class MyController {
@RequestMapping
public QueryCount index() throws Exception {
// do some database operations
return QueryCountHolder.getGrandTotal();
}
}
Display query metrics on jsp.
<%@ taglib prefix="dsp" uri="http://www.ttddyy.net/dsproxy/tags" %>
<dsp:metrics metric="select"/> - Select
<dsp:metrics metric="update" dataSource="FOO" /> - Num of update queries for datasource FOO
<dsp:metrics metric="total"/> - Total Queries
<dsp:metrics metric="time"/> - Total TIme
> curl http://localhost:8080
{"select":1,"insert":0,"update":0,"delete":0,"other":0,"total":1,"failure":0,"success":1,"time":6}
see example projects: datasource-proxy-examples