Example usage for org.springframework.web.reactive HandlerMapping BEST_MATCHING_HANDLER_ATTRIBUTE

List of usage examples for org.springframework.web.reactive HandlerMapping BEST_MATCHING_HANDLER_ATTRIBUTE

Introduction

In this page you can find the example usage for org.springframework.web.reactive HandlerMapping BEST_MATCHING_HANDLER_ATTRIBUTE.

Prototype

String BEST_MATCHING_HANDLER_ATTRIBUTE

To view the source code for org.springframework.web.reactive HandlerMapping BEST_MATCHING_HANDLER_ATTRIBUTE.

Click Source Link

Document

Name of the ServerWebExchange#getAttributes() attribute that contains the mapped handler for the best matching pattern.

Usage

From source file:org.springframework.cloud.sleuth.instrument.web.TraceWebFilter.java

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    if (tracer().currentSpan() != null) {
        // clear any previous trace
        tracer().withSpanInScope(null);// w w w  .ja v a2 s  . c  o  m
    }
    String uri = exchange.getRequest().getPath().pathWithinApplication().value();
    if (log.isDebugEnabled()) {
        log.debug("Received a request to uri [" + uri + "]");
    }
    Span spanFromAttribute = getSpanFromAttribute(exchange);
    final String CONTEXT_ERROR = "sleuth.webfilter.context.error";
    return chain.filter(exchange).compose(f -> f.then(Mono.subscriberContext())
            .onErrorResume(t -> Mono.subscriberContext().map(c -> c.put(CONTEXT_ERROR, t))).flatMap(c -> {
                // reactivate span from context
                Span span = spanFromContext(c);
                Mono<Void> continuation;
                Throwable t = null;
                if (c.hasKey(CONTEXT_ERROR)) {
                    t = c.get(CONTEXT_ERROR);
                    continuation = Mono.error(t);
                } else {
                    continuation = Mono.empty();
                }
                String httpRoute = null;
                Object attribute = exchange.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
                if (attribute instanceof HandlerMethod) {
                    HandlerMethod handlerMethod = (HandlerMethod) attribute;
                    addClassMethodTag(handlerMethod, span);
                    addClassNameTag(handlerMethod, span);
                    Object pattern = exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
                    httpRoute = pattern != null ? pattern.toString() : "";
                }
                addResponseTagsForSpanWithoutParent(exchange, exchange.getResponse(), span);
                DecoratedServerHttpResponse delegate = new DecoratedServerHttpResponse(exchange.getResponse(),
                        exchange.getRequest().getMethodValue(), httpRoute);
                handler().handleSend(delegate, t, span);
                if (log.isDebugEnabled()) {
                    log.debug("Handled send of " + span);
                }
                return continuation;
            }).subscriberContext(c -> {
                Span span;
                if (c.hasKey(Span.class)) {
                    Span parent = c.get(Span.class);
                    span = tracer().nextSpan(TraceContextOrSamplingFlags.create(parent.context())).start();
                    if (log.isDebugEnabled()) {
                        log.debug("Found span in reactor context" + span);
                    }
                } else {
                    if (spanFromAttribute != null) {
                        span = spanFromAttribute;
                        if (log.isDebugEnabled()) {
                            log.debug("Found span in attribute " + span);
                        }
                    } else {
                        span = handler().handleReceive(extractor(), exchange.getRequest().getHeaders(),
                                exchange.getRequest());
                        if (log.isDebugEnabled()) {
                            log.debug("Handled receive of span " + span);
                        }
                    }
                    exchange.getAttributes().put(TRACE_REQUEST_ATTR, span);
                }
                return c.put(Span.class, span);
            }));
}