2525import net .bytebuddy .matcher .ElementMatchers ;
2626import org .camunda .bpm .engine .impl .pvm .delegate .ActivityExecution ;
2727
28- public class CamundaTaskActivityBehaviorInstrumentation implements TypeInstrumentation {
28+ public class CamundaCommonBehaviorInstrumentation implements TypeInstrumentation {
2929
3030 @ Override
3131 public ElementMatcher <ClassLoader > classLoaderOptimization () {
3232 return hasClassesNamed ("org.camunda.bpm.engine.impl.bpmn.behavior.TaskActivityBehavior" );
3333 }
3434
35- @ Override
36- public ElementMatcher <TypeDescription > typeMatcher () {
37- return hasSuperType (named ("org.camunda.bpm.engine.impl.bpmn.behavior.TaskActivityBehavior" ));
38- }
35+ @ Override
36+ public ElementMatcher <TypeDescription > typeMatcher () {
37+ return hasSuperType (named ("org.camunda.bpm.engine.impl.bpmn.behavior.TaskActivityBehavior" ))
38+ .or (named ("org.camunda.bpm.engine.impl.bpmn.behavior.ExternalTaskActivityBehavior" ))
39+ .or (named ("org.camunda.bpm.engine.impl.bpmn.behavior.TerminateEndEventActivityBehavior" ))
40+ .or (named ("org.camunda.bpm.engine.impl.bpmn.behavior.NoneEndEventActivityBehavior" ))
41+ .or (named ("org.camunda.bpm.engine.impl.bpmn.behavior.ErrorEndEventActivityBehavior" ));
42+ // elements that have been tested with instrumentation, eventually this can be
43+ // replaced by the supertype AbstractBpmnActivityBehavior, once all elements
44+ // instrumentations have been certified and instrumented, but will need to make
45+ // sure its not callable element as the instrumentation should remain separate
46+ // due to logic
47+ }
3948
4049 @ Override
4150 public void transform (TypeTransformer transformer ) {
4251 transformer .applyAdviceToMethod (
43- ElementMatchers .isMethod ().and (ElementMatchers .named ("performExecution " )),
44- this .getClass ().getName () + "$CamundaTaskActivityBehaviorAdvice " );
52+ ElementMatchers .isMethod ().and (ElementMatchers .named ("execute " )),
53+ this .getClass ().getName () + "$CamundaCommonBehaviorAdvice " );
4554 }
4655
4756 @ SuppressWarnings ("unused" )
48- public static class CamundaTaskActivityBehaviorAdvice {
57+ public static class CamundaCommonBehaviorAdvice {
4958
5059 @ Advice .OnMethodEnter (suppress = Throwable .class )
5160 public static void addTracingEnter (
@@ -63,7 +72,35 @@ public static void addTracingEnter(
6372 request .setProcessDefinitionId (Optional .ofNullable (execution .getProcessDefinitionId ()));
6473 request .setProcessInstanceId (Optional .ofNullable (execution .getProcessInstanceId ()));
6574 request .setActivityId (Optional .ofNullable (execution .getCurrentActivityId ()));
66- request .setActivityName (Optional .ofNullable (execution .getCurrentActivityName ()));
75+
76+ name : {
77+ if (execution .getBpmnModelElementInstance () != null ) {
78+ // TODO lambda does not work due to access modifier
79+ if (execution .getBpmnModelElementInstance () instanceof EndEvent ) {
80+ getLogger ().info ("instance of EndEvent" );
81+ EndEvent e = (EndEvent ) execution .getBpmnModelElementInstance ();
82+
83+ if (e .getEventDefinitions () == null || e .getEventDefinitions ().isEmpty ()) {
84+ request .setActivityName (Optional .of ("End" ));
85+ }
86+ for (EventDefinition ed : e .getEventDefinitions ()) {
87+ if (ed instanceof TerminateEventDefinition ) {
88+ request .setActivityName (Optional .of ("End" ));
89+ } else if (ed instanceof ErrorEventDefinition ) {
90+ request .setActivityName (Optional .of ("Error End" ));
91+ } else if (ed instanceof CompensateEventDefinition ) {
92+ request .setActivityName (Optional .of ("Compensation End" ));
93+ } else {
94+ request .setActivityName (Optional .of ("End" ));
95+ }
96+ }
97+ break name ;
98+ } else if (execution .getBpmnModelElementInstance () instanceof Gateway ) {
99+ // TODO
100+ }
101+ }
102+ request .setActivityName (Optional .ofNullable (execution .getCurrentActivityName ()));
103+ }
67104
68105 Context parentContext =
69106 getOpentelemetry ()
@@ -79,12 +116,18 @@ public static void addTracingEnter(
79116 if (getInstumenter ().shouldStart (Java8BytecodeBridge .currentContext (), request )) {
80117 context = getInstumenter ().start (Java8BytecodeBridge .currentContext (), request );
81118 scope = context .makeCurrent ();
119+
120+ if (target .getClass () == org .camunda .bpm .engine .impl .bpmn .behavior .ExternalTaskActivityBehavior .class ) {
121+
122+ getOpentelemetry ().getPropagators ().getTextMapPropagator ().inject (context , execution ,
123+ new CamundaActivityExecutionLocalSetter ());
124+ }
82125 }
83126 }
84127
85128 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
86129 public static void closeTrace (
87- @ Advice .Local ("request" ) CamundaCommonRequest request ,
130+ @ Advice .Local ("request" ) CamundaCommonRequest request , @ Advice . This Object target ,
88131 @ Advice .Local ("otelParentScope" ) Scope parentScope ,
89132 @ Advice .Local ("otelContext" ) Context context ,
90133 @ Advice .Local ("otelScope" ) Scope scope ,
0 commit comments