Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -47,32 +47,44 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class HttpJspPageAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) HttpServletRequest req,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = Java8BytecodeBridge.currentContext();
if (!instrumenter().shouldStart(parentContext, req)) {
return;
public static class AdviceScope {
private final Context context;
private final Scope scope;

private AdviceScope(Context context, Scope scope) {
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(HttpServletRequest req) {
Context parentContext = Context.current();
if (!instrumenter().shouldStart(parentContext, req)) {
return null;
}
Context context = instrumenter().start(parentContext, req);
return new AdviceScope(context, context.makeCurrent());
}

context = instrumenter().start(parentContext, req);
scope = context.makeCurrent();
public void end(HttpServletRequest req, Throwable throwable) {
scope.close();
instrumenter().end(context, req, null, throwable);
}
}

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onEnter(@Advice.Argument(0) HttpServletRequest req) {
return AdviceScope.start(req);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Argument(0) HttpServletRequest req,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(req, throwable);
}

scope.close();
instrumenter().end(context, req, null, throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand All @@ -37,32 +37,45 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class CompileAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This JspCompilationContext jspCompilationContext,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = Java8BytecodeBridge.currentContext();
if (!instrumenter().shouldStart(parentContext, jspCompilationContext)) {
return;
public static class AdviceScope {
private final Context context;
private final Scope scope;

private AdviceScope(Context context, Scope scope) {
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(JspCompilationContext jspCompilationContext) {
Context parentContext = Context.current();
if (!instrumenter().shouldStart(parentContext, jspCompilationContext)) {
return null;
}
Context context = instrumenter().start(parentContext, jspCompilationContext);
return new AdviceScope(context, context.makeCurrent());
}

context = instrumenter().start(parentContext, jspCompilationContext);
scope = context.makeCurrent();
public void end(@Nullable Throwable throwable, JspCompilationContext jspCompilationContext) {
scope.close();
instrumenter().end(context, jspCompilationContext, null, throwable);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onEnter(@Advice.This JspCompilationContext jspCompilationContext) {
return AdviceScope.start(jspCompilationContext);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.This JspCompilationContext jspCompilationContext,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(throwable, jspCompilationContext);
}

scope.close();
instrumenter().end(context, jspCompilationContext, null, throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class JspInstrumentationModule extends InstrumentationModule {
public class JspInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {
public JspInstrumentationModule() {
super("jsp", "jsp-2.3");
}
Expand All @@ -22,4 +24,9 @@ public JspInstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return asList(new HttpJspPageInstrumentation(), new JspCompilationContextInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Loading