-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ludovic Orban <[email protected]>
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...erver/src/main/java/org/eclipse/jetty/server/handler/AbstractLatencyRecordingHandler.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,86 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// ======================================================================== | ||
// | ||
|
||
package org.eclipse.jetty.server.handler; | ||
|
||
import java.util.function.Function; | ||
|
||
import org.eclipse.jetty.server.Handler; | ||
import org.eclipse.jetty.server.HttpStream; | ||
import org.eclipse.jetty.server.Request; | ||
import org.eclipse.jetty.server.Response; | ||
import org.eclipse.jetty.util.Callback; | ||
import org.eclipse.jetty.util.NanoTime; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A <code>Handler</code> that allows recording the latencies of the requests executed by the wrapped handler. | ||
*/ | ||
public abstract class AbstractLatencyRecordingHandler extends Handler.Wrapper | ||
{ | ||
private static final Logger LOG = LoggerFactory.getLogger(AbstractLatencyRecordingHandler.class); | ||
|
||
private final Function<HttpStream, HttpStream> recordingWrapper; | ||
|
||
public AbstractLatencyRecordingHandler() | ||
{ | ||
this.recordingWrapper = httpStream -> new HttpStream.Wrapper(httpStream) | ||
{ | ||
@Override | ||
public void succeeded() | ||
{ | ||
long begin = httpStream.getNanoTime(); | ||
super.succeeded(); | ||
try | ||
{ | ||
onRequestComplete(NanoTime.since(begin)); | ||
} | ||
catch (Throwable t) | ||
{ | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Error thrown by onRequestComplete", t); | ||
} | ||
} | ||
|
||
@Override | ||
public void failed(Throwable x) | ||
{ | ||
long begin = httpStream.getNanoTime(); | ||
super.failed(x); | ||
try | ||
{ | ||
onRequestComplete(NanoTime.since(begin)); | ||
} | ||
catch (Throwable t) | ||
{ | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Error thrown by onRequestComplete", t); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean process(Request request, Response response, Callback callback) throws Exception | ||
{ | ||
request.addHttpStreamWrapper(recordingWrapper); | ||
return super.process(request, response, callback); | ||
} | ||
|
||
/** | ||
* Called back for each completed request with its execution's duration. | ||
* @param durationInNs the duration in nanoseconds of the completed request | ||
*/ | ||
protected abstract void onRequestComplete(long durationInNs); | ||
} |