-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Spring RMI instrumentation * Change the order of import statements * remove extra separation in import statements * stylistic changes * Fix groovy rule violations * Formatting changes in groovy file * Spotless fixes and muzzle check version change * Fixed minimum version in filenames and fixed muzzle check * single InstrumentationModule and added context propagation test * Merged singletons, use random port in test and add stricter matchers. * Remove unused import
- Loading branch information
1 parent
a806937
commit 3b0c490
Showing
13 changed files
with
547 additions
and
2 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
23 changes: 23 additions & 0 deletions
23
instrumentation/spring/spring-rmi-4.0/javaagent/build.gradle.kts
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,23 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("org.springframework") | ||
module.set("spring-context") | ||
versions.set("[4.0.0.RELEASE,)") | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly("com.google.auto.value:auto-value-annotations") | ||
annotationProcessor("com.google.auto.value:auto-value") | ||
|
||
bootstrap(project(":instrumentation:rmi:bootstrap")) | ||
testInstrumentation(project(":instrumentation:rmi:javaagent")) | ||
|
||
library("org.springframework:spring-context:4.0.0.RELEASE") | ||
library("org.springframework:spring-aop:4.0.0.RELEASE") | ||
testLibrary("org.springframework.boot:spring-boot:1.1.0.RELEASE") | ||
} |
28 changes: 28 additions & 0 deletions
28
.../io/opentelemetry/javaagent/instrumentation/springrmi/SpringRmiInstrumentationModule.java
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,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.springrmi; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.instrumentation.springrmi.client.ClientInstrumentation; | ||
import io.opentelemetry.javaagent.instrumentation.springrmi.server.ServerInstrumentation; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class SpringRmiInstrumentationModule extends InstrumentationModule { | ||
|
||
public SpringRmiInstrumentationModule() { | ||
super("spring-rmi", "spring-rmi-4.0"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return asList(new ClientInstrumentation(), new ServerInstrumentation()); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...c/main/java/io/opentelemetry/javaagent/instrumentation/springrmi/SpringRmiSingletons.java
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,55 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.springrmi; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcSpanNameExtractor; | ||
import io.opentelemetry.instrumentation.api.util.ClassAndMethod; | ||
import io.opentelemetry.javaagent.instrumentation.springrmi.client.ClientAttributesExtractor; | ||
import io.opentelemetry.javaagent.instrumentation.springrmi.server.ServerAttributesExtractor; | ||
import java.lang.reflect.Method; | ||
|
||
public final class SpringRmiSingletons { | ||
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-rmi-4.0"; | ||
|
||
private static final Instrumenter<Method, Void> CLIENT_INSTRUMENTER = buildClientInstrumenter(); | ||
private static final Instrumenter<ClassAndMethod, Void> SERVER_INSTRUMENTER = | ||
buildServerInstrumenter(); | ||
|
||
private static Instrumenter<Method, Void> buildClientInstrumenter() { | ||
ClientAttributesExtractor attributesExtractor = new ClientAttributesExtractor(); | ||
|
||
return Instrumenter.<Method, Void>builder( | ||
GlobalOpenTelemetry.get(), | ||
INSTRUMENTATION_NAME, | ||
RpcSpanNameExtractor.create(attributesExtractor)) | ||
.addAttributesExtractor(attributesExtractor) | ||
.newInstrumenter(SpanKindExtractor.alwaysClient()); | ||
} | ||
|
||
private static Instrumenter<ClassAndMethod, Void> buildServerInstrumenter() { | ||
ServerAttributesExtractor attributesExtractor = new ServerAttributesExtractor(); | ||
|
||
return Instrumenter.<ClassAndMethod, Void>builder( | ||
GlobalOpenTelemetry.get(), | ||
INSTRUMENTATION_NAME, | ||
RpcSpanNameExtractor.create(attributesExtractor)) | ||
.addAttributesExtractor(attributesExtractor) | ||
.newInstrumenter(SpanKindExtractor.alwaysServer()); | ||
} | ||
|
||
public static Instrumenter<Method, Void> clientInstrumenter() { | ||
return CLIENT_INSTRUMENTER; | ||
} | ||
|
||
public static Instrumenter<ClassAndMethod, Void> serverInstrumenter() { | ||
return SERVER_INSTRUMENTER; | ||
} | ||
|
||
private SpringRmiSingletons() {} | ||
} |
27 changes: 27 additions & 0 deletions
27
...o/opentelemetry/javaagent/instrumentation/springrmi/client/ClientAttributesExtractor.java
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,27 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.springrmi.client; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesExtractor; | ||
import java.lang.reflect.Method; | ||
|
||
public final class ClientAttributesExtractor extends RpcAttributesExtractor<Method, Void> { | ||
|
||
@Override | ||
protected String system(Method method) { | ||
return "spring_rmi"; | ||
} | ||
|
||
@Override | ||
protected String service(Method method) { | ||
return method.getDeclaringClass().getName(); | ||
} | ||
|
||
@Override | ||
protected String method(Method method) { | ||
return method.getName(); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...va/io/opentelemetry/javaagent/instrumentation/springrmi/client/ClientInstrumentation.java
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,80 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.springrmi.client; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass; | ||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static io.opentelemetry.javaagent.instrumentation.springrmi.SpringRmiSingletons.clientInstrumenter; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; | ||
import java.lang.reflect.Method; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
public class ClientInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderOptimization() { | ||
return hasClassesNamed("org.springframework.remoting.rmi.RmiClientInterceptor"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return extendsClass(named("org.springframework.remoting.rmi.RmiClientInterceptor")); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(named("invoke")) | ||
.and(takesArgument(0, named("org.aopalliance.intercept.MethodInvocation"))), | ||
this.getClass().getName() + "$InvokeMethodAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class InvokeMethodAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(0) MethodInvocation methodInv, | ||
@Advice.Local("method") Method method, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
method = methodInv.getMethod(); | ||
Context parentContext = Java8BytecodeBridge.currentContext(); | ||
if (!clientInstrumenter().shouldStart(parentContext, method)) { | ||
return; | ||
} | ||
|
||
context = clientInstrumenter().start(parentContext, method); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void stopSpan( | ||
@Advice.Local("method") Method method, | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
if (scope == null) { | ||
return; | ||
} | ||
scope.close(); | ||
clientInstrumenter().end(context, method, null, throwable); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...o/opentelemetry/javaagent/instrumentation/springrmi/server/ServerAttributesExtractor.java
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,27 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.springrmi.server; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.util.ClassAndMethod; | ||
|
||
public final class ServerAttributesExtractor extends RpcAttributesExtractor<ClassAndMethod, Void> { | ||
|
||
@Override | ||
protected String system(ClassAndMethod classAndMethod) { | ||
return "spring_rmi"; | ||
} | ||
|
||
@Override | ||
protected String service(ClassAndMethod classAndMethod) { | ||
return classAndMethod.declaringClass().getName(); | ||
} | ||
|
||
@Override | ||
protected String method(ClassAndMethod classAndMethod) { | ||
return classAndMethod.methodName(); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...va/io/opentelemetry/javaagent/instrumentation/springrmi/server/ServerInstrumentation.java
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,89 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.springrmi.server; | ||
|
||
import static io.opentelemetry.javaagent.bootstrap.rmi.ThreadLocalContext.THREAD_LOCAL_CONTEXT; | ||
import static io.opentelemetry.javaagent.instrumentation.springrmi.SpringRmiSingletons.serverInstrumenter; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.util.ClassAndMethod; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.opentelemetry.javaagent.instrumentation.api.CallDepth; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.springframework.remoting.rmi.RmiBasedExporter; | ||
import org.springframework.remoting.support.RemoteInvocation; | ||
|
||
public class ServerInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.springframework.remoting.rmi.RmiBasedExporter"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(named("invoke")) | ||
.and(takesArgument(0, named("org.springframework.remoting.support.RemoteInvocation"))), | ||
this.getClass().getName() + "$InvokeMethodAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class InvokeMethodAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.This RmiBasedExporter thisObject, | ||
@Advice.Argument(0) RemoteInvocation remoteInv, | ||
@Advice.Local("otelCallDepth") CallDepth callDepth, | ||
@Advice.Local("otelRequest") ClassAndMethod request, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
callDepth = CallDepth.forClass(RmiBasedExporter.class); | ||
if (callDepth.getAndIncrement() > 0) { | ||
return; | ||
} | ||
|
||
Context parentContext = THREAD_LOCAL_CONTEXT.getAndResetContext(); | ||
Class<?> serverClass = thisObject.getService().getClass(); | ||
String methodName = remoteInv.getMethodName(); | ||
request = ClassAndMethod.create(serverClass, methodName); | ||
|
||
if (!serverInstrumenter().shouldStart(parentContext, request)) { | ||
return; | ||
} | ||
|
||
context = serverInstrumenter().start(parentContext, request); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void stopSpan( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelCallDepth") CallDepth callDepth, | ||
@Advice.Local("otelRequest") ClassAndMethod request, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
if (callDepth.decrementAndGet() > 0) { | ||
return; | ||
} | ||
if (scope == null) { | ||
return; | ||
} | ||
scope.close(); | ||
serverInstrumenter().end(context, request, null, throwable); | ||
} | ||
} | ||
} |
Oops, something went wrong.