|
| 1 | +package io.opentelemetry.javaagent.instrumentation.camunda.v7_18.behavior; |
| 2 | + |
| 3 | +import static io.opentelemetry.javaagent.instrumentation.camunda.v7_18.behavior.CamundaBehaviorSingletons.getInstumenter; |
| 4 | +import static io.opentelemetry.javaagent.instrumentation.camunda.v7_18.behavior.CamundaBehaviorSingletons.getOpentelemetry; |
| 5 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
| 6 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType; |
| 7 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 8 | + |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +import org.camunda.bpm.engine.impl.pvm.delegate.ActivityExecution; |
| 12 | +import org.camunda.bpm.engine.variable.VariableMap; |
| 13 | + |
| 14 | +import java.util.logging.Level; |
| 15 | + |
| 16 | +import io.opentelemetry.javaagent.camunda.v7_18.behavior.CamundaActivityExecutionGetter; |
| 17 | +import io.opentelemetry.javaagent.camunda.v7_18.behavior.CamundaVariableMapSetter; |
| 18 | +import io.opentelemetry.javaagent.camunda.v7_18.common.CamundaCommonRequest; |
| 19 | + |
| 20 | +import io.opentelemetry.api.trace.SpanContext; |
| 21 | +import io.opentelemetry.context.Context; |
| 22 | +import io.opentelemetry.context.Scope; |
| 23 | +import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
| 24 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 25 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 26 | +import net.bytebuddy.asm.Advice; |
| 27 | +import net.bytebuddy.description.type.TypeDescription; |
| 28 | +import net.bytebuddy.matcher.ElementMatcher; |
| 29 | +import net.bytebuddy.matcher.ElementMatchers; |
| 30 | + |
| 31 | +public class CamundaCallableElementActivityBehaviorInstrumentation implements TypeInstrumentation { |
| 32 | + |
| 33 | + @Override |
| 34 | + public ElementMatcher<ClassLoader> classLoaderOptimization() { |
| 35 | + return hasClassesNamed("org.camunda.bpm.engine.impl.bpmn.behavior.CallableElementActivityBehavior"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 40 | + return hasSuperType(named("org.camunda.bpm.engine.impl.bpmn.behavior.CallableElementActivityBehavior")); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void transform(TypeTransformer transformer) { |
| 45 | + transformer.applyAdviceToMethod(ElementMatchers.isMethod().and(ElementMatchers.named("startInstance")), |
| 46 | + this.getClass().getName() + "$CamundaCallableElementActivityBehaviorAdvice"); |
| 47 | + } |
| 48 | + |
| 49 | + public static class CamundaCallableElementActivityBehaviorAdvice { |
| 50 | + |
| 51 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 52 | + public static void addTracingEnter(@Advice.Argument(0) ActivityExecution execution, |
| 53 | + @Advice.Argument(1) VariableMap variables, @Advice.Local("request") CamundaCommonRequest request, |
| 54 | + @Advice.Local("otelParentScope") Scope parentScope, @Advice.Local("otelContext") Context context, |
| 55 | + @Advice.Local("otelScope") Scope scope) { |
| 56 | + |
| 57 | + |
| 58 | + if (execution == null) { |
| 59 | + //log warning |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + request = new CamundaCommonRequest(); |
| 64 | + request.setProcessDefinitionId(Optional.ofNullable(execution.getProcessDefinitionId())); |
| 65 | + request.setProcessInstanceId(Optional.ofNullable(execution.getProcessInstanceId())); |
| 66 | + request.setActivityId(Optional.ofNullable(execution.getCurrentActivityId())); |
| 67 | + request.setActivityName(Optional.ofNullable(execution.getCurrentActivityName())); |
| 68 | + |
| 69 | + String processInstanceId = execution.getProcessInstanceId(); |
| 70 | + |
| 71 | + if (Java8BytecodeBridge.currentContext() == Java8BytecodeBridge.rootContext()) { |
| 72 | + //log |
| 73 | + } |
| 74 | + |
| 75 | + Context parentContext = getOpentelemetry().getPropagators().getTextMapPropagator() |
| 76 | + .extract(Java8BytecodeBridge.currentContext(), execution, new CamundaActivityExecutionGetter()); |
| 77 | + |
| 78 | + parentScope = parentContext.makeCurrent(); |
| 79 | + |
| 80 | + if (getInstumenter().shouldStart(Java8BytecodeBridge.currentContext(), request)) { |
| 81 | + context = getInstumenter().start(Java8BytecodeBridge.currentContext(), request); |
| 82 | + scope = context.makeCurrent(); |
| 83 | + |
| 84 | + // Inject subflow trace context as pi variables so they are propagated and |
| 85 | + // accessible |
| 86 | + |
| 87 | + SpanContext currentSpanContext = Java8BytecodeBridge.spanFromContext(context).getSpanContext(); |
| 88 | + if (currentSpanContext.isValid()) { |
| 89 | + getOpentelemetry().getPropagators().getTextMapPropagator().inject(context, variables, |
| 90 | + new CamundaVariableMapSetter()); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 98 | + public static void closeTrace(@Advice.Argument(0) ActivityExecution execution, |
| 99 | + @Advice.Local("request") CamundaCommonRequest request, |
| 100 | + @Advice.Local("otelParentScope") Scope parentScope, @Advice.Local("otelContext") Context context, |
| 101 | + @Advice.Local("otelScope") Scope scope, @Advice.Thrown Throwable throwable) { |
| 102 | + |
| 103 | + |
| 104 | + if (context != null && scope != null) { |
| 105 | + getInstumenter().end(context, request, "NA", throwable); |
| 106 | + scope.close(); |
| 107 | + } |
| 108 | + |
| 109 | + if (parentScope != null) { |
| 110 | + parentScope.close(); |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments