@@ -41,64 +41,78 @@ public void transform(TypeTransformer transformer) {
4141
4242 @ SuppressWarnings ("unused" )
4343 public static class StoreInContextAdvice {
44- @ Advice .OnMethodEnter (skipOn = Advice .OnDefaultValue .class )
45- public static Object onEnter () {
46- return null ;
47- }
48-
49- @ Advice .OnMethodExit (suppress = Throwable .class )
50- public static void onExit (
44+ @ Advice .OnMethodEnter (skipOn = Advice .OnNonDefaultValue .class )
45+ public static Context onEnter (
5146 @ Advice .This SpanKey applicationSpanKey ,
5247 @ Advice .Argument (0 ) Context applicationContext ,
53- @ Advice .Argument (1 ) Span applicationSpan ,
54- @ Advice .Return (readOnly = false ) Context newApplicationContext ) {
48+ @ Advice .Argument (1 ) Span applicationSpan ) {
5549
5650 io .opentelemetry .instrumentation .api .internal .SpanKey agentSpanKey =
5751 SpanKeyBridging .toAgentOrNull (applicationSpanKey );
5852 if (agentSpanKey == null ) {
59- return ;
53+ return null ;
6054 }
6155
6256 io .opentelemetry .context .Context agentContext =
6357 AgentContextStorage .getAgentContext (applicationContext );
6458
6559 io .opentelemetry .api .trace .Span agentSpan = Bridging .toAgentOrNull (applicationSpan );
6660 if (agentSpan == null ) {
67- return ;
61+ // if application span can not be bridged to agent span, this could happen when it is not
62+ // created through bridged GlobalOpenTelemetry, we'll let the original method run and
63+ // store the span in context without bridging
64+ return null ;
6865 }
6966
7067 io .opentelemetry .context .Context newAgentContext =
7168 agentSpanKey .storeInContext (agentContext , agentSpan );
7269
73- newApplicationContext = AgentContextStorage .toApplicationContext (newAgentContext );
70+ return AgentContextStorage .toApplicationContext (newAgentContext );
71+ }
72+
73+ @ Advice .OnMethodExit (suppress = Throwable .class )
74+ public static void onExit (
75+ @ Advice .Enter Context newApplicationContext ,
76+ @ Advice .Return (readOnly = false ) Context result ) {
77+
78+ if (newApplicationContext != null ) {
79+ result = newApplicationContext ;
80+ }
7481 }
7582 }
7683
7784 @ SuppressWarnings ("unused" )
7885 public static class FromContextOrNullAdvice {
79- @ Advice .OnMethodEnter (skipOn = Advice .OnDefaultValue .class )
80- public static Object onEnter () {
81- return null ;
82- }
83-
84- @ Advice .OnMethodExit (suppress = Throwable .class )
85- public static void onExit (
86- @ Advice .This SpanKey applicationSpanKey ,
87- @ Advice .Argument (0 ) Context applicationContext ,
88- @ Advice .Return (readOnly = false ) Span applicationSpan ) {
86+ @ Advice .OnMethodEnter (skipOn = Advice .OnNonDefaultValue .class )
87+ public static Span onEnter (
88+ @ Advice .This SpanKey applicationSpanKey , @ Advice .Argument (0 ) Context applicationContext ) {
8989
9090 io .opentelemetry .instrumentation .api .internal .SpanKey agentSpanKey =
9191 SpanKeyBridging .toAgentOrNull (applicationSpanKey );
9292 if (agentSpanKey == null ) {
93- return ;
93+ return null ;
9494 }
9595
9696 io .opentelemetry .context .Context agentContext =
9797 AgentContextStorage .getAgentContext (applicationContext );
9898
9999 io .opentelemetry .api .trace .Span agentSpan = agentSpanKey .fromContextOrNull (agentContext );
100+ if (agentSpan == null ) {
101+ // Bridged agent span was not found. Run the original method, there could be an unbridged
102+ // span stored in the application context.
103+ return null ;
104+ }
100105
101- applicationSpan = agentSpan == null ? null : Bridging .toApplication (agentSpan );
106+ return Bridging .toApplication (agentSpan );
107+ }
108+
109+ @ Advice .OnMethodExit (suppress = Throwable .class )
110+ public static void onExit (
111+ @ Advice .Enter Span applicationSpan , @ Advice .Return (readOnly = false ) Span result ) {
112+
113+ if (applicationSpan != null ) {
114+ result = applicationSpan ;
115+ }
102116 }
103117 }
104118}
0 commit comments