@@ -59,12 +59,14 @@ final class InstrumentationApiContextBridging {
59
59
private static final MethodHandle AGENT_GET_METHOD ;
60
60
private static final MethodHandle AGENT_GET_ROUTE ;
61
61
private static final MethodHandle AGENT_GET_UPDATED_BY_SOURCE_ORDER ;
62
+ private static final MethodHandle AGENT_GET_SPAN ;
62
63
63
64
private static final Class <?> APPLICATION_HTTP_ROUTE_STATE ;
64
65
private static final MethodHandle APPLICATION_CREATE ;
65
66
private static final MethodHandle APPLICATION_GET_METHOD ;
66
67
private static final MethodHandle APPLICATION_GET_ROUTE ;
67
68
private static final MethodHandle APPLICATION_GET_UPDATED_BY_SOURCE_ORDER ;
69
+ private static final MethodHandle APPLICATION_GET_SPAN ;
68
70
69
71
static {
70
72
MethodHandles .Lookup lookup = MethodHandles .lookup ();
@@ -74,11 +76,13 @@ final class InstrumentationApiContextBridging {
74
76
MethodHandle agentGetMethod = null ;
75
77
MethodHandle agentGetRoute = null ;
76
78
MethodHandle agentGetUpdatedBySourceOrder = null ;
79
+ MethodHandle agentGetSpan = null ;
77
80
Class <?> applicationHttpRouteState = null ;
78
81
MethodHandle applicationCreate = null ;
79
82
MethodHandle applicationGetMethod = null ;
80
83
MethodHandle applicationGetRoute = null ;
81
84
MethodHandle applicationGetUpdatedBySourceOrder = null ;
85
+ MethodHandle applicationGetSpan = null ;
82
86
83
87
try {
84
88
agentHttpRouteState =
@@ -87,23 +91,43 @@ final class InstrumentationApiContextBridging {
87
91
lookup .findStatic (
88
92
agentHttpRouteState ,
89
93
"create" ,
90
- MethodType .methodType (agentHttpRouteState , String .class , String .class , int .class ));
94
+ MethodType .methodType (
95
+ agentHttpRouteState ,
96
+ String .class ,
97
+ String .class ,
98
+ int .class ,
99
+ io .opentelemetry .api .trace .Span .class ));
91
100
agentGetMethod =
92
101
lookup .findVirtual (agentHttpRouteState , "getMethod" , MethodType .methodType (String .class ));
93
102
agentGetRoute =
94
103
lookup .findVirtual (agentHttpRouteState , "getRoute" , MethodType .methodType (String .class ));
95
104
agentGetUpdatedBySourceOrder =
96
105
lookup .findVirtual (
97
106
agentHttpRouteState , "getUpdatedBySourceOrder" , MethodType .methodType (int .class ));
107
+ agentGetSpan =
108
+ lookup .findVirtual (
109
+ agentHttpRouteState ,
110
+ "getSpan" ,
111
+ MethodType .methodType (io .opentelemetry .api .trace .Span .class ));
98
112
99
113
applicationHttpRouteState =
100
114
Class .forName ("application.io.opentelemetry.instrumentation.api.internal.HttpRouteState" );
101
- applicationCreate =
102
- lookup .findStatic (
103
- applicationHttpRouteState ,
104
- "create" ,
105
- MethodType .methodType (
106
- applicationHttpRouteState , String .class , String .class , int .class ));
115
+ try {
116
+ applicationCreate =
117
+ lookup .findStatic (
118
+ applicationHttpRouteState ,
119
+ "create" ,
120
+ MethodType .methodType (
121
+ applicationHttpRouteState , String .class , String .class , int .class , Span .class ));
122
+ } catch (NoSuchMethodException exception ) {
123
+ // older instrumentation-api has only the variant that does not take span
124
+ applicationCreate =
125
+ lookup .findStatic (
126
+ applicationHttpRouteState ,
127
+ "create" ,
128
+ MethodType .methodType (
129
+ applicationHttpRouteState , String .class , String .class , int .class ));
130
+ }
107
131
applicationGetMethod =
108
132
lookup .findVirtual (
109
133
applicationHttpRouteState , "getMethod" , MethodType .methodType (String .class ));
@@ -115,6 +139,13 @@ final class InstrumentationApiContextBridging {
115
139
applicationHttpRouteState ,
116
140
"getUpdatedBySourceOrder" ,
117
141
MethodType .methodType (int .class ));
142
+ try {
143
+ applicationGetSpan =
144
+ lookup .findVirtual (
145
+ applicationHttpRouteState , "getSpan" , MethodType .methodType (Span .class ));
146
+ } catch (NoSuchMethodException ignored ) {
147
+ // not present in older instrumentation-api
148
+ }
118
149
} catch (Throwable ignored ) {
119
150
// instrumentation-api may be absent on the classpath, or it might be an older version
120
151
}
@@ -124,11 +155,13 @@ final class InstrumentationApiContextBridging {
124
155
AGENT_GET_METHOD = agentGetMethod ;
125
156
AGENT_GET_ROUTE = agentGetRoute ;
126
157
AGENT_GET_UPDATED_BY_SOURCE_ORDER = agentGetUpdatedBySourceOrder ;
158
+ AGENT_GET_SPAN = agentGetSpan ;
127
159
APPLICATION_HTTP_ROUTE_STATE = applicationHttpRouteState ;
128
160
APPLICATION_CREATE = applicationCreate ;
129
161
APPLICATION_GET_METHOD = applicationGetMethod ;
130
162
APPLICATION_GET_ROUTE = applicationGetRoute ;
131
163
APPLICATION_GET_UPDATED_BY_SOURCE_ORDER = applicationGetUpdatedBySourceOrder ;
164
+ APPLICATION_GET_SPAN = applicationGetSpan ;
132
165
}
133
166
134
167
@ Nullable
@@ -151,12 +184,16 @@ final class InstrumentationApiContextBridging {
151
184
APPLICATION_CREATE ,
152
185
AGENT_GET_METHOD ,
153
186
AGENT_GET_ROUTE ,
154
- AGENT_GET_UPDATED_BY_SOURCE_ORDER ),
187
+ AGENT_GET_UPDATED_BY_SOURCE_ORDER ,
188
+ AGENT_GET_SPAN ,
189
+ o -> o != null ? Bridging .toApplication ((io .opentelemetry .api .trace .Span ) o ) : null ),
155
190
httpRouteStateConvert (
156
191
AGENT_CREATE ,
157
192
APPLICATION_GET_METHOD ,
158
193
APPLICATION_GET_ROUTE ,
159
- APPLICATION_GET_UPDATED_BY_SOURCE_ORDER ));
194
+ APPLICATION_GET_UPDATED_BY_SOURCE_ORDER ,
195
+ APPLICATION_GET_SPAN ,
196
+ o -> o != null ? Bridging .toAgentOrNull ((Span ) o ) : null ));
160
197
} catch (Throwable ignored ) {
161
198
return null ;
162
199
}
@@ -166,12 +203,18 @@ private static Function<Object, Object> httpRouteStateConvert(
166
203
MethodHandle create ,
167
204
MethodHandle getMethod ,
168
205
MethodHandle getRoute ,
169
- MethodHandle getUpdatedBySourceOrder ) {
206
+ MethodHandle getUpdatedBySourceOrder ,
207
+ MethodHandle getSpan ,
208
+ Function <Object , Object > convertSpan ) {
170
209
return httpRouteHolder -> {
171
210
try {
172
211
String method = (String ) getMethod .invoke (httpRouteHolder );
173
212
String route = (String ) getRoute .invoke (httpRouteHolder );
174
213
int updatedBySourceOrder = (int ) getUpdatedBySourceOrder .invoke (httpRouteHolder );
214
+ if (create .type ().parameterCount () == 4 && getSpan != null ) {
215
+ Object span = convertSpan .apply (getSpan .invoke (httpRouteHolder ));
216
+ return create .invoke (method , route , updatedBySourceOrder , span );
217
+ }
175
218
return create .invoke (method , route , updatedBySourceOrder );
176
219
} catch (Throwable e ) {
177
220
return null ;
0 commit comments