六合彩最高极限

聊聊spring boot的WebFluxTagsProvider

本文主要研究一下webflux的WebFluxTagsProvider

WebFluxTagsProvider

spring-boot-actuator-2.1.5.RELEASE-sources.jar!/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsProvider.java

@FunctionalInterface
public interface WebFluxTagsProvider {

    /**
     * Provides tags to be associated with metrics for the given {@code exchange}.
     * @param exchange the exchange
     * @param ex the current exception (may be {@code null})
     * @return tags to associate with metrics for the request and response exchange
     */
    Iterable<Tag> httpRequestTags(ServerWebExchange exchange, Throwable ex);

}
  • WebFluxTagsProvider接口定义了httpRequestTags方法

DefaultWebFluxTagsProvider

spring-boot-actuator-2.1.5.RELEASE-sources.jar!/org/springframework/boot/actuate/metrics/web/reactive/server/DefaultWebFluxTagsProvider.java

public class DefaultWebFluxTagsProvider implements WebFluxTagsProvider {

    @Override
    public Iterable<Tag> httpRequestTags(ServerWebExchange exchange,
            Throwable exception) {
        return Arrays.asList(WebFluxTags.method(exchange), WebFluxTags.uri(exchange),
                WebFluxTags.exception(exception), WebFluxTags.status(exchange),
                WebFluxTags.outcome(exchange));
    }

}
  • DefaultWebFluxTagsProvider实现了WebFluxTagsProvider接口,它返回了method、uri、exception、status、outcome这几个tag

WebFluxTags

spring-boot-actuator-2.1.5.RELEASE-sources.jar!/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java

public final class WebFluxTags {

    private static final Tag URI_NOT_FOUND = Tag.of("uri", "NOT_FOUND");

    private static final Tag URI_REDIRECTION = Tag.of("uri", "REDIRECTION");

    private static final Tag URI_ROOT = Tag.of("uri", "root");

    private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN");

    private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");

    private static final Tag OUTCOME_UNKNOWN = Tag.of("outcome", "UNKNOWN");

    private static final Tag OUTCOME_INFORMATIONAL = Tag.of("outcome", "INFORMATIONAL");

    private static final Tag OUTCOME_SUCCESS = Tag.of("outcome", "SUCCESS");

    private static final Tag OUTCOME_REDIRECTION = Tag.of("outcome", "REDIRECTION");

    private static final Tag OUTCOME_CLIENT_ERROR = Tag.of("outcome", "CLIENT_ERROR");

    private static final Tag OUTCOME_SERVER_ERROR = Tag.of("outcome", "SERVER_ERROR");

    private WebFluxTags() {
    }

    public static Tag method(ServerWebExchange exchange) {
        return Tag.of("method", exchange.getRequest().getMethodValue());
    }

    public static Tag status(ServerWebExchange exchange) {
        HttpStatus status = exchange.getResponse().getStatusCode();
        if (status == null) {
            status = HttpStatus.OK;
        }
        return Tag.of("status", String.valueOf(status.value()));
    }

    public static Tag uri(ServerWebExchange exchange) {
        PathPattern pathPattern = exchange
                .getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        if (pathPattern != null) {
            return Tag.of("uri", pathPattern.getPatternString());
        }
        HttpStatus status = exchange.getResponse().getStatusCode();
        if (status != null) {
            if (status.is3xxRedirection()) {
                return URI_REDIRECTION;
            }
            if (status == HttpStatus.NOT_FOUND) {
                return URI_NOT_FOUND;
            }
        }
        String path = getPathInfo(exchange);
        if (path.isEmpty()) {
            return URI_ROOT;
        }
        return URI_UNKNOWN;
    }

    private static String getPathInfo(ServerWebExchange exchange) {
        String path = exchange.getRequest().getPath().value();
        String uri = StringUtils.hasText(path) ? path : "/";
        return uri.replaceAll("//+", "/").replaceAll("/$", "");
    }

    public static Tag exception(Throwable exception) {
        if (exception != null) {
            String simpleName = exception.getClass().getSimpleName();
            return Tag.of("exception", StringUtils.hasText(simpleName) ? simpleName
                    : exception.getClass().getName());
        }
        return EXCEPTION_NONE;
    }

    public static Tag outcome(ServerWebExchange exchange) {
        HttpStatus status = exchange.getResponse().getStatusCode();
        if (status != null) {
            if (status.is1xxInformational()) {
                return OUTCOME_INFORMATIONAL;
            }
            if (status.is2xxSuccessful()) {
                return OUTCOME_SUCCESS;
            }
            if (status.is3xxRedirection()) {
                return OUTCOME_REDIRECTION;
            }
            if (status.is4xxClientError()) {
                return OUTCOME_CLIENT_ERROR;
            }
            return OUTCOME_SERVER_ERROR;
        }
        return OUTCOME_UNKNOWN;
    }

}
  • WebFluxTags定义了URI_NOT_FOUND、URI_REDIRECTION、URI_ROOT、URI_UNKNOWN、EXCEPTION_NONE、OUTCOME_UNKNOWN、OUTCOME_INFORMATIONAL、OUTCOME_SUCCESS、OUTCOME_REDIRECTION、OUTCOME_CLIENT_ERROR、OUTCOME_SERVER_ERROR这些Tag常量

小结

WebFluxTagsProvider接口定义了httpRequestTags方法;DefaultWebFluxTagsProvider实现了WebFluxTagsProvider接口,它返回了method、uri、exception、status、outcome这几个tag;WebFluxTags定义了URI_NOT_FOUND、URI_REDIRECTION、URI_ROOT、URI_UNKNOWN、EXCEPTION_NONE、OUTCOME_UNKNOWN、OUTCOME_INFORMATIONAL、OUTCOME_SUCCESS、OUTCOME_REDIRECTION、OUTCOME_CLIENT_ERROR、OUTCOME_SERVER_ERROR这些Tag常量

doc

  • WebFluxTagsProvider

原文 

https://segmentfault.com/a/1190000019725854

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版?#25945;?#20986;异议,本站将立即删除。如果您对文章转载有任?#25105;?#38382;请告之我们,以便我们及?#26412;?#27491;。

PS?#21644;?#33616;一个微信公众号: askHarries 或者qq?#28023;?74807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高?#38405;堋?#20998;布式、微服务架构的原理,JVM?#38405;?#20248;化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog? » 聊聊spring boot的WebFluxTagsProvider

赞 (0)
分享到:更多 ()

评论 0

  • 邮箱 (必填)
  • 网址
六合彩最高极限