1717
1818package org .apache .hugegraph .api .filter ;
1919
20- import static org .apache .hugegraph .api .filter .PathFilter .REQUEST_PARAMS_JSON ;
2120import static org .apache .hugegraph .api .filter .PathFilter .REQUEST_TIME ;
2221import static org .apache .hugegraph .metrics .MetricsUtil .METRICS_PATH_FAILED_COUNTER ;
2322import static org .apache .hugegraph .metrics .MetricsUtil .METRICS_PATH_RESPONSE_TIME_HISTOGRAM ;
2423import static org .apache .hugegraph .metrics .MetricsUtil .METRICS_PATH_SUCCESS_COUNTER ;
2524import static org .apache .hugegraph .metrics .MetricsUtil .METRICS_PATH_TOTAL_COUNTER ;
2625
2726import java .io .IOException ;
27+ import java .net .URI ;
2828
2929import org .apache .hugegraph .config .HugeConfig ;
3030import org .apache .hugegraph .config .ServerOptions ;
3131import org .apache .hugegraph .metrics .MetricsUtil ;
32- import org .apache .hugegraph .metrics .SlowQueryLog ;
33- import org .apache .hugegraph .util .JsonUtil ;
3432import org .apache .hugegraph .util .Log ;
3533import org .slf4j .Logger ;
3634
4240import jakarta .ws .rs .core .Context ;
4341import jakarta .ws .rs .ext .Provider ;
4442
45-
43+ // TODO: should add test for this class
4644@ Provider
4745@ Singleton
4846public class AccessLogFilter implements ContainerResponseFilter {
4947
50- private static final String DELIMETER = "/" ;
48+ private static final Logger LOG = Log .logger (AccessLogFilter .class );
49+
50+ private static final String DELIMITER = "/" ;
5151 private static final String GRAPHS = "graphs" ;
5252 private static final String GREMLIN = "gremlin" ;
5353 private static final String CYPHER = "cypher" ;
5454
55- private static final Logger LOG = Log .logger (AccessLogFilter .class );
56-
5755 @ Context
5856 private jakarta .inject .Provider <HugeConfig > configProvider ;
5957
58+ public static boolean needRecordLog (ContainerRequestContext context ) {
59+ // TODO: add test for 'path' result ('/gremlin' or 'gremlin')
60+ String path = context .getUriInfo ().getPath ();
61+
62+ // GraphsAPI/CypherAPI/Job GremlinAPI
63+ if (path .startsWith (GRAPHS )) {
64+ if (HttpMethod .GET .equals (context .getMethod ()) || path .endsWith (CYPHER )) {
65+ return true ;
66+ }
67+ }
68+ // Direct GremlinAPI
69+ return path .endsWith (GREMLIN );
70+ }
71+
72+ private String join (String path1 , String path2 ) {
73+ return String .join (DELIMITER , path1 , path2 );
74+ }
75+
6076 /**
6177 * Use filter to log request info
6278 *
6379 * @param requestContext requestContext
6480 * @param responseContext responseContext
6581 */
6682 @ Override
67- public void filter (ContainerRequestContext requestContext , ContainerResponseContext responseContext ) throws IOException {
83+ public void filter (ContainerRequestContext requestContext ,
84+ ContainerResponseContext responseContext ) throws IOException {
6885 // Grab corresponding request / response info from context;
69- String method = requestContext .getRequest ().getMethod ();
70- String path = requestContext .getUriInfo ().getPath ();
86+ URI uri = requestContext .getUriInfo ().getRequestUri ();
87+ String path = uri .getRawPath ();
88+ String method = requestContext .getMethod ();
7189 String metricsName = join (path , method );
7290
7391 MetricsUtil .registerCounter (join (metricsName , METRICS_PATH_TOTAL_COUNTER )).inc ();
@@ -77,48 +95,28 @@ public void filter(ContainerRequestContext requestContext, ContainerResponseCont
7795 MetricsUtil .registerCounter (join (metricsName , METRICS_PATH_FAILED_COUNTER )).inc ();
7896 }
7997
80- // get responseTime
8198 Object requestTime = requestContext .getProperty (REQUEST_TIME );
82- if (requestTime != null ){
99+ if (requestTime != null ) {
83100 long now = System .currentTimeMillis ();
84101 long start = (Long ) requestTime ;
85- long responseTime = now - start ;
102+ long executeTime = now - start ;
86103
87- MetricsUtil .registerHistogram (
88- join (metricsName , METRICS_PATH_RESPONSE_TIME_HISTOGRAM ))
89- .update (responseTime );
104+ MetricsUtil .registerHistogram (join (metricsName , METRICS_PATH_RESPONSE_TIME_HISTOGRAM ))
105+ .update (executeTime );
90106
91107 HugeConfig config = configProvider .get ();
92108 long timeThreshold = config .get (ServerOptions .SLOW_QUERY_LOG_TIME_THRESHOLD );
93-
94- // record slow query log
95- if ( timeThreshold > 0 && isSlowQueryLogWhiteAPI (requestContext ) && responseTime > timeThreshold ) {
96- SlowQueryLog log = new SlowQueryLog ( responseTime , start , ( String ) requestContext . getProperty ( REQUEST_PARAMS_JSON ),
97- method , timeThreshold , path );
98- LOG . info ( "Slow query: {}" , JsonUtil . toJson ( log ));
109+ // Record slow query if meet needs, watch out the perf
110+ if ( timeThreshold > 0 && executeTime > timeThreshold &&
111+ needRecordLog (requestContext )) {
112+ // TODO: set RequestBody null, handle it later & should record "client IP"
113+ LOG . info ( "[Slow Query] execTime={}ms, body={}, method={}, path={}, query={}" ,
114+ executeTime , null , method , path , uri . getQuery ( ));
99115 }
100116 }
101117 }
102118
103- private String join (String path1 , String path2 ) {
104- return String .join (DELIMETER , path1 , path2 );
105- }
106-
107- private boolean statusOk (int status ){
108- return status == 200 || status == 201 || status == 202 ;
109- }
110-
111- public static boolean isSlowQueryLogWhiteAPI (ContainerRequestContext context ) {
112- String path = context .getUriInfo ().getPath ();
113- String method = context .getRequest ().getMethod ();
114-
115- // GraphsAPI/CypherAPI/Job GremlinAPI
116- if (path .startsWith (GRAPHS )) {
117- if (method .equals (HttpMethod .GET ) || path .endsWith (CYPHER ) || path .endsWith (GREMLIN ) ){
118- return true ;
119- }
120- }
121- // Raw GremlinAPI
122- return path .startsWith (GREMLIN );
119+ private boolean statusOk (int status ) {
120+ return status >= 200 && status < 300 ;
123121 }
124122}
0 commit comments