-
Notifications
You must be signed in to change notification settings - Fork 886
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix prematurely ending server span on undertow (#2560)
* Fix permaturely ending server span on undertow * Trigger Build * Add undertow to supported frameworks * Review fixes * Update instrumentation/undertow/javaagent/src/test/groovy/UndertowServerTest.groovy Co-authored-by: Trask Stalnaker <[email protected]> * Update instrumentation/undertow/javaagent/src/test/groovy/UndertowServerTest.groovy Co-authored-by: Trask Stalnaker <[email protected]> * Review fixes Co-authored-by: Trask Stalnaker <[email protected]>
- Loading branch information
Showing
10 changed files
with
285 additions
and
87 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
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
File renamed without changes.
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
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
182 changes: 182 additions & 0 deletions
182
instrumentation/undertow-1.4/javaagent/src/test/groovy/UndertowServerTest.groovy
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,182 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.ERROR | ||
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.EXCEPTION | ||
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM | ||
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT | ||
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.SUCCESS | ||
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan | ||
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderTrace | ||
|
||
import io.opentelemetry.api.trace.Span | ||
import io.opentelemetry.api.trace.SpanKind | ||
import io.opentelemetry.instrumentation.test.AgentTestTrait | ||
import io.opentelemetry.instrumentation.test.base.HttpServerTest | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes | ||
import io.undertow.Handlers | ||
import io.undertow.Undertow | ||
import io.undertow.util.Headers | ||
import io.undertow.util.StatusCodes | ||
import okhttp3.HttpUrl | ||
import okhttp3.Response | ||
|
||
//TODO make test which mixes handlers and servlets | ||
class UndertowServerTest extends HttpServerTest<Undertow> implements AgentTestTrait { | ||
|
||
@Override | ||
Undertow startServer(int port) { | ||
Undertow server = Undertow.builder() | ||
.addHttpListener(port, "localhost") | ||
.setHandler(Handlers.path() | ||
.addExactPath(SUCCESS.rawPath()) { exchange -> | ||
controller(SUCCESS) { | ||
exchange.getResponseSender().send(SUCCESS.body) | ||
} | ||
} | ||
.addExactPath(QUERY_PARAM.rawPath()) { exchange -> | ||
controller(QUERY_PARAM) { | ||
exchange.getResponseSender().send(exchange.getQueryString()) | ||
} | ||
} | ||
.addExactPath(REDIRECT.rawPath()) { exchange -> | ||
controller(REDIRECT) { | ||
exchange.setStatusCode(StatusCodes.FOUND) | ||
exchange.getResponseHeaders().put(Headers.LOCATION, REDIRECT.body) | ||
exchange.endExchange() | ||
} | ||
} | ||
.addExactPath(ERROR.rawPath()) { exchange -> | ||
controller(ERROR) { | ||
exchange.setStatusCode(ERROR.status) | ||
exchange.getResponseSender().send(ERROR.body) | ||
} | ||
} | ||
.addExactPath(EXCEPTION.rawPath()) { exchange -> | ||
controller(EXCEPTION) { | ||
throw new Exception(EXCEPTION.body) | ||
} | ||
} | ||
.addExactPath("sendResponse") { exchange -> | ||
Span.current().addEvent("before-event") | ||
runUnderTrace("sendResponse") { | ||
exchange.setStatusCode(StatusCodes.OK) | ||
exchange.getResponseSender().send("sendResponse") | ||
} | ||
// event is added only when server span has not been ended | ||
// we need to make sure that sending response does not end server span | ||
Span.current().addEvent("after-event") | ||
} | ||
.addExactPath("sendResponseWithException") { exchange -> | ||
Span.current().addEvent("before-event") | ||
runUnderTrace("sendResponseWithException") { | ||
exchange.setStatusCode(StatusCodes.OK) | ||
exchange.getResponseSender().send("sendResponseWithException") | ||
} | ||
// event is added only when server span has not been ended | ||
// we need to make sure that sending response does not end server span | ||
Span.current().addEvent("after-event") | ||
throw new Exception("exception after sending response") | ||
} | ||
).build() | ||
server.start() | ||
return server | ||
} | ||
|
||
@Override | ||
void stopServer(Undertow undertow) { | ||
undertow.stop() | ||
} | ||
|
||
@Override | ||
String expectedServerSpanName(ServerEndpoint endpoint) { | ||
return "HTTP GET" | ||
} | ||
|
||
def "test send response"() { | ||
setup: | ||
def uri = address.resolve("sendResponse") | ||
def url = HttpUrl.get(uri).newBuilder().build() | ||
def request = request(url, "GET", null).build() | ||
Response response = client.newCall(request).execute() | ||
|
||
expect: | ||
response.code() == 200 | ||
response.body().string().trim() == "sendResponse" | ||
|
||
and: | ||
assertTraces(1) { | ||
trace(0, 2) { | ||
it.span(0) { | ||
hasNoParent() | ||
name "HTTP GET" | ||
kind SpanKind.SERVER | ||
|
||
event(0) { | ||
eventName "before-event" | ||
} | ||
event(1) { | ||
eventName "after-event" | ||
} | ||
|
||
attributes { | ||
"${SemanticAttributes.NET_PEER_PORT.key}" { it instanceof Long } | ||
"${SemanticAttributes.NET_PEER_IP.key}" "127.0.0.1" | ||
"${SemanticAttributes.HTTP_CLIENT_IP.key}" TEST_CLIENT_IP | ||
"${SemanticAttributes.HTTP_URL.key}" uri.toString() | ||
"${SemanticAttributes.HTTP_METHOD.key}" "GET" | ||
"${SemanticAttributes.HTTP_STATUS_CODE.key}" 200 | ||
"${SemanticAttributes.HTTP_FLAVOR.key}" "1.1" | ||
"${SemanticAttributes.HTTP_USER_AGENT.key}" TEST_USER_AGENT | ||
} | ||
} | ||
basicSpan(it, 1, "sendResponse", span(0)) | ||
} | ||
} | ||
} | ||
|
||
def "test send response with exception"() { | ||
setup: | ||
def uri = address.resolve("sendResponseWithException") | ||
def url = HttpUrl.get(uri).newBuilder().build() | ||
def request = request(url, "GET", null).build() | ||
Response response = client.newCall(request).execute() | ||
|
||
expect: | ||
response.code() == 200 | ||
response.body().string().trim() == "sendResponseWithException" | ||
|
||
and: | ||
assertTraces(1) { | ||
trace(0, 2) { | ||
it.span(0) { | ||
hasNoParent() | ||
name "HTTP GET" | ||
kind SpanKind.SERVER | ||
|
||
event(0) { | ||
eventName "before-event" | ||
} | ||
event(1) { | ||
eventName "after-event" | ||
} | ||
errorEvent(Exception, "exception after sending response", 2) | ||
|
||
attributes { | ||
"${SemanticAttributes.NET_PEER_PORT.key}" { it instanceof Long } | ||
"${SemanticAttributes.NET_PEER_IP.key}" "127.0.0.1" | ||
"${SemanticAttributes.HTTP_CLIENT_IP.key}" TEST_CLIENT_IP | ||
"${SemanticAttributes.HTTP_URL.key}" uri.toString() | ||
"${SemanticAttributes.HTTP_METHOD.key}" "GET" | ||
"${SemanticAttributes.HTTP_STATUS_CODE.key}" 200 | ||
"${SemanticAttributes.HTTP_FLAVOR.key}" "1.1" | ||
"${SemanticAttributes.HTTP_USER_AGENT.key}" TEST_USER_AGENT | ||
} | ||
} | ||
basicSpan(it, 1, "sendResponseWithException", span(0)) | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.