Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -10,6 +10,7 @@
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;

/**
Expand All @@ -26,7 +27,8 @@
* </ul>
*/
@AutoService(InstrumentationModule.class)
public class LibertyInstrumentationModule extends InstrumentationModule {
public class LibertyInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public LibertyInstrumentationModule() {
super("liberty", "liberty-20.0");
Expand All @@ -36,4 +38,9 @@ public LibertyInstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new LibertyWebAppInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext;
import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Accessor;
import javax.annotation.Nullable;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -51,33 +52,33 @@ public void transform(TypeTransformer transformer) {
public static class HandleRequestAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
public static boolean onEnter(
@Advice.Argument(value = 0) ServletRequest request,
@Advice.Argument(value = 1) ServletResponse response,
@Advice.Local("otelHandled") boolean handled) {
@Advice.Argument(value = 1) ServletResponse response) {

// liberty has two handleRequest methods, skip processing when thread local context is already
// set up
handled = ThreadLocalContext.get() == null;
boolean handled = ThreadLocalContext.get() == null;
if (!handled
|| !(request instanceof HttpServletRequest)
|| !(response instanceof HttpServletResponse)) {
return;
return false;
}

HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// it is a bit too early to start span at this point because calling
// some methods on HttpServletRequest will give a NPE
// just remember the request and use it a bit later to start the span
ThreadLocalContext.startRequest(httpServletRequest, (HttpServletResponse) response);
return true;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Argument(0) ServletRequest servletRequest,
@Advice.Argument(1) ServletResponse servletResponse,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelHandled") boolean handled) {
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter boolean handled) {
if (!handled) {
return;
}
Expand Down
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 LibertyDispatcherInstrumentationModule extends InstrumentationModule {
public class LibertyDispatcherInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public LibertyDispatcherInstrumentationModule() {
super("liberty-dispatcher", "liberty-dispatcher-20.0", "liberty");
Expand All @@ -23,4 +25,9 @@ public LibertyDispatcherInstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new LibertyDispatcherLinkInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,31 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class SendResponseAdvice {

public static class AdviceLocals {
public LibertyRequest request;
public Context context;
public Scope scope;
}

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.FieldValue("isc") HttpInboundServiceContextImpl isc,
@Advice.Local("otelRequest") LibertyRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
public static AdviceLocals onEnter(
@Advice.FieldValue("isc") HttpInboundServiceContextImpl isc) {

AdviceLocals locals = new AdviceLocals();
Context parentContext = Java8BytecodeBridge.currentContext();
request =
locals.request =
new LibertyRequest(
isc.getRequest(),
isc.getLocalAddr(),
isc.getLocalPort(),
isc.getRemoteAddr(),
isc.getRemotePort());
if (!instrumenter().shouldStart(parentContext, request)) {
return;
if (!instrumenter().shouldStart(parentContext, locals.request)) {
return locals;
}
context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
locals.context = instrumenter().start(parentContext, locals.request);
locals.scope = locals.context.makeCurrent();
return locals;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand All @@ -77,18 +83,17 @@ public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Argument(value = 0) StatusCodes statusCode,
@Advice.Argument(value = 2) Exception failure,
@Advice.Local("otelRequest") LibertyRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
@Advice.Enter AdviceLocals locals) {

if (locals.scope == null) {
return;
}
scope.close();
locals.scope.close();

LibertyResponse response = new LibertyResponse(httpDispatcherLink, statusCode);

Throwable t = failure != null ? failure : throwable;
instrumenter().end(context, request, response, t);
instrumenter().end(locals.context, locals.request, response, t);
}
}
}
Loading