Convert spring-ws-2.0 to instrumenter api
MODIFY
instrumentation/spring/spring-ws-2.0/javaagent/build.gradle.kts
instrumentation/spring/spring-ws-2.0/javaagent/build.gradle.kts
@@ -32,6 +32,9 @@ tasks {
dependencies {
compileOnly("org.springframework.ws:spring-ws-core:2.0.0.RELEASE")
+ compileOnly("com.google.auto.value:auto-value-annotations")
+ annotationProcessor("com.google.auto.value:auto-value")
+
testLibrary("org.springframework.boot:spring-boot-starter-web-services:2.0.0.RELEASE")
testLibrary("org.springframework.boot:spring-boot-starter-web:2.0.0.RELEASE")

MODIFY
instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/AnnotatedMethodInstrumentation.java
instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/AnnotatedMethodInstrumentation.java
package io.opentelemetry.javaagent.instrumentation.spring.ws;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
-import static io.opentelemetry.javaagent.instrumentation.spring.ws.SpringWsTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext;
+import static io.opentelemetry.javaagent.instrumentation.spring.ws.SpringWsSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
@@ -17,7 +18,6 @@ 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.CallDepth;
-import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
@@ -53,15 +53,24 @@ public class AnnotatedMethodInstrumentation implements TypeInstrumentation {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void startSpan(
- @Advice.Origin Method method,
+ @Advice.Origin("#t") Class<?> codeClass,
+ @Advice.Origin("#m") String methodName,
@Advice.Local("otelCallDepth") CallDepth callDepth,
+ @Advice.Local("otelRequest") SpringWsRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
callDepth = CallDepth.forClass(PayloadRoot.class);
if (callDepth.getAndIncrement() > 0) {
return;
}
- context = tracer().startSpan(method);
+
+ Context parentContext = currentContext();
+ request = SpringWsRequest.create(codeClass, methodName);
+ if (!instrumenter().shouldStart(parentContext, request)) {
+ return;
+ }
+
+ context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
}
@@ -69,6 +78,7 @@ public class AnnotatedMethodInstrumentation implements TypeInstrumentation {
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth,
+ @Advice.Local("otelRequest") SpringWsRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (callDepth.decrementAndGet() > 0) {
@@ -76,11 +86,7 @@ public class AnnotatedMethodInstrumentation implements TypeInstrumentation {
}
scope.close();
- if (throwable == null) {
- tracer().end(context);
- } else {
- tracer().endExceptionally(context, throwable);
- }
+ instrumenter().end(context, request, null, throwable);
}
}
}

ADD
instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/SpringWsCodeAttributesExtractor.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.spring.ws;
+
+import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesExtractor;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+public class SpringWsCodeAttributesExtractor
+ extends CodeAttributesExtractor<SpringWsRequest, Void> {
+
+ @Override
+ protected @Nullable Class<?> codeClass(SpringWsRequest request) {
+ return request.getCodeClass();
+ }
+
+ @Override
+ protected @Nullable String methodName(SpringWsRequest request) {
+ return request.getMethodName();
+ }
+
+ @Override
+ protected @Nullable String filePath(SpringWsRequest request) {
+ return null;
+ }
+
+ @Override
+ protected @Nullable Long lineNumber(SpringWsRequest request) {
+ return null;
+ }
+}

ADD
instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/SpringWsRequest.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.spring.ws;
+
+import com.google.auto.value.AutoValue;
+
+@AutoValue
+public abstract class SpringWsRequest {
+
+ public static SpringWsRequest create(Class<?> codeClass, String methodName) {
+ return new AutoValue_SpringWsRequest(codeClass, methodName);
+ }
+
+ public abstract Class<?> getCodeClass();
+
+ public abstract String getMethodName();
+}

ADD
instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/SpringWsSingletons.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.spring.ws;
+
+import io.opentelemetry.api.GlobalOpenTelemetry;
+import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
+import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesExtractor;
+import io.opentelemetry.instrumentation.api.instrumenter.code.CodeSpanNameExtractor;
+
+public class SpringWsSingletons {
+ private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-ws-2.0";
+
+ private static final Instrumenter<SpringWsRequest, Void> INSTRUMENTER;
+
+ static {
+ CodeAttributesExtractor<SpringWsRequest, Void> codeAttributes =
+ new SpringWsCodeAttributesExtractor();
+ INSTRUMENTER =
+ Instrumenter.<SpringWsRequest, Void>newBuilder(
+ GlobalOpenTelemetry.get(),
+ INSTRUMENTATION_NAME,
+ CodeSpanNameExtractor.create(codeAttributes))
+ .addAttributesExtractor(codeAttributes)
+ .newInstrumenter();
+ }
+
+ public static Instrumenter<SpringWsRequest, Void> instrumenter() {
+ return INSTRUMENTER;
+ }
+
+ private SpringWsSingletons() {}
+}

DELETE
instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/SpringWsTracer.java
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.javaagent.instrumentation.spring.ws;
-
-import io.opentelemetry.api.trace.Span;
-import io.opentelemetry.api.trace.SpanKind;
-import io.opentelemetry.context.Context;
-import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
-import io.opentelemetry.instrumentation.api.tracer.SpanNames;
-import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
-import java.lang.reflect.Method;
-
-public class SpringWsTracer extends BaseTracer {
-
- private static final SpringWsTracer TRACER = new SpringWsTracer();
-
- public static SpringWsTracer tracer() {
- return TRACER;
- }
-
- public Context startSpan(Method method) {
- Context parentContext = Context.current();
- Span span =
- spanBuilder(parentContext, SpanNames.fromMethod(method), SpanKind.INTERNAL)
- .setAttribute(SemanticAttributes.CODE_NAMESPACE, method.getDeclaringClass().getName())
- .setAttribute(SemanticAttributes.CODE_FUNCTION, method.getName())
- .startSpan();
- return parentContext.with(span);
- }
-
- @Override
- protected String getInstrumentationName() {
- return "io.opentelemetry.spring-ws-2.0";
- }
-}

Convert spring-ws-2.0 to instrumenter api (#3801)