|
7 | 7 |
|
8 | 8 | import static java.util.Collections.emptyList;
|
9 | 9 |
|
| 10 | +import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientExperimentalAttributesGetter; |
10 | 11 | import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
|
11 | 12 | import java.util.List;
|
| 13 | +import java.util.Map; |
| 14 | +import java.util.regex.Pattern; |
12 | 15 | import javax.annotation.Nullable;
|
13 | 16 | import org.springframework.web.reactive.function.client.ClientRequest;
|
14 | 17 | import org.springframework.web.reactive.function.client.ClientResponse;
|
| 18 | +import org.springframework.web.reactive.function.client.WebClient; |
15 | 19 |
|
16 | 20 | /**
|
17 | 21 | * This class is internal and is hence not for public use. Its APIs are unstable and can change at
|
18 | 22 | * any time.
|
19 | 23 | */
|
20 | 24 | public enum WebClientHttpAttributesGetter
|
21 |
| - implements HttpClientAttributesGetter<ClientRequest, ClientResponse> { |
| 25 | + implements HttpClientExperimentalAttributesGetter<ClientRequest, ClientResponse> { |
22 | 26 | INSTANCE;
|
23 | 27 |
|
| 28 | + |
| 29 | + private static final String URI_TEMPLATE_ATTRIBUTE = WebClient.class.getName() + ".uriTemplate"; |
| 30 | + private static final Pattern PATTERN_BEFORE_PATH = Pattern.compile("^https?://[^/]+/"); |
| 31 | + |
| 32 | + @Nullable |
| 33 | + @Override |
| 34 | + public String getUrlTemplate(ClientRequest clientRequest) { |
| 35 | + Map<String, Object> attributes = clientRequest.attributes(); |
| 36 | + Object value = attributes.get(URI_TEMPLATE_ATTRIBUTE); |
| 37 | + if (value instanceof String) { |
| 38 | + String uriTemplate = (String) value; |
| 39 | + String path = PATTERN_BEFORE_PATH.matcher(uriTemplate).replaceFirst(""); |
| 40 | + return path.startsWith("/") ? path : "/" + path; |
| 41 | + } |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
24 | 45 | @Override
|
25 | 46 | public String getUrlFull(ClientRequest request) {
|
26 | 47 | return request.url().toString();
|
@@ -71,4 +92,6 @@ public String getErrorType(
|
71 | 92 | }
|
72 | 93 | return null;
|
73 | 94 | }
|
| 95 | + |
| 96 | + |
74 | 97 | }
|
0 commit comments