Skip to content

Commit

Permalink
Use instrumentation modules (#123)
Browse files Browse the repository at this point in the history
* Use instrumentation module

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* add more

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* fix muzzle

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* revert muzzle

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>
  • Loading branch information
pavolloffay authored Nov 18, 2020
1 parent 9397c41 commit 85ede52
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 239 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright The Hypertrace 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.instrumentation.hypertrace.grpc.v1_5;

import com.google.auto.service.AutoService;
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.client.GrpcClientBodyInstrumentation;
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.server.GrpcServerBodyInstrumentation;
import io.opentelemetry.javaagent.tooling.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.Arrays;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class GrpcBodyInstrumentationModule extends InstrumentationModule {

public GrpcBodyInstrumentationModule() {
super("grpc");
}

@Override
public int getOrder() {
return 1;
}

@Override
public String[] helperClassNames() {
return new String[] {
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator",
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.InstrumentationName",
packageName + ".client.GrpcClientInterceptor",
packageName + ".client.GrpcClientInterceptor$TracingClientCall",
packageName + ".client.GrpcClientInterceptor$TracingClientCallListener",
packageName + ".server.GrpcServerInterceptor",
packageName + ".server.GrpcServerInterceptor$TracingServerCall",
packageName + ".server.GrpcServerInterceptor$TracingServerCallListener",
packageName + ".server.NoopServerCallListener"
};
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return Arrays.asList(new GrpcClientBodyInstrumentation(), new GrpcServerBodyInstrumentation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,16 @@
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.google.auto.service.AutoService;
import io.grpc.ClientInterceptor;
import io.opentelemetry.javaagent.tooling.Instrumenter;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.List;
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;

@AutoService(Instrumenter.class)
public class GrpcClientBodyInstrumentation extends Instrumenter.Default {

public GrpcClientBodyInstrumentation() {
super("grpc");
}

@Override
public int getOrder() {
return 1;
}

@Override
public String[] helperClassNames() {
return new String[] {
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator",
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.InstrumentationName",
packageName + ".GrpcClientInterceptor",
packageName + ".GrpcClientInterceptor$TracingClientCall",
packageName + ".GrpcClientInterceptor$TracingClientCallListener"
};
}
public class GrpcClientBodyInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,16 @@
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.google.auto.service.AutoService;
import io.grpc.ServerInterceptor;
import io.opentelemetry.javaagent.tooling.Instrumenter;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.List;
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;

@AutoService(Instrumenter.class)
public class GrpcServerBodyInstrumentation extends Instrumenter.Default {

public GrpcServerBodyInstrumentation() {
super("grpc");
}

@Override
public int getOrder() {
return 1;
}

@Override
public String[] helperClassNames() {
return new String[] {
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator",
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.InstrumentationName",
packageName + ".GrpcServerInterceptor",
packageName + ".GrpcServerInterceptor$TracingServerCall",
packageName + ".GrpcServerInterceptor$TracingServerCallListener",
packageName + ".NoopServerCallListener"
};
}
public class GrpcServerBodyInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.tooling.Instrumenter;
import io.opentelemetry.javaagent.tooling.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
Expand All @@ -31,10 +34,10 @@
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;

@AutoService(Instrumenter.class)
public class OkHttp3BodyInstrumentation extends Instrumenter.Default {
@AutoService(InstrumentationModule.class)
public class OkHttp3BodyInstrumentationModule extends InstrumentationModule {

public OkHttp3BodyInstrumentation() {
public OkHttp3BodyInstrumentationModule() {
super(InstrumentationName.INSTRUMENTATION_NAME[0], InstrumentationName.INSTRUMENTATION_NAME[1]);
}

Expand All @@ -44,22 +47,29 @@ public int getOrder() {
}

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("okhttp3.OkHttpClient");
public String[] helperClassNames() {
return new String[] {
packageName + ".InstrumentationName", packageName + ".OkHttpTracingInterceptor",
};
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
isConstructor().and(takesArgument(0, named("okhttp3.OkHttpClient$Builder"))),
OkHttp3BodyInstrumentation.class.getName() + "$OkHttp3Advice");
public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new OkHttp3BodyInstrumentation());
}

@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".InstrumentationName", packageName + ".OkHttpTracingInterceptor",
};
private static final class OkHttp3BodyInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("okhttp3.OkHttpClient");
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
isConstructor().and(takesArgument(0, named("okhttp3.OkHttpClient$Builder"))),
OkHttp3BodyInstrumentationModule.class.getName() + "$OkHttp3Advice");
}
}

public static class OkHttp3Advice {
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/servlet/servlet-2.3/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ muzzle {
fail {
group = "javax.servlet"
module = "javax.servlet-api"
versions = "(,)"
versions = "(3.0.1,)"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.tooling.Instrumenter;
import io.opentelemetry.javaagent.tooling.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
Expand All @@ -52,10 +54,10 @@
* version 2.2, however this implementation uses request and response wrappers that were introduced
* in 2.3.
*/
@AutoService(Instrumenter.class)
public class Servlet2BodyInstrumentation extends Instrumenter.Default {
@AutoService(InstrumentationModule.class)
public class Servlet2BodyInstrumentationModule extends InstrumentationModule {

public Servlet2BodyInstrumentation() {
public Servlet2BodyInstrumentationModule() {
super(InstrumentationName.INSTRUMENTATION_NAME[0], InstrumentationName.INSTRUMENTATION_NAME[1]);
}

Expand All @@ -64,21 +66,6 @@ public int getOrder() {
return 1;
}

// this is required to make sure servlet 2 instrumentation won't apply to servlet 3
@Override
public ElementMatcher<ClassLoader> classLoaderMatcher() {
// Request/response wrappers are available since servlet 2.3!
return hasClassesNamed(
"javax.servlet.http.HttpServlet", "javax.servlet.http.HttpServletRequestWrapper")
.and(not(hasClassesNamed("javax.servlet.AsyncEvent", "javax.servlet.AsyncListener")));
}

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return safeHasSuperType(
namedOneOf("javax.servlet.FilterChain", "javax.servlet.http.HttpServlet"));
}

@Override
public Map<String, String> contextStore() {
return singletonMap("javax.servlet.ServletResponse", Integer.class.getName());
Expand All @@ -102,13 +89,36 @@ public String[] helperClassNames() {
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
namedOneOf("doFilter", "service")
.and(takesArgument(0, named("javax.servlet.ServletRequest")))
.and(takesArgument(1, named("javax.servlet.ServletResponse")))
.and(isPublic()),
Filter2Advice.class.getName());
public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new Servlet2BodyInstrumentation());
}

public static class Servlet2BodyInstrumentation implements TypeInstrumentation {

// this is required to make sure servlet 2 instrumentation won't apply to servlet 3
@Override
public ElementMatcher<ClassLoader> classLoaderMatcher() {
// Request/response wrappers are available since servlet 2.3!
return hasClassesNamed(
"javax.servlet.http.HttpServlet", "javax.servlet.http.HttpServletRequestWrapper")
.and(not(hasClassesNamed("javax.servlet.AsyncEvent", "javax.servlet.AsyncListener")));
}

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return safeHasSuperType(
namedOneOf("javax.servlet.FilterChain", "javax.servlet.http.HttpServlet"));
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
namedOneOf("doFilter", "service")
.and(takesArgument(0, named("javax.servlet.ServletRequest")))
.and(takesArgument(1, named("javax.servlet.ServletResponse")))
.and(isPublic()),
Filter2Advice.class.getName());
}
}

public static class Filter2Advice {
Expand Down
Loading

0 comments on commit 85ede52

Please sign in to comment.