|
| 1 | +# Spring Gateway Plugin |
| 2 | + |
| 3 | +Spring Gateway Plugin only support Spring Gateway 2.x, 3.x and 4.x. It has capabilities to create entry spans for |
| 4 | +incoming calls, continue tracing context propagation in Spring Gateway and create exit spans for outgoing calls. |
| 5 | + |
| 6 | +About the filter extension of Gateway, it provides automatically support as much as possible, including GlobalFilter and GatewayFilter |
| 7 | +support. However, the filter extension by using `chain.filter(exchange).then(...)` is not able to transparently. |
| 8 | + |
| 9 | +## Supported Auto-Instrument Filters |
| 10 | + |
| 11 | +```java |
| 12 | +@Component |
| 13 | +public class Filter1 implements GlobalFilter, Ordered { |
| 14 | + |
| 15 | + private static final Logger log = LoggerFactory.getLogger(Filter1.class); |
| 16 | + @Override |
| 17 | + public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
| 18 | + // Available trace context |
| 19 | + // For log framework integration(trace context log output) and manual trace context usage. |
| 20 | + String traceId = TraceContext.traceId(); |
| 21 | + log.info("available traceId: {}", traceId); |
| 22 | + |
| 23 | + String segmentId = TraceContext.segmentId(); |
| 24 | + log.info("available segmentId: {}", segmentId); |
| 25 | + |
| 26 | + int spanId = TraceContext.spanId(); |
| 27 | + log.info("available spanId: {}", spanId); |
| 28 | + |
| 29 | + return chain.filter(exchange); |
| 30 | + } |
| 31 | + @Override |
| 32 | + public int getOrder() { |
| 33 | + return -100; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | +```java |
| 38 | +@Component |
| 39 | +public class GatewayFilter1 implements GatewayFilter { |
| 40 | + |
| 41 | + private static final Logger log = LoggerFactory.getLogger(GatewayFilter1.class); |
| 42 | + @Override |
| 43 | + public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
| 44 | + // Available trace context |
| 45 | + log.info("gatewayFilter1 running"); |
| 46 | + return chain.filter(exchange); |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## Unsupported Auto-Instrument Filters |
| 52 | +Typically, in the following case, you need to read via [Webflux Tracing Assistant APIs](../Application-toolkit-webflux.md) to get the trace context. |
| 53 | + |
| 54 | +```java |
| 55 | +@Component |
| 56 | +public class UnsupportedFilter implements GlobalFilter, Ordered { |
| 57 | + private static final Logger log = LoggerFactory.getLogger(UnsupportedFilter.class); |
| 58 | + @Override |
| 59 | + public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
| 60 | + String traceId = TraceContext.traceId(); |
| 61 | + // Trace ID is available as it's in the GlobalFilter. |
| 62 | + log.info("available traceId: {}", traceId); |
| 63 | + |
| 64 | + String segmentId = TraceContext.segmentId(); |
| 65 | + // Segment ID is available as it's in the GlobalFilter. |
| 66 | + log.info("available segmentId: {}", segmentId); |
| 67 | + |
| 68 | + int spanId = TraceContext.spanId(); |
| 69 | + // Span ID is available as it's in the GlobalFilter. |
| 70 | + log.info("available spanId: {}", spanId); |
| 71 | + |
| 72 | + return chain.filter(exchange).then(Mono.fromRunnable(() -> { |
| 73 | + // Trace ID/context is not available, N/A in the all following logs. |
| 74 | + // The trace context is not available in the then-closure. |
| 75 | + // Only webflux assistant API can get the trace context. |
| 76 | + String traceId2 = WebFluxSkyWalkingTraceContext.traceId(exchange); |
| 77 | + // Trace ID is not available, N/A in the logs. |
| 78 | + log.info("unavailable in then-closure, available traceId: {} through webflux assistant API", traceId2); |
| 79 | + |
| 80 | + String segmentId2 = WebFluxSkyWalkingTraceContext.segmentId(exchange); |
| 81 | + // Segment ID is not available, N/A in the logs. |
| 82 | + log.info("unavailable in then-closure, available segmentId: {} through webflux assistant API", segmentId2); |
| 83 | + |
| 84 | + int spanId2 = WebFluxSkyWalkingTraceContext.spanId(exchange); |
| 85 | + // Span ID is not available, N/A in the logs. |
| 86 | + log.info("unavailable in then-closure, available spanId: {} through webflux assistant API", spanId2); |
| 87 | + })); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public int getOrder() { |
| 92 | + return 10; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +``` |
0 commit comments