10
10
import io .opentelemetry .instrumentation .api .instrumenter .AttributesExtractor ;
11
11
import io .opentelemetry .instrumentation .api .internal .cache .Cache ;
12
12
import java .lang .reflect .Method ;
13
- import java .lang .reflect .Parameter ;
14
13
import javax .annotation .Nullable ;
15
14
16
15
/** Extractor of {@link io.opentelemetry.api.common.Attributes} for a traced method. */
@@ -22,7 +21,7 @@ public final class MethodSpanAttributesExtractor<REQUEST, RESPONSE>
22
21
private final Cache <Method , AttributeBindings > cache ;
23
22
private final ParameterAttributeNamesExtractor parameterAttributeNamesExtractor ;
24
23
25
- public static <REQUEST , RESPONSE > MethodSpanAttributesExtractor <REQUEST , RESPONSE > newInstance (
24
+ public static <REQUEST , RESPONSE > MethodSpanAttributesExtractor <REQUEST , RESPONSE > create (
26
25
MethodExtractor <REQUEST > methodExtractor ,
27
26
ParameterAttributeNamesExtractor parameterAttributeNamesExtractor ,
28
27
MethodArgumentsExtractor <REQUEST > methodArgumentsExtractor ) {
@@ -48,7 +47,9 @@ public static <REQUEST, RESPONSE> MethodSpanAttributesExtractor<REQUEST, RESPONS
48
47
@ Override
49
48
public void onStart (AttributesBuilder attributes , Context parentContext , REQUEST request ) {
50
49
Method method = methodExtractor .extract (request );
51
- AttributeBindings bindings = cache .computeIfAbsent (method , this ::bind );
50
+ AttributeBindings bindings =
51
+ cache .computeIfAbsent (
52
+ method , (Method m ) -> AttributeBindings .bind (m , parameterAttributeNamesExtractor ));
52
53
if (!bindings .isEmpty ()) {
53
54
Object [] args = methodArgumentsExtractor .extract (request );
54
55
bindings .apply (attributes , args );
@@ -62,82 +63,4 @@ public void onEnd(
62
63
REQUEST request ,
63
64
@ Nullable RESPONSE response ,
64
65
@ Nullable Throwable error ) {}
65
-
66
- /**
67
- * Creates a binding of the parameters of the traced method to span attributes.
68
- *
69
- * @param method the traced method
70
- * @return the bindings of the parameters
71
- */
72
- private AttributeBindings bind (Method method ) {
73
- AttributeBindings bindings = EmptyAttributeBindings .INSTANCE ;
74
-
75
- Parameter [] parameters = method .getParameters ();
76
- if (parameters .length == 0 ) {
77
- return bindings ;
78
- }
79
-
80
- String [] attributeNames = parameterAttributeNamesExtractor .extract (method , parameters );
81
- if (attributeNames .length != parameters .length ) {
82
- return bindings ;
83
- }
84
-
85
- for (int i = 0 ; i < parameters .length ; i ++) {
86
- Parameter parameter = parameters [i ];
87
- String attributeName = attributeNames [i ];
88
- if (attributeName == null || attributeName .isEmpty ()) {
89
- continue ;
90
- }
91
-
92
- bindings =
93
- new CombinedAttributeBindings (
94
- bindings ,
95
- i ,
96
- AttributeBindingFactory .createBinding (
97
- attributeName , parameter .getParameterizedType ()));
98
- }
99
-
100
- return bindings ;
101
- }
102
-
103
- protected enum EmptyAttributeBindings implements AttributeBindings {
104
- INSTANCE ;
105
-
106
- @ Override
107
- public boolean isEmpty () {
108
- return true ;
109
- }
110
-
111
- @ Override
112
- public void apply (AttributesBuilder target , Object [] args ) {}
113
- }
114
-
115
- private static final class CombinedAttributeBindings implements AttributeBindings {
116
- private final AttributeBindings parent ;
117
- private final int index ;
118
- private final AttributeBinding binding ;
119
-
120
- public CombinedAttributeBindings (
121
- AttributeBindings parent , int index , AttributeBinding binding ) {
122
- this .parent = parent ;
123
- this .index = index ;
124
- this .binding = binding ;
125
- }
126
-
127
- @ Override
128
- public boolean isEmpty () {
129
- return false ;
130
- }
131
-
132
- @ Override
133
- public void apply (AttributesBuilder target , Object [] args ) {
134
- parent .apply (target , args );
135
- if (args != null && args .length > index ) {
136
- Object arg = args [index ];
137
- if (arg != null ) {
138
- binding .apply (target , arg );
139
- }
140
- }
141
- }
142
- }
143
66
}
0 commit comments