opentelemetry-java-instrume.../instrumentation/servlet/servlet-3.0/src/test/groovy/AbstractServlet3Test.groovy

117 lines
4.4 KiB
Groovy

/*
* Copyright The OpenTelemetry Authors
*
* 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.
*/
import io.opentelemetry.auto.instrumentation.api.MoreAttributes
import io.opentelemetry.auto.test.asserts.TraceAssert
import io.opentelemetry.auto.test.base.HttpServerTest
import io.opentelemetry.trace.Span
import io.opentelemetry.trace.attributes.SemanticAttributes
import okhttp3.Request
import okhttp3.RequestBody
import javax.servlet.Servlet
import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.AUTH_REQUIRED
import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.ERROR
import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.SUCCESS
abstract class AbstractServlet3Test<SERVER, CONTEXT> extends HttpServerTest<SERVER> {
@Override
URI buildAddress() {
return new URI("http://localhost:$port/$context/")
}
// FIXME: Add authentication tests back in...
// @Shared
// protected String user = "user"
// @Shared
// protected String pass = "password"
abstract String getContext()
Class<Servlet> servlet = servlet()
abstract Class<Servlet> servlet()
abstract void addServlet(CONTEXT context, String path, Class<Servlet> servlet)
protected void setupServlets(CONTEXT context) {
def servlet = servlet()
addServlet(context, SUCCESS.path, servlet)
addServlet(context, QUERY_PARAM.path, servlet)
addServlet(context, ERROR.path, servlet)
addServlet(context, EXCEPTION.path, servlet)
addServlet(context, REDIRECT.path, servlet)
addServlet(context, AUTH_REQUIRED.path, servlet)
}
protected ServerEndpoint lastRequest
@Override
Request.Builder request(ServerEndpoint uri, String method, RequestBody body) {
lastRequest = uri
super.request(uri, method, body)
}
@Override
void serverSpan(TraceAssert trace, int index, String traceID = null, String parentID = null, String method = "GET", Long responseContentLength = null, ServerEndpoint endpoint = SUCCESS) {
trace.span(index) {
operationName entryPointName()
spanKind Span.Kind.SERVER // can't use static import because of SERVER type parameter
errored endpoint.errored
if (parentID != null) {
traceId traceID
parentId parentID
} else {
parent()
}
attributes {
"${SemanticAttributes.NET_PEER_IP.key()}" { it == null || it == "127.0.0.1" } // Optional
"${SemanticAttributes.NET_PEER_PORT.key()}" Long
"${SemanticAttributes.HTTP_URL.key()}" { it == "${endpoint.resolve(address)}" || it == "${endpoint.resolveWithoutFragment(address)}" }
"${SemanticAttributes.HTTP_METHOD.key()}" method
"${SemanticAttributes.HTTP_STATUS_CODE.key()}" endpoint.status
// exception bodies are not yet recorded
"${SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH.key()}" { "${responseContentLength ?: 0}" || endpoint == EXCEPTION }
// Optional
if (context) {
"servlet.context" "/$context"
}
"servlet.path" { it == endpoint.path || it == "/dispatch$endpoint.path" }
if (endpoint.errored) {
"error.msg" { it == null || it == EXCEPTION.body }
"error.type" { it == null || it == Exception.name }
"error.stack" { it == null || it instanceof String }
}
if (endpoint.query) {
"$MoreAttributes.HTTP_QUERY" endpoint.query
}
}
}
}
//Simple class name plus method name of the entry point of the given servlet container.
//"Entry point" here means the first filter or servlet that accepts incoming requests.
//This will serve as a default name of the SERVER span created for this request.
protected String entryPointName() {
'HttpServlet.service'
}
}