|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.spring.webflux.client; |
| 7 | + |
| 8 | +import static java.util.Collections.emptyList; |
| 9 | + |
| 10 | +import io.opentelemetry.instrumentation.api.instrumenter.http.CapturedHttpHeaders; |
| 11 | +import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor; |
| 12 | +import java.lang.invoke.MethodHandle; |
| 13 | +import java.lang.invoke.MethodHandles; |
| 14 | +import java.lang.invoke.MethodType; |
| 15 | +import java.util.List; |
| 16 | +import javax.annotation.Nullable; |
| 17 | +import org.springframework.web.reactive.function.client.ClientRequest; |
| 18 | +import org.springframework.web.reactive.function.client.ClientResponse; |
| 19 | + |
| 20 | +final class SpringWebfluxHttpAttributesExtractor |
| 21 | + extends HttpClientAttributesExtractor<ClientRequest, ClientResponse> { |
| 22 | + |
| 23 | + private static final MethodHandle RAW_STATUS_CODE = findRawStatusCode(); |
| 24 | + |
| 25 | + // rawStatusCode() method was introduced in webflux 5.1 |
| 26 | + // prior to this method, the best we can get is HttpStatus enum, which only covers standard status |
| 27 | + // codes (see usage below) |
| 28 | + private static MethodHandle findRawStatusCode() { |
| 29 | + try { |
| 30 | + return MethodHandles.publicLookup() |
| 31 | + .findVirtual(ClientResponse.class, "rawStatusCode", MethodType.methodType(int.class)); |
| 32 | + } catch (IllegalAccessException | NoSuchMethodException e) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + SpringWebfluxHttpAttributesExtractor(CapturedHttpHeaders capturedHttpHeaders) { |
| 38 | + super(capturedHttpHeaders); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected String url(ClientRequest request) { |
| 43 | + return request.url().toString(); |
| 44 | + } |
| 45 | + |
| 46 | + @Nullable |
| 47 | + @Override |
| 48 | + protected String flavor(ClientRequest request, @Nullable ClientResponse response) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected String method(ClientRequest request) { |
| 54 | + return request.method().name(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected List<String> requestHeader(ClientRequest request, String name) { |
| 59 | + return request.headers().getOrDefault(name, emptyList()); |
| 60 | + } |
| 61 | + |
| 62 | + @Nullable |
| 63 | + @Override |
| 64 | + protected Long requestContentLength(ClientRequest request, @Nullable ClientResponse response) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + @Nullable |
| 69 | + @Override |
| 70 | + protected Long requestContentLengthUncompressed( |
| 71 | + ClientRequest request, @Nullable ClientResponse response) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected Integer statusCode(ClientRequest request, ClientResponse response) { |
| 77 | + if (RAW_STATUS_CODE != null) { |
| 78 | + // rawStatusCode() method was introduced in webflux 5.1 |
| 79 | + try { |
| 80 | + return (int) RAW_STATUS_CODE.invokeExact(response); |
| 81 | + } catch (Throwable ignored) { |
| 82 | + // Ignore |
| 83 | + } |
| 84 | + } |
| 85 | + // prior to webflux 5.1, the best we can get is HttpStatus enum, which only covers standard |
| 86 | + // status codes |
| 87 | + return response.statusCode().value(); |
| 88 | + } |
| 89 | + |
| 90 | + @Nullable |
| 91 | + @Override |
| 92 | + protected Long responseContentLength(ClientRequest request, ClientResponse response) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + @Nullable |
| 97 | + @Override |
| 98 | + protected Long responseContentLengthUncompressed(ClientRequest request, ClientResponse response) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + protected List<String> responseHeader( |
| 104 | + ClientRequest request, ClientResponse response, String name) { |
| 105 | + return response.headers().header(name); |
| 106 | + } |
| 107 | +} |
0 commit comments