Skip to content

Commit

Permalink
[HWKMETRICS-719] Log REST API requests execution time.
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvp8510 authored and John Sanda committed Sep 15, 2017
1 parent 3e70024 commit 53c0a8a
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/metrics-api-jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@
<version>${version.org.infinispan.wildfly}</version>
</dependency>

<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<scope>provided</scope>
</dependency>

<!-- documentation -->
<dependency>
<groupId>io.swagger</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hawkular.metrics.api.jaxrs.log.time;

import org.hawkular.metrics.api.jaxrs.log.RestLogger;
import org.hawkular.metrics.api.jaxrs.log.RestLogging;

import io.undertow.server.ExchangeCompletionListener;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;

public class RequestTimeLogger implements HttpHandler {

private static final RestLogger log = RestLogging.getRestLogger(RequestTimeLogger.class);

private HttpHandler next;

private long timeThreshold;

public RequestTimeLogger(HttpHandler next, long timeThreshold) {
this.next = next;
this.timeThreshold = timeThreshold;
}

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {

TimeMeasurer timeMeasurer = new TimeMeasurer(this.timeThreshold);
timeMeasurer.setStartTime(System.currentTimeMillis());
exchange.addExchangeCompleteListener(timeMeasurer);
next.handleRequest(exchange);
}

private class TimeMeasurer implements ExchangeCompletionListener {

private long start;

private long timeThreshold;

TimeMeasurer(long timeThreshold){
this.timeThreshold = timeThreshold;
}

@Override
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
try {
long end = System.currentTimeMillis();
long duration = end - start;
if (duration > this.timeThreshold) {
String method = exchange.getRequestMethod().toString();
String uri = exchange.getRequestURI();
String query = exchange.getQueryString();
log.warn("Request " + method + " " + uri + (query.isEmpty() ? "":("?" + query)) + " took: "+
duration + " ms, exceeds " + this.timeThreshold +" ms threshold");
}
} finally {
if (nextListener != null) {
nextListener.proceed();
}
}
}

void setStartTime(long start) {
this.start = start;
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hawkular.metrics.api.jaxrs.log.time;

import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;

import io.undertow.servlet.ServletExtension;
import io.undertow.servlet.api.DeploymentInfo;


public class TimerLoggerServletExtension implements ServletExtension {

private RequestTimeLogger requestTimeLogger;

private static final String PROPERTY_FILE = "/WEB-INF/time-logger-filter.properties";


@Override
public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {

long timeThreshold = 0;

InputStream is = servletContext.getResourceAsStream(PROPERTY_FILE);

if (is != null) {
try {
String timeThresholdString;
Properties props = new Properties();
props.load(is);
timeThresholdString = props.getProperty("logging-time-threshold");
if (timeThresholdString != null && !timeThresholdString.isEmpty()) {
timeThreshold = Long.parseLong(timeThresholdString);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

final long cTimeThreshold = timeThreshold;

if (timeThreshold > 0) {
deploymentInfo.addInitialHandlerChainWrapper(containerHandler -> {
requestTimeLogger = new RequestTimeLogger(containerHandler, cTimeThreshold);
return requestTimeLogger;
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
# and other contributors as indicated by the @author tags.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

org.hawkular.metrics.api.jaxrs.log.time.TimerLoggerServletExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
# and other contributors as indicated by the @author tags.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

logging-time-threshold = 5

0 comments on commit 53c0a8a

Please sign in to comment.