Skip to content

Commit bb6d26a

Browse files
committed
Log the entry/end for each request and the elapsed time
1 parent 3a614ea commit bb6d26a

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/util/RestInterceptor.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.commonjava.indy.bind.jaxrs.util;
1717

18+
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
1920

2021
import javax.interceptor.AroundInvoke;
@@ -23,6 +24,8 @@
2324
import javax.ws.rs.Path;
2425
import java.nio.file.Paths;
2526

27+
import static java.lang.System.currentTimeMillis;
28+
import static org.apache.commons.lang3.StringUtils.isNotBlank;
2629
import static org.commonjava.indy.util.RequestContextHelper.REST_CLASS;
2730
import static org.commonjava.indy.util.RequestContextHelper.REST_CLASS_PATH;
2831
import static org.commonjava.indy.util.RequestContextHelper.REST_ENDPOINT_PATH;
@@ -34,18 +37,25 @@
3437
@REST
3538
public class RestInterceptor
3639
{
40+
/**
41+
* Interceptor decorating MDC. Log the beginning/end of REST request and time elapsed.
42+
*/
3743
@AroundInvoke
3844
public Object operation( InvocationContext context ) throws Exception
3945
{
46+
final Logger logger = LoggerFactory.getLogger( context.getTarget().getClass() );
47+
4048
Class<?> targetClass = context.getTarget().getClass();
41-
Path classAnno = null;
49+
Path classAnno;
4250
do
4351
{
4452
classAnno = targetClass.getAnnotation( Path.class );
4553
targetClass = targetClass.getSuperclass();
4654
}
4755
while( classAnno == null && targetClass != null );
4856

57+
String endpointPath = null;
58+
4959
if ( getContext( REST_CLASS ) == null )
5060
{
5161
String targetName = context.getMethod().getDeclaringClass().getSimpleName();
@@ -64,15 +74,29 @@ public Object operation( InvocationContext context ) throws Exception
6474
String methodPath = methAnno.value();
6575
setContext( REST_METHOD_PATH, methodPath );
6676

67-
String endpointPath = Paths.get( classPath, methodPath ).toString();
77+
endpointPath = Paths.get( classPath, methodPath ).toString();
6878
setContext( REST_ENDPOINT_PATH, endpointPath );
6979
}
7080
}
7181

72-
LoggerFactory.getLogger( context.getTarget().getClass() ).trace( "Interceptor decorating MDC." );
82+
//logger.trace( "Interceptor decorating MDC." );
7383

74-
return context.proceed();
84+
final boolean isEndpoint = isNotBlank( endpointPath );
85+
final long begin = currentTimeMillis();
86+
try
87+
{
88+
if ( isEndpoint )
89+
{
90+
logger.info( "Start REST: {}", endpointPath );
91+
}
92+
return context.proceed();
93+
}
94+
finally
95+
{
96+
if ( isEndpoint )
97+
{
98+
logger.info( "End REST: {}, elapsed: {}ms", endpointPath, ( currentTimeMillis() - begin ) );
99+
}
100+
}
75101
}
76-
77-
78102
}

0 commit comments

Comments
 (0)