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