|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.spring.gateway.v2_0; |
| 7 | + |
| 8 | +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; |
| 9 | + |
| 10 | +import io.opentelemetry.api.common.AttributeKey; |
| 11 | +import io.opentelemetry.api.internal.StringUtils; |
| 12 | +import io.opentelemetry.api.trace.Span; |
| 13 | +import io.opentelemetry.context.Context; |
| 14 | +import io.opentelemetry.instrumentation.api.instrumenter.LocalRootSpan; |
| 15 | +import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; |
| 16 | +import java.util.regex.Pattern; |
| 17 | +import org.springframework.cloud.gateway.route.Route; |
| 18 | +import org.springframework.web.server.ServerWebExchange; |
| 19 | + |
| 20 | +public final class ServerWebExchangeHelper { |
| 21 | + |
| 22 | + /** Route ID attribute key. */ |
| 23 | + private static final AttributeKey<String> ROUTE_ID_ATTRIBUTE = |
| 24 | + AttributeKey.stringKey("spring-cloud-gateway.route.id"); |
| 25 | + |
| 26 | + /** Route URI attribute key. */ |
| 27 | + private static final AttributeKey<String> ROUTE_URI_ATTRIBUTE = |
| 28 | + AttributeKey.stringKey("spring-cloud-gateway.route.uri"); |
| 29 | + |
| 30 | + /** Route order attribute key. */ |
| 31 | + private static final AttributeKey<Long> ROUTE_ORDER_ATTRIBUTE = |
| 32 | + AttributeKey.longKey("spring-cloud-gateway.route.order"); |
| 33 | + |
| 34 | + /** Route filter size attribute key. */ |
| 35 | + private static final AttributeKey<Long> ROUTE_FILTER_SIZE_ATTRIBUTE = |
| 36 | + AttributeKey.longKey("spring-cloud-gateway.route.filter.size"); |
| 37 | + |
| 38 | + private static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES; |
| 39 | + |
| 40 | + static { |
| 41 | + CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = |
| 42 | + InstrumentationConfig.get() |
| 43 | + .getBoolean( |
| 44 | + "otel.instrumentation.spring-cloud-gateway.experimental-span-attributes", false); |
| 45 | + } |
| 46 | + |
| 47 | + /* Regex for UUID */ |
| 48 | + private static final Pattern UUID_REGEX = |
| 49 | + Pattern.compile( |
| 50 | + "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); |
| 51 | + |
| 52 | + private static final String INVALID_RANDOM_ROUTE_ID = |
| 53 | + "org.springframework.util.AlternativeJdkIdGenerator@"; |
| 54 | + |
| 55 | + private ServerWebExchangeHelper() {} |
| 56 | + |
| 57 | + public static void extractAttributes(ServerWebExchange exchange, Context context) { |
| 58 | + // Record route info |
| 59 | + Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR); |
| 60 | + if (route != null && CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { |
| 61 | + Span serverSpan = LocalRootSpan.fromContextOrNull(context); |
| 62 | + if (serverSpan == null) { |
| 63 | + return; |
| 64 | + } |
| 65 | + serverSpan.setAttribute(ROUTE_ID_ATTRIBUTE, route.getId()); |
| 66 | + serverSpan.setAttribute(ROUTE_URI_ATTRIBUTE, route.getUri().toASCIIString()); |
| 67 | + serverSpan.setAttribute(ROUTE_ORDER_ATTRIBUTE, route.getOrder()); |
| 68 | + serverSpan.setAttribute(ROUTE_FILTER_SIZE_ATTRIBUTE, route.getFilters().size()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static String extractServerRoute(ServerWebExchange exchange) { |
| 73 | + Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR); |
| 74 | + if (route != null) { |
| 75 | + return convergeRouteId(route); |
| 76 | + } |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * To avoid high cardinality, we ignore random UUID generated by Spring Cloud Gateway. Spring |
| 82 | + * Cloud Gateway generate invalid random routeID, and it is fixed until 3.1.x |
| 83 | + * |
| 84 | + * @see <a |
| 85 | + * href="https://github.com/spring-cloud/spring-cloud-gateway/commit/5002fe2e0a2825ef47dd667cade37b844c276cf6"/> |
| 86 | + */ |
| 87 | + private static String convergeRouteId(Route route) { |
| 88 | + String routeId = route.getId(); |
| 89 | + if (StringUtils.isNullOrEmpty(routeId)) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + if (UUID_REGEX.matcher(routeId).matches()) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + if (routeId.startsWith(INVALID_RANDOM_ROUTE_ID)) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + return routeId; |
| 99 | + } |
| 100 | +} |
0 commit comments