-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HWKMETRICS-719] Log REST API requests execution time.
- Loading branch information
1 parent
3e70024
commit 53c0a8a
Showing
5 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...cs-api-jaxrs/src/main/java/org/hawkular/metrics/api/jaxrs/log/time/RequestTimeLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...rs/src/main/java/org/hawkular/metrics/api/jaxrs/log/time/TimerLoggerServletExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...trics-api-jaxrs/src/main/resources/META-INF/services/io.undertow.servlet.ServletExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
18 changes: 18 additions & 0 deletions
18
api/metrics-api-jaxrs/src/main/webapp/WEB-INF/time-logger-filter.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |