|
| 1 | +package datadog.trace.instrumentation.springwebflux.client; |
| 2 | + |
| 3 | +import java.lang.invoke.MethodHandle; |
| 4 | +import java.lang.invoke.MethodHandles; |
| 5 | +import java.lang.invoke.MethodType; |
| 6 | +import java.util.function.Function; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.web.reactive.function.client.ClientResponse; |
| 11 | + |
| 12 | +// Adapted from Opentelemetry |
| 13 | +public final class StatusCodes { |
| 14 | + private static final Logger LOGGER = LoggerFactory.getLogger(StatusCodes.class); |
| 15 | + public static final Function<ClientResponse, Integer> STATUS_CODE_FUNCTION = |
| 16 | + getStatusCodeFunction(); |
| 17 | + |
| 18 | + private static Function<ClientResponse, Integer> getStatusCodeFunction() { |
| 19 | + Function<ClientResponse, Integer> statusCodeFunction = getStatusCodeFunction60(); |
| 20 | + if (statusCodeFunction == null) { |
| 21 | + statusCodeFunction = getStatusCodeFunction51(); |
| 22 | + } |
| 23 | + if (statusCodeFunction == null) { |
| 24 | + statusCodeFunction = getStatusCodeFunction50(); |
| 25 | + } |
| 26 | + if (statusCodeFunction == null) { |
| 27 | + LOGGER.debug( |
| 28 | + "Unable to find a status code extractor function working for the current webflux client version. " |
| 29 | + + "Status codes will not be tagged on webflux client spans"); |
| 30 | + } |
| 31 | + return statusCodeFunction; |
| 32 | + } |
| 33 | + |
| 34 | + // in webflux 6.0, HttpStatusCode class was introduced, and statusCode() was changed to return |
| 35 | + // HttpStatusCode instead of HttpStatus |
| 36 | + private static Function<ClientResponse, Integer> getStatusCodeFunction60() { |
| 37 | + MethodHandle statusCode; |
| 38 | + MethodHandle value; |
| 39 | + try { |
| 40 | + Class<?> httpStatusCodeClass = |
| 41 | + Class.forName( |
| 42 | + "org.springframework.http.HttpStatusCode", false, StatusCodes.class.getClassLoader()); |
| 43 | + statusCode = |
| 44 | + MethodHandles.publicLookup() |
| 45 | + .findVirtual( |
| 46 | + ClientResponse.class, "statusCode", MethodType.methodType(httpStatusCodeClass)); |
| 47 | + value = |
| 48 | + MethodHandles.publicLookup() |
| 49 | + .findVirtual(httpStatusCodeClass, "value", MethodType.methodType(int.class)); |
| 50 | + } catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException e) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + return response -> { |
| 55 | + try { |
| 56 | + Object httpStatusCode = statusCode.invoke(response); |
| 57 | + return (int) value.invoke(httpStatusCode); |
| 58 | + } catch (Throwable e) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + // in webflux 5.1, rawStatusCode() was introduced to retrieve the exact status code |
| 65 | + // note: rawStatusCode() was deprecated in 6.0 |
| 66 | + private static Function<ClientResponse, Integer> getStatusCodeFunction51() { |
| 67 | + MethodHandle rawStatusCode; |
| 68 | + try { |
| 69 | + rawStatusCode = |
| 70 | + MethodHandles.publicLookup() |
| 71 | + .findVirtual(ClientResponse.class, "rawStatusCode", MethodType.methodType(int.class)); |
| 72 | + } catch (IllegalAccessException | NoSuchMethodException e) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return response -> { |
| 77 | + try { |
| 78 | + return (int) rawStatusCode.invoke(response); |
| 79 | + } catch (Throwable e) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + // in webflux 5.0, statusCode() returns HttpStatus, which only represents standard status codes |
| 86 | + // (there's no way to capture arbitrary status codes) |
| 87 | + private static Function<ClientResponse, Integer> getStatusCodeFunction50() { |
| 88 | + MethodHandle statusCode; |
| 89 | + MethodHandle value; |
| 90 | + try { |
| 91 | + statusCode = |
| 92 | + MethodHandles.publicLookup() |
| 93 | + .findVirtual( |
| 94 | + ClientResponse.class, "statusCode", MethodType.methodType(HttpStatus.class)); |
| 95 | + value = |
| 96 | + MethodHandles.publicLookup() |
| 97 | + .findVirtual(HttpStatus.class, "value", MethodType.methodType(int.class)); |
| 98 | + } catch (IllegalAccessException | NoSuchMethodException e) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + return response -> { |
| 103 | + try { |
| 104 | + Object httpStatusCode = statusCode.invoke(response); |
| 105 | + return (int) value.invoke(httpStatusCode); |
| 106 | + } catch (Throwable e) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + private StatusCodes() {} |
| 113 | +} |
0 commit comments