Skip to content

Commit

Permalink
Don't overwrite start time in InstrumentedHandler with concurrent asy…
Browse files Browse the repository at this point in the history
…nc requests (#2429)

Conflicts:
	metrics-jetty11/src/main/java/io/dropwizard/metrics5/jetty11/InstrumentedHandler.java

Closes #2121
Refs #2413

(cherry picked from commit 533cac7)

Co-authored-by: Jacob Schlather <[email protected]>
  • Loading branch information
joschi and jschlather authored Feb 3, 2022
1 parent b6adbdb commit 2e9b0f0
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,36 +210,7 @@ public Ratio getRatio() {
});


this.listener = new AsyncListener() {
private long startTime;

@Override
public void onTimeout(AsyncEvent event) throws IOException {
asyncTimeouts.mark();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
startTime = System.currentTimeMillis();
event.getAsyncContext().addListener(this);
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
final HttpServletRequest request = (HttpServletRequest) state.getRequest();
final HttpServletResponse response = (HttpServletResponse) state.getResponse();
updateResponses(request, response, startTime, true);
if (state.getHttpChannelState().getState() != HttpChannelState.State.HANDLING &&
state.getHttpChannelState().isSuspended()) {
activeSuspended.dec();
}
}
};
this.listener = new AsyncAttachingListener();
}

@Override
Expand Down Expand Up @@ -369,4 +340,53 @@ private void updateResponses(HttpServletRequest request, HttpServletResponse res
private MetricName getMetricPrefix() {
return this.prefix == null ? name(getHandler().getClass(), name) : name(this.prefix, name);
}

private class AsyncAttachingListener implements AsyncListener {

@Override
public void onTimeout(AsyncEvent event) throws IOException {}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
event.getAsyncContext().addListener(new InstrumentedAsyncListener());
}

@Override
public void onError(AsyncEvent event) throws IOException {}

@Override
public void onComplete(AsyncEvent event) throws IOException {}
};

private class InstrumentedAsyncListener implements AsyncListener {
private final long startTime;

InstrumentedAsyncListener() {
this.startTime = System.currentTimeMillis();
}

@Override
public void onTimeout(AsyncEvent event) throws IOException {
asyncTimeouts.mark();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
final HttpServletRequest request = (HttpServletRequest) state.getRequest();
final HttpServletResponse response = (HttpServletResponse) state.getResponse();
updateResponses(request, response, startTime, true);
if (!state.getHttpChannelState().isSuspended()) {
activeSuspended.dec();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,7 @@ public RatioGauge.Ratio getRatio() {
}
});

this.listener = new AsyncListener() {
private long startTime;

@Override
public void onTimeout(AsyncEvent event) throws IOException {
asyncTimeouts.mark();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
startTime = System.currentTimeMillis();
event.getAsyncContext().addListener(this);
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
final HttpServletRequest request = (HttpServletRequest) state.getRequest();
final HttpServletResponse response = (HttpServletResponse) state.getResponse();
updateResponses(request, response, startTime, true);
if (!state.getHttpChannelState().isSuspended()) {
activeSuspended.dec();
}
}
};
this.listener = new AsyncAttachingListener();
}

@Override
Expand Down Expand Up @@ -355,4 +327,53 @@ private Timer requestTimer(String method) {
}
}
}

private class AsyncAttachingListener implements AsyncListener {

@Override
public void onTimeout(AsyncEvent event) throws IOException {}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
event.getAsyncContext().addListener(new InstrumentedAsyncListener());
}

@Override
public void onError(AsyncEvent event) throws IOException {}

@Override
public void onComplete(AsyncEvent event) throws IOException {}
};

private class InstrumentedAsyncListener implements AsyncListener {
private final long startTime;

InstrumentedAsyncListener() {
this.startTime = System.currentTimeMillis();
}

@Override
public void onTimeout(AsyncEvent event) throws IOException {
asyncTimeouts.mark();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
final HttpServletRequest request = (HttpServletRequest) state.getRequest();
final HttpServletResponse response = (HttpServletResponse) state.getResponse();
updateResponses(request, response, startTime, true);
if (!state.getHttpChannelState().isSuspended()) {
activeSuspended.dec();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,38 +209,7 @@ public Ratio getRatio() {
}
});



this.listener = new AsyncListener() {
private long startTime;

@Override
public void onTimeout(AsyncEvent event) throws IOException {
asyncTimeouts.mark();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
startTime = System.currentTimeMillis();
event.getAsyncContext().addListener(this);
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
final HttpServletRequest request = (HttpServletRequest) state.getRequest();
final HttpServletResponse response = (HttpServletResponse) state.getResponse();
updateResponses(request, response, startTime, true);
if (state.getHttpChannelState().getState() != HttpChannelState.State.HANDLING &&
state.getHttpChannelState().isSuspended()) {
activeSuspended.dec();
}
}
};
this.listener = new AsyncAttachingListener();
}

@Override
Expand Down Expand Up @@ -370,4 +339,53 @@ private void updateResponses(HttpServletRequest request, HttpServletResponse res
private MetricName getMetricPrefix() {
return this.prefix == null ? name(getHandler().getClass(), name) : name(this.prefix, name);
}

private class AsyncAttachingListener implements AsyncListener {

@Override
public void onTimeout(AsyncEvent event) throws IOException {}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
event.getAsyncContext().addListener(new InstrumentedAsyncListener());
}

@Override
public void onError(AsyncEvent event) throws IOException {}

@Override
public void onComplete(AsyncEvent event) throws IOException {}
};

private class InstrumentedAsyncListener implements AsyncListener {
private final long startTime;

InstrumentedAsyncListener() {
this.startTime = System.currentTimeMillis();
}

@Override
public void onTimeout(AsyncEvent event) throws IOException {
asyncTimeouts.mark();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
final HttpServletRequest request = (HttpServletRequest) state.getRequest();
final HttpServletResponse response = (HttpServletResponse) state.getResponse();
updateResponses(request, response, startTime, true);
if (!state.getHttpChannelState().isSuspended()) {
activeSuspended.dec();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,7 @@ public RatioGauge.Ratio getRatio() {
}
});

this.listener = new AsyncListener() {
private long startTime;

@Override
public void onTimeout(AsyncEvent event) throws IOException {
asyncTimeouts.mark();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
startTime = System.currentTimeMillis();
event.getAsyncContext().addListener(this);
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
final HttpServletRequest request = (HttpServletRequest) state.getRequest();
final HttpServletResponse response = (HttpServletResponse) state.getResponse();
updateResponses(request, response, startTime, true);
if (!state.getHttpChannelState().isSuspended()) {
activeSuspended.dec();
}
}
};
this.listener = new AsyncAttachingListener();
}

@Override
Expand Down Expand Up @@ -355,4 +327,53 @@ private Timer requestTimer(String method) {
}
}
}

private class AsyncAttachingListener implements AsyncListener {

@Override
public void onTimeout(AsyncEvent event) throws IOException {}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
event.getAsyncContext().addListener(new InstrumentedAsyncListener());
}

@Override
public void onError(AsyncEvent event) throws IOException {}

@Override
public void onComplete(AsyncEvent event) throws IOException {}
};

private class InstrumentedAsyncListener implements AsyncListener {
private final long startTime;

InstrumentedAsyncListener() {
this.startTime = System.currentTimeMillis();
}

@Override
public void onTimeout(AsyncEvent event) throws IOException {
asyncTimeouts.mark();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onComplete(AsyncEvent event) throws IOException {
final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
final HttpServletRequest request = (HttpServletRequest) state.getRequest();
final HttpServletResponse response = (HttpServletResponse) state.getResponse();
updateResponses(request, response, startTime, true);
if (!state.getHttpChannelState().isSuspended()) {
activeSuspended.dec();
}
}
}
}
Loading

0 comments on commit 2e9b0f0

Please sign in to comment.