|
7 | 7 |
|
8 | 8 | import static java.util.Collections.emptyList; |
9 | 9 |
|
10 | | -import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter; |
| 10 | +import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientExperimentalAttributesGetter; |
11 | 11 | import java.lang.invoke.MethodHandle; |
12 | 12 | import java.lang.invoke.MethodHandles; |
13 | 13 | import java.lang.invoke.MethodType; |
14 | 14 | import java.util.List; |
| 15 | +import java.util.Map; |
15 | 16 | import javax.annotation.Nullable; |
16 | 17 | import org.springframework.http.HttpRequest; |
17 | 18 | import org.springframework.http.client.ClientHttpResponse; |
18 | 19 |
|
19 | 20 | enum SpringWebHttpAttributesGetter |
20 | | - implements HttpClientAttributesGetter<HttpRequest, ClientHttpResponse> { |
| 21 | + implements HttpClientExperimentalAttributesGetter<HttpRequest, ClientHttpResponse> { |
21 | 22 | INSTANCE; |
22 | 23 |
|
23 | 24 | @Override |
@@ -107,4 +108,56 @@ public String getServerAddress(HttpRequest httpRequest) { |
107 | 108 | public Integer getServerPort(HttpRequest httpRequest) { |
108 | 109 | return httpRequest.getURI().getPort(); |
109 | 110 | } |
| 111 | + |
| 112 | + private static final String URI_TEMPLATE_ATTRIBUTE = "org.springframework.web.client.RestClient.uriTemplate"; |
| 113 | + @Nullable private static final MethodHandle GET_ATTRIBUTES; |
| 114 | + |
| 115 | + static { |
| 116 | + MethodHandle getAttributes = null; |
| 117 | + Class<?> httpRequestClass = null; |
| 118 | + |
| 119 | + try { |
| 120 | + httpRequestClass = Class.forName("org.springframework.http.HttpRequest"); |
| 121 | + } catch (ClassNotFoundException e) { |
| 122 | + // ignored |
| 123 | + } |
| 124 | + |
| 125 | + if (httpRequestClass != null) { |
| 126 | + MethodHandles.Lookup lookup = MethodHandles.publicLookup(); |
| 127 | + try { |
| 128 | + getAttributes = |
| 129 | + lookup.findVirtual( |
| 130 | + httpRequestClass, |
| 131 | + "getAttributes", |
| 132 | + MethodType.methodType(java.util.Map.class)); |
| 133 | + } catch (NoSuchMethodException | IllegalAccessException ignored) { |
| 134 | + // ignored |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + GET_ATTRIBUTES = getAttributes; |
| 139 | + } |
| 140 | + |
| 141 | + @Nullable |
| 142 | + @Override |
| 143 | + public String getUrlTemplate(HttpRequest httpRequest) { |
| 144 | + if (GET_ATTRIBUTES == null) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + try { |
| 149 | + Object attributes = GET_ATTRIBUTES.invoke(httpRequest); |
| 150 | + if (attributes instanceof java.util.Map) { |
| 151 | + Map<?, ?> map = (Map<?, ?>) attributes; |
| 152 | + Object value = map.get(URI_TEMPLATE_ATTRIBUTE); |
| 153 | + if (value instanceof String) { |
| 154 | + return (String) value; |
| 155 | + } |
| 156 | + } |
| 157 | + } catch (Throwable e) { |
| 158 | + // ignore |
| 159 | + } |
| 160 | + |
| 161 | + return null; |
| 162 | + } |
110 | 163 | } |
0 commit comments