Migrate ResponseSendAdvice to Instrumenter (#4488)
* Migrate ResponseSendAdvice to Instrumenter * Add shouldStart checks
This commit is contained in:
parent
3d92cd2337
commit
e19d086d58
|
@ -5,7 +5,7 @@
|
|||
|
||||
package io.opentelemetry.javaagent.instrumentation.servlet.v5_0.response;
|
||||
|
||||
import static io.opentelemetry.javaagent.instrumentation.servlet.v5_0.response.ResponseTracer.tracer;
|
||||
import static io.opentelemetry.javaagent.instrumentation.servlet.v5_0.response.ResponseSingletons.instrumenter;
|
||||
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.context.Scope;
|
||||
|
@ -29,15 +29,20 @@ public class ResponseSendAdvice {
|
|||
if (callDepth.getAndIncrement() > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Context parentContext = Java8BytecodeBridge.currentContext();
|
||||
// Don't want to generate a new top-level span
|
||||
if (Java8BytecodeBridge.currentSpan().getSpanContext().isValid()) {
|
||||
context = tracer().startSpan(method);
|
||||
scope = context.makeCurrent();
|
||||
if (Java8BytecodeBridge.spanFromContext(parentContext).getSpanContext().isValid()) {
|
||||
if (instrumenter().shouldStart(parentContext, method)) {
|
||||
context = instrumenter().start(parentContext, method);
|
||||
scope = context.makeCurrent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
||||
public static void stopSpan(
|
||||
@Advice.Origin Method method,
|
||||
@Advice.Thrown Throwable throwable,
|
||||
@Advice.Local("otelContext") Context context,
|
||||
@Advice.Local("otelScope") Scope scope,
|
||||
|
@ -45,6 +50,6 @@ public class ResponseSendAdvice {
|
|||
if (callDepth.decrementAndGet() > 0) {
|
||||
return;
|
||||
}
|
||||
HttpServletResponseAdviceHelper.stopSpan(tracer(), throwable, context, scope);
|
||||
HttpServletResponseAdviceHelper.stopSpan(instrumenter(), throwable, context, scope, method);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.javaagent.instrumentation.servlet.v5_0.response;
|
||||
|
||||
import io.opentelemetry.api.GlobalOpenTelemetry;
|
||||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
|
||||
import io.opentelemetry.instrumentation.api.tracer.SpanNames;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class ResponseSingletons {
|
||||
|
||||
private static final Instrumenter<Method, Void> INSTRUMENTER;
|
||||
|
||||
static {
|
||||
INSTRUMENTER =
|
||||
Instrumenter.<Method, Void>builder(
|
||||
GlobalOpenTelemetry.get(), "io.opentelemetry.servlet-5.0", SpanNames::fromMethod)
|
||||
.newInstrumenter();
|
||||
}
|
||||
|
||||
public static Instrumenter<Method, Void> instrumenter() {
|
||||
return INSTRUMENTER;
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.javaagent.instrumentation.servlet.v5_0.response;
|
||||
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
|
||||
import io.opentelemetry.instrumentation.api.tracer.SpanNames;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class ResponseTracer extends BaseTracer {
|
||||
private static final ResponseTracer TRACER = new ResponseTracer();
|
||||
|
||||
public static ResponseTracer tracer() {
|
||||
return TRACER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getInstrumentationName() {
|
||||
return "io.opentelemetry.servlet-5.0";
|
||||
}
|
||||
|
||||
public Context startSpan(Method method) {
|
||||
return startSpan(SpanNames.fromMethod(method));
|
||||
}
|
||||
}
|
|
@ -7,19 +7,20 @@ package io.opentelemetry.javaagent.instrumentation.servlet.common.response;
|
|||
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.context.Scope;
|
||||
import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
|
||||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class HttpServletResponseAdviceHelper {
|
||||
public static void stopSpan(
|
||||
BaseTracer tracer, Throwable throwable, Context context, Scope scope) {
|
||||
if (context != null) {
|
||||
Instrumenter<Method, Void> instrumenter,
|
||||
Throwable throwable,
|
||||
Context context,
|
||||
Scope scope,
|
||||
Method request) {
|
||||
if (scope != null) {
|
||||
scope.close();
|
||||
|
||||
if (throwable != null) {
|
||||
tracer.endExceptionally(context, throwable);
|
||||
} else {
|
||||
tracer.end(context);
|
||||
}
|
||||
instrumenter.end(context, request, null, throwable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
package io.opentelemetry.javaagent.instrumentation.servlet.javax.response;
|
||||
|
||||
import static io.opentelemetry.javaagent.instrumentation.servlet.javax.response.ResponseTracer.tracer;
|
||||
import static io.opentelemetry.javaagent.instrumentation.servlet.javax.response.ResponseSingletons.instrumenter;
|
||||
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.context.Scope;
|
||||
|
@ -29,15 +29,19 @@ public class ResponseSendAdvice {
|
|||
if (callDepth.getAndIncrement() > 0) {
|
||||
return;
|
||||
}
|
||||
Context parentContext = Java8BytecodeBridge.currentContext();
|
||||
// Don't want to generate a new top-level span
|
||||
if (Java8BytecodeBridge.currentSpan().getSpanContext().isValid()) {
|
||||
context = tracer().startSpan(method);
|
||||
scope = context.makeCurrent();
|
||||
if (Java8BytecodeBridge.spanFromContext(parentContext).getSpanContext().isValid()) {
|
||||
if (instrumenter().shouldStart(parentContext, method)) {
|
||||
context = instrumenter().start(parentContext, method);
|
||||
scope = context.makeCurrent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
||||
public static void stopSpan(
|
||||
@Advice.Origin Method method,
|
||||
@Advice.Thrown Throwable throwable,
|
||||
@Advice.Local("otelContext") Context context,
|
||||
@Advice.Local("otelScope") Scope scope,
|
||||
|
@ -45,6 +49,6 @@ public class ResponseSendAdvice {
|
|||
if (callDepth.decrementAndGet() > 0) {
|
||||
return;
|
||||
}
|
||||
HttpServletResponseAdviceHelper.stopSpan(tracer(), throwable, context, scope);
|
||||
HttpServletResponseAdviceHelper.stopSpan(instrumenter(), throwable, context, scope, method);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.javaagent.instrumentation.servlet.javax.response;
|
||||
|
||||
import io.opentelemetry.api.GlobalOpenTelemetry;
|
||||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
|
||||
import io.opentelemetry.instrumentation.api.tracer.SpanNames;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class ResponseSingletons {
|
||||
|
||||
private static final Instrumenter<Method, Void> INSTRUMENTER;
|
||||
|
||||
static {
|
||||
INSTRUMENTER =
|
||||
Instrumenter.<Method, Void>builder(
|
||||
GlobalOpenTelemetry.get(),
|
||||
"io.opentelemetry.servlet-javax-common",
|
||||
SpanNames::fromMethod)
|
||||
.newInstrumenter();
|
||||
}
|
||||
|
||||
public static Instrumenter<Method, Void> instrumenter() {
|
||||
return INSTRUMENTER;
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.javaagent.instrumentation.servlet.javax.response;
|
||||
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
|
||||
import io.opentelemetry.instrumentation.api.tracer.SpanNames;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class ResponseTracer extends BaseTracer {
|
||||
private static final ResponseTracer TRACER = new ResponseTracer();
|
||||
|
||||
public static ResponseTracer tracer() {
|
||||
return TRACER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getInstrumentationName() {
|
||||
return "io.opentelemetry.servlet-javax-common";
|
||||
}
|
||||
|
||||
public Context startSpan(Method method) {
|
||||
return startSpan(SpanNames.fromMethod(method));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue