Simplify grizzly instrumentation, Part 1 (#604)

This commit is contained in:
Trask Stalnaker 2020-06-30 17:44:11 -07:00 committed by GitHub
parent c46623034a
commit e2a0504bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 44 additions and 278 deletions

View File

@ -116,7 +116,8 @@ provide the path to a JAR file including an SPI implementation using the system
| [Finatra](https://github.com/twitter/finatra) | 2.9+ |
| [Geode Client](https://geode.apache.org/) | 1.4+ |
| [Google HTTP Client](https://github.com/googleapis/google-http-java-client) | 1.19+ |
| [Grizzly](https://javaee.github.io/grizzly/httpserverframework.html) | 2.0+ (disabled by default, see below) |
| [Grizzly](https://javaee.github.io/grizzly/httpserverframework.html) | 2.0+ (disabled by default, see below) |
| [Grizzly Client](https://github.com/javaee/grizzly-ahc) | 1.9+ |
| [gRPC](https://github.com/grpc/grpc-java) | 1.5+ |
| [Hibernate](https://github.com/hibernate/hibernate-orm) | 3.3+ |
| [HttpURLConnection](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/HttpURLConnection.html) | Java 7+ |

View File

@ -21,6 +21,8 @@ testSets {
dependencies {
compileOnly group: 'org.glassfish.grizzly', name: 'grizzly-http-server', version: '2.0'
compile project(':instrumentation:grizzly-http-2.0')
testCompile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.3'
testCompile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0'
testCompile group: 'org.glassfish.grizzly', name: 'grizzly-http-server', version: '2.0'

View File

@ -1,124 +0,0 @@
/*
* 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.
*/
package io.opentelemetry.auto.instrumentation.grizzly;
import static io.opentelemetry.auto.bootstrap.instrumentation.decorator.HttpServerTracer.CONTEXT_ATTRIBUTE;
import static io.opentelemetry.auto.instrumentation.grizzly.GrizzlyHttpServerTracer.TRACER;
import static io.opentelemetry.trace.TracingContextUtils.getSpan;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import io.grpc.Context;
import io.opentelemetry.auto.tooling.Instrumenter;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span;
import java.lang.reflect.Method;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.glassfish.grizzly.http.server.AfterServiceListener;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
@AutoService(Instrumenter.class)
public class GrizzlyHttpHandlerInstrumentation extends Instrumenter.Default {
public GrizzlyHttpHandlerInstrumentation() {
super("grizzly");
}
@Override
protected boolean defaultEnabled() {
return false;
}
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("org.glassfish.grizzly.http.server.HttpHandler");
}
@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".GrizzlyHttpServerTracer",
packageName + ".GrizzlyRequestExtractAdapter",
getClass().getName() + "$SpanClosingListener"
};
}
@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
isMethod()
.and(named("doHandle"))
.and(takesArgument(0, named("org.glassfish.grizzly.http.server.Request")))
.and(takesArgument(1, named("org.glassfish.grizzly.http.server.Response"))),
GrizzlyHttpHandlerInstrumentation.class.getName() + "$HandleAdvice");
}
public static class HandleAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void methodEnter(
@Advice.Origin final Method method,
@Advice.Argument(0) final Request request,
@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
if (TRACER.getAttachedContext(request) != null) {
return;
}
request.addAfterServiceListener(SpanClosingListener.LISTENER);
span = TRACER.startSpan(request, method, null);
scope = TRACER.startScope(span, request);
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Argument(1) final Response response,
@Advice.Thrown final Throwable throwable,
@Advice.Local("otelSpan") Span span,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
}
scope.close();
if (throwable != null) {
TRACER.endExceptionally(span, throwable, response.getStatus());
}
}
}
public static class SpanClosingListener implements AfterServiceListener {
public static final SpanClosingListener LISTENER = new SpanClosingListener();
@Override
public void onAfterService(final Request request) {
final Object contextAttribute = request.getAttribute(CONTEXT_ATTRIBUTE);
if (contextAttribute instanceof Context) {
request.removeAttribute(CONTEXT_ATTRIBUTE);
TRACER.end(getSpan((Context) contextAttribute), request.getResponse().getStatus());
}
}
}
}

View File

@ -1,91 +0,0 @@
/*
* 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.
*/
package io.opentelemetry.auto.instrumentation.grizzly;
import static io.opentelemetry.auto.instrumentation.grizzly.GrizzlyRequestExtractAdapter.GETTER;
import io.grpc.Context;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.HttpServerTracer;
import io.opentelemetry.context.propagation.HttpTextFormat.Getter;
import io.opentelemetry.trace.Span;
import java.net.URI;
import java.net.URISyntaxException;
import org.glassfish.grizzly.http.server.Request;
public class GrizzlyHttpServerTracer extends HttpServerTracer<Request> {
public static final GrizzlyHttpServerTracer TRACER = new GrizzlyHttpServerTracer();
@Override
protected String getVersion() {
return null;
}
@Override
protected String getInstrumentationName() {
return "io.opentelemetry.auto.grizzly-2.0";
}
@Override
protected Integer peerPort(Request request) {
return request.getRemotePort();
}
@Override
protected String peerHostIP(Request request) {
return request.getRemoteAddr();
}
@Override
protected void attachContextToRequest(Context context, Request request) {
request.setAttribute(CONTEXT_ATTRIBUTE, context);
}
@Override
public Context getAttachedContext(Request request) {
Object context = request.getAttribute(CONTEXT_ATTRIBUTE);
return context instanceof Context ? (Context) context : null;
}
@Override
protected URI url(Request request) throws URISyntaxException {
return new URI(
request.getScheme(),
null,
request.getServerName(),
request.getServerPort(),
request.getRequestURI(),
request.getQueryString(),
null);
}
@Override
protected String method(Request request) {
return request.getMethod().getMethodString();
}
@Override
protected Getter<Request> getGetter() {
return GETTER;
}
@Override
public void onRequest(Span span, Request request) {
request.setAttribute("traceId", span.getContext().getTraceId().toLowerBase16());
request.setAttribute("spanId", span.getContext().getSpanId().toLowerBase16());
super.onRequest(span, request);
}
}

View File

@ -1,30 +0,0 @@
/*
* 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.
*/
package io.opentelemetry.auto.instrumentation.grizzly;
import io.opentelemetry.context.propagation.HttpTextFormat;
import org.glassfish.grizzly.http.server.Request;
public class GrizzlyRequestExtractAdapter implements HttpTextFormat.Getter<Request> {
public static final GrizzlyRequestExtractAdapter GETTER = new GrizzlyRequestExtractAdapter();
@Override
public String get(final Request carrier, final String key) {
return carrier.getHeader(key);
}
}

View File

@ -15,15 +15,16 @@
*/
import io.opentelemetry.auto.test.base.HttpServerTest
import org.glassfish.grizzly.http.server.HttpServer
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory
import org.glassfish.jersey.server.ResourceConfig
import javax.ws.rs.GET
import javax.ws.rs.NotFoundException
import javax.ws.rs.Path
import javax.ws.rs.QueryParam
import javax.ws.rs.core.Response
import javax.ws.rs.ext.ExceptionMapper
import org.glassfish.grizzly.http.server.HttpServer
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory
import org.glassfish.jersey.server.ResourceConfig
import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.ERROR
import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
@ -105,9 +106,4 @@ class GrizzlyTest extends HttpServerTest<HttpServer> {
return null
}
}
@Override
String expectedOperationName(String method, ServerEndpoint serverEndpoint) {
return 'HttpHandler.doHandle'
}
}

View File

@ -5,7 +5,8 @@ muzzle {
pass {
group = "org.glassfish.grizzly"
module = 'grizzly-http'
versions = "[2.3,)"
versions = "[2.0,)"
assertInverse = true
}
}
@ -17,7 +18,7 @@ testSets {
dependencies {
compileOnly group: 'org.glassfish.grizzly', name: 'grizzly-http', version: '2.3'
compileOnly group: 'org.glassfish.grizzly', name: 'grizzly-http', version: '2.0'
testCompile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.3'
testCompile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0'

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import net.bytebuddy.asm.Advice;
import org.glassfish.grizzly.filterchain.FilterChainContext;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
@ -33,7 +33,7 @@ import net.bytebuddy.matcher.ElementMatcher;
public class DefaultFilterChainInstrumentation extends Instrumenter.Default {
public DefaultFilterChainInstrumentation() {
super("grizzly-filterchain");
super("grizzly");
}
@Override

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import io.opentelemetry.context.propagation.HttpTextFormat;
import org.glassfish.grizzly.http.HttpHeader;

View File

@ -14,9 +14,9 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import static io.opentelemetry.auto.instrumentation.grizzly.http.v2_3.GrizzlyDecorator.TRACER;
import static io.opentelemetry.auto.instrumentation.grizzly.GrizzlyDecorator.TRACER;
import static io.opentelemetry.context.ContextUtils.withScopedContext;
import io.grpc.Context;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import static io.opentelemetry.auto.tooling.ClassLoaderMatcher.hasClassesNamed;
import static java.util.Collections.singletonMap;
@ -36,7 +36,7 @@ import net.bytebuddy.matcher.ElementMatchers;
public final class FilterInstrumentation extends Instrumenter.Default {
public FilterInstrumentation() {
super("grizzly-filterchain");
super("grizzly");
}
@Override
@ -63,6 +63,11 @@ public final class FilterInstrumentation extends Instrumenter.Default {
return new String[] {packageName + ".GrizzlyDecorator", packageName + ".ExtractAdapter"};
}
@Override
protected boolean defaultEnabled() {
return false;
}
@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import static io.opentelemetry.auto.bootstrap.instrumentation.decorator.HttpServerTracer.CONTEXT_ATTRIBUTE;
import static io.opentelemetry.trace.Span.Kind.SERVER;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import net.bytebuddy.asm.Advice;
import org.glassfish.grizzly.filterchain.FilterChainContext;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
@ -32,7 +32,7 @@ import net.bytebuddy.matcher.ElementMatcher;
public final class HttpCodecFilterInstrumentation extends Instrumenter.Default {
public HttpCodecFilterInstrumentation() {
super("grizzly-filterchain");
super("grizzly");
}
@Override

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import net.bytebuddy.asm.Advice;
import org.glassfish.grizzly.filterchain.FilterChainContext;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import net.bytebuddy.asm.Advice;
import org.glassfish.grizzly.filterchain.FilterChainContext;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.grizzly.http.v2_3;
package io.opentelemetry.auto.instrumentation.grizzly;
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
import static net.bytebuddy.matcher.ElementMatchers.named;
@ -32,7 +32,7 @@ import net.bytebuddy.matcher.ElementMatcher;
public class HttpServerFilterInstrumentation extends Instrumenter.Default {
public HttpServerFilterInstrumentation() {
super("grizzly-filterchain");
super("grizzly");
}
@Override

View File

@ -51,7 +51,7 @@ import static org.glassfish.grizzly.memory.Buffers.wrap
class GrizzlyFilterchainServerTest extends HttpServerTest<HttpServer> {
static {
System.setProperty("ota.integration.grizzly-filterchain.enabled", "true")
System.setProperty("ota.integration.grizzly.enabled", "true")
}
private TCPNIOTransport transport

View File

@ -15,6 +15,7 @@
*/
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import io.opentelemetry.auto.test.base.HttpServerTestAdvice;
@ -22,14 +23,19 @@ import io.opentelemetry.auto.tooling.Instrumenter;
import net.bytebuddy.agent.builder.AgentBuilder;
@AutoService(Instrumenter.class)
public class GrizzlyTestInstrumentation implements Instrumenter {
public class GrizzlyFilterchainServerTestInstrumentation implements Instrumenter {
@Override
public AgentBuilder instrument(final AgentBuilder agentBuilder) {
return agentBuilder
.type(named("org.glassfish.grizzly.http.server.HttpHandlerChain"))
.type(named("org.glassfish.grizzly.http.HttpCodecFilter"))
.transform(
new AgentBuilder.Transformer.ForAdvice()
.advice(named("doHandle"), HttpServerTestAdvice.ServerEntryAdvice.class.getName()));
.advice(
named("handleRead")
.and(
takesArgument(
0, named("org.glassfish.grizzly.filterchain.FilterChainContext"))),
HttpServerTestAdvice.ServerEntryAdvice.class.getName()));
}
}

View File

@ -35,7 +35,7 @@ import static io.opentelemetry.auto.test.base.HttpServerTest.ServerEndpoint.SUCC
class GrizzlyTest extends HttpServerTest<HttpServer> {
static {
System.setProperty("ota.integration.grizzly-filterchain.enabled", "true")
System.setProperty("ota.integration.grizzly.enabled", "true")
}
@Override

View File

@ -68,7 +68,7 @@ include ':instrumentation:geode-1.4'
include ':instrumentation:google-http-client-1.19'
include ':instrumentation:grizzly-2.0'
include ':instrumentation:grizzly-client-1.9'
include ':instrumentation:grizzly-http-2.3'
include ':instrumentation:grizzly-http-2.0'
include ':instrumentation:grpc-1.5'
include ':instrumentation:hibernate:hibernate-3.3'
include ':instrumentation:hibernate:hibernate-4.0'