@@ -41,64 +41,78 @@ public void transform(TypeTransformer transformer) {
41
41
42
42
@ SuppressWarnings ("unused" )
43
43
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 (
51
46
@ Advice .This SpanKey applicationSpanKey ,
52
47
@ Advice .Argument (0 ) Context applicationContext ,
53
- @ Advice .Argument (1 ) Span applicationSpan ,
54
- @ Advice .Return (readOnly = false ) Context newApplicationContext ) {
48
+ @ Advice .Argument (1 ) Span applicationSpan ) {
55
49
56
50
io .opentelemetry .instrumentation .api .internal .SpanKey agentSpanKey =
57
51
SpanKeyBridging .toAgentOrNull (applicationSpanKey );
58
52
if (agentSpanKey == null ) {
59
- return ;
53
+ return null ;
60
54
}
61
55
62
56
io .opentelemetry .context .Context agentContext =
63
57
AgentContextStorage .getAgentContext (applicationContext );
64
58
65
59
io .opentelemetry .api .trace .Span agentSpan = Bridging .toAgentOrNull (applicationSpan );
66
60
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 ;
68
65
}
69
66
70
67
io .opentelemetry .context .Context newAgentContext =
71
68
agentSpanKey .storeInContext (agentContext , agentSpan );
72
69
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
+ }
74
81
}
75
82
}
76
83
77
84
@ SuppressWarnings ("unused" )
78
85
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 ) {
89
89
90
90
io .opentelemetry .instrumentation .api .internal .SpanKey agentSpanKey =
91
91
SpanKeyBridging .toAgentOrNull (applicationSpanKey );
92
92
if (agentSpanKey == null ) {
93
- return ;
93
+ return null ;
94
94
}
95
95
96
96
io .opentelemetry .context .Context agentContext =
97
97
AgentContextStorage .getAgentContext (applicationContext );
98
98
99
99
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
+ }
100
105
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
+ }
102
116
}
103
117
}
104
118
}
0 commit comments