-
Notifications
You must be signed in to change notification settings - Fork 1k
pekko: fix spans on server timeout #13435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
47cb628
pekko: fix spans on server timeout
samwright 4c8655c
spotless
samwright 1bc0337
fix formatting
samwright 56ffab8
fix formatting, enable http-pipelining test + fixes
samwright f2e4560
spotless
samwright 5768f33
rename
samwright e97a1df
undo http-pipelining changes (will move to another PR)
samwright ce53b3a
remove enableStrictContext=false
samwright File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
45 changes: 45 additions & 0 deletions
45
...y/javaagent/instrumentation/pekkohttp/v1_0/server/HttpServerBluePrintInstrumentation.java
This file contains hidden or 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,45 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.pekko.http.scaladsl.model.HttpRequest; | ||
import org.apache.pekko.http.scaladsl.model.HttpResponse; | ||
import org.apache.pekko.stream.scaladsl.BidiFlow; | ||
|
||
public class HttpServerBluePrintInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.apache.pekko.http.impl.engine.server.HttpServerBluePrint$"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("requestPreparation") | ||
.and(returns(named("org.apache.pekko.stream.scaladsl.BidiFlow"))), | ||
this.getClass().getName() + "$PekkoBindAndHandleAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PekkoBindAndHandleAdvice { | ||
|
||
@Advice.AssignReturned.ToReturned | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static BidiFlow<HttpResponse, ?, ?, HttpRequest, ?> wrapHandler( | ||
@Advice.Return BidiFlow<HttpResponse, ?, ?, HttpRequest, ?> handler) { | ||
|
||
return PekkoHttpServerTracer.wrap(handler); | ||
} | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
188 changes: 188 additions & 0 deletions
188
.../opentelemetry/javaagent/instrumentation/pekkohttp/v1_0/server/PekkoHttpServerTracer.java
This file contains hidden or 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,188 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server; | ||
|
||
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; | ||
import static io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.PekkoHttpServerSingletons.instrumenter; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; | ||
import io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route.PekkoRouteHolder; | ||
import java.util.ArrayDeque; | ||
import java.util.List; | ||
import java.util.Queue; | ||
import org.apache.pekko.http.javadsl.model.HttpHeader; | ||
import org.apache.pekko.http.scaladsl.model.HttpRequest; | ||
import org.apache.pekko.http.scaladsl.model.HttpResponse; | ||
import org.apache.pekko.stream.Attributes; | ||
import org.apache.pekko.stream.BidiShape; | ||
import org.apache.pekko.stream.Inlet; | ||
import org.apache.pekko.stream.Outlet; | ||
import org.apache.pekko.stream.scaladsl.BidiFlow; | ||
import org.apache.pekko.stream.stage.AbstractInHandler; | ||
import org.apache.pekko.stream.stage.AbstractOutHandler; | ||
import org.apache.pekko.stream.stage.GraphStage; | ||
import org.apache.pekko.stream.stage.GraphStageLogic; | ||
|
||
public class PekkoHttpServerTracer | ||
extends GraphStage<BidiShape<HttpResponse, HttpResponse, HttpRequest, HttpRequest>> { | ||
laurit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final Inlet<HttpRequest> requestIn = Inlet.create("otel.requestIn"); | ||
private final Outlet<HttpRequest> requestOut = Outlet.create("otel.requestOut"); | ||
private final Inlet<HttpResponse> responseIn = Inlet.create("otel.responseIn"); | ||
private final Outlet<HttpResponse> responseOut = Outlet.create("otel.responseOut"); | ||
|
||
private final BidiShape<HttpResponse, HttpResponse, HttpRequest, HttpRequest> shape = | ||
BidiShape.of(responseIn, responseOut, requestIn, requestOut); | ||
|
||
public static BidiFlow<HttpResponse, ?, ?, HttpRequest, ?> wrap( | ||
BidiFlow<HttpResponse, ?, ?, HttpRequest, ?> handler) { | ||
return BidiFlow.fromGraph(new PekkoHttpServerTracer()).atopMat(handler, (a, b) -> b); | ||
} | ||
|
||
@Override | ||
public BidiShape<HttpResponse, HttpResponse, HttpRequest, HttpRequest> shape() { | ||
return shape; | ||
} | ||
|
||
@Override | ||
public GraphStageLogic createLogic(Attributes attributes) { | ||
return new TracingLogic(); | ||
} | ||
|
||
private class TracingLogic extends GraphStageLogic { | ||
private final Queue<PekkoTracingRequest> requests = new ArrayDeque<>(); | ||
|
||
public TracingLogic() { | ||
super(shape); | ||
|
||
// server pulls response, pass response from user code to server | ||
setHandler( | ||
responseOut, | ||
new AbstractOutHandler() { | ||
@Override | ||
public void onPull() { | ||
pull(responseIn); | ||
} | ||
|
||
@Override | ||
public void onDownstreamFinish(Throwable cause) { | ||
cancel(responseIn); | ||
} | ||
}); | ||
|
||
// user code pulls request, pass request from server to user code | ||
setHandler( | ||
requestOut, | ||
new AbstractOutHandler() { | ||
@Override | ||
public void onPull() { | ||
pull(requestIn); | ||
} | ||
|
||
@Override | ||
public void onDownstreamFinish(Throwable cause) { | ||
// Invoked on errors. Don't complete this stage to allow error-capturing | ||
cancel(requestIn); | ||
} | ||
}); | ||
|
||
// new request from server | ||
setHandler( | ||
requestIn, | ||
new AbstractInHandler() { | ||
@Override | ||
public void onPush() { | ||
HttpRequest request = grab(requestIn); | ||
PekkoTracingRequest tracingRequest = PekkoTracingRequest.EMPTY; | ||
Context parentContext = currentContext(); | ||
if (instrumenter().shouldStart(parentContext, request)) { | ||
Context context = instrumenter().start(parentContext, request); | ||
context = PekkoRouteHolder.init(context); | ||
tracingRequest = new PekkoTracingRequest(context, request); | ||
request = | ||
(HttpRequest) | ||
request.addAttribute(PekkoTracingRequest.ATTR_KEY, tracingRequest); | ||
} | ||
// event if span wasn't started we need to push TracingRequest to match response | ||
// with request | ||
requests.add(tracingRequest); | ||
|
||
push(requestOut, request); | ||
} | ||
|
||
@Override | ||
public void onUpstreamFinish() { | ||
complete(requestOut); | ||
} | ||
|
||
@Override | ||
public void onUpstreamFailure(Throwable exception) { | ||
fail(requestOut, exception); | ||
} | ||
}); | ||
|
||
// response from user code | ||
setHandler( | ||
responseIn, | ||
new AbstractInHandler() { | ||
@Override | ||
public void onPush() { | ||
HttpResponse response = grab(responseIn); | ||
|
||
PekkoTracingRequest tracingRequest = requests.poll(); | ||
if (tracingRequest != null && tracingRequest != PekkoTracingRequest.EMPTY) { | ||
// pekko response is immutable so the customizer just captures the added headers | ||
PekkoHttpResponseMutator responseMutator = new PekkoHttpResponseMutator(); | ||
HttpServerResponseCustomizerHolder.getCustomizer() | ||
.customize(tracingRequest.context, response, responseMutator); | ||
// build a new response with the added headers | ||
List<HttpHeader> headers = responseMutator.getHeaders(); | ||
if (!headers.isEmpty()) { | ||
response = (HttpResponse) response.addHeaders(headers); | ||
} | ||
|
||
instrumenter().end(tracingRequest.context, tracingRequest.request, response, null); | ||
} | ||
push(responseOut, response); | ||
} | ||
|
||
@Override | ||
public void onUpstreamFailure(Throwable exception) { | ||
// End the span for the request that failed | ||
PekkoTracingRequest tracingRequest = requests.poll(); | ||
if (tracingRequest != null && tracingRequest != PekkoTracingRequest.EMPTY) { | ||
instrumenter() | ||
.end( | ||
tracingRequest.context, | ||
tracingRequest.request, | ||
PekkoHttpServerSingletons.errorResponse(), | ||
exception); | ||
} | ||
|
||
fail(responseOut, exception); | ||
} | ||
|
||
@Override | ||
public void onUpstreamFinish() { | ||
// End any ongoing spans, though there should be none. | ||
PekkoTracingRequest tracingRequest; | ||
while ((tracingRequest = requests.poll()) != null) { | ||
if (tracingRequest == PekkoTracingRequest.EMPTY) { | ||
continue; | ||
} | ||
instrumenter() | ||
.end( | ||
tracingRequest.context, | ||
tracingRequest.request, | ||
PekkoHttpServerSingletons.errorResponse(), | ||
null); | ||
} | ||
completeStage(); | ||
} | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both here and in PekkoHttpServerTracer I'm using a queue (LIFO) because that's what the approach taken in Pekko by the request-timeout stage here.
The previous approach used the Deque as FIFO. Changing it here for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the previous usage was a bug since pipelined requests should complete in the oder they are received. Currently with our tests it seems that pekko does not have multiple requests in this queue, probably some extra configuration is needed. If we could figure out how to actually enable pipelining we could consider improving the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've enabled the http-pipelining test for the async tests, and managed to get it to work (see the comment on how). Nevertheless switching both deques from LIFO to FIFO has no effect - the tests pass in both cases.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking at this. If it doesn't break then I'd guess that perhaps there isn't more than 1 request in the queue at the same time. Maybe the requests are processed too fast, idk. I think if there is interest we can tackle this in a follow up PR.
Looking at https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/13435/files#diff-dc7412c25079c85f7462b02768d4445cda72f0934517132253e0b9140f7d69dcR147-R153 this really isn't a good idea. The scope from
makeCurrent
must be closed. Leaving the scope open is almost never the best solution as not closing a scope will prevent closing all other scopes that were open for that thread, it could lead to a memory leak or the thread ending in a state where all subsequent spans are added to one trace. If this is just for pipelining I'd suggest you to revert this part for now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I can revert it. This PR didn't break it so yeah a follow up should look into it.
A thought - how bad would it be to use the
Context.root()
as the parent context when creating the server span? i.e. change this to:The tests pass if I do that and remove the
tracingRequest.parentContext.makeCurrent()
bit, since the previous request's context is ignored when handling the next request.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually it is better to reset the scope where the unwanted context propagation happens. We usually fail the tests when there are leaked scopes but this is disabled in
opentelemetry-java-instrumentation/instrumentation/pekko/pekko-http-1.0/javaagent/build.gradle.kts
Line 50 in dfe392e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I've removed my dodgy workarounds - we can consider better solutions for http-pipelining in another issue.
I've also removed that system property and the tests still pass 👍