Created
November 1, 2018 00:37
-
-
Save fitzoh/4c61621d9b97fed8bc89631890ccd6e2 to your computer and use it in GitHub Desktop.
spring cloud gateway span tagger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cardinalhealth.connectgateway; | |
import brave.Span; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | |
import org.springframework.cloud.gateway.filter.GlobalFilter; | |
import org.springframework.cloud.gateway.route.Route; | |
import org.springframework.cloud.sleuth.instrument.web.TraceWebFilter; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.server.ServerWebExchange; | |
import reactor.core.publisher.Mono; | |
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; | |
@Component | |
public class RouteIdTaggingGlobalFilter implements GlobalFilter { | |
/** | |
* This is protected in {@link org.springframework.cloud.sleuth.instrument.web.TraceWebFilter} :( | |
*/ | |
protected static final String TRACE_REQUEST_ATTR = TraceWebFilter.class.getName() | |
+ ".TRACE"; | |
private static final Logger log = LoggerFactory.getLogger(RouteIdTaggingGlobalFilter.class); | |
@Override | |
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | |
Object spanObj = exchange.getAttributes().get(TRACE_REQUEST_ATTR); | |
if (!(spanObj instanceof Span)) { | |
log.info("expected span in exchange attributes, got {}", spanObj); | |
return chain.filter(exchange); | |
} | |
Span span = (Span) spanObj; | |
Object routeObj = exchange.getAttributes().get(GATEWAY_ROUTE_ATTR); | |
if (!(routeObj instanceof Route)) { | |
log.info("expected route in exchange attributes, got {}", routeObj); | |
return chain.filter(exchange); | |
} | |
Route route = (Route) routeObj; | |
span.tag("route-id", route.getId()); | |
return chain.filter(exchange); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment