微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

聊聊spring boot的WebFluxTagsProvider的使用

这篇文章主要介绍了聊聊spring boot的WebFluxTagsProvider的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文主要研究一下webflux的WebFluxTagsProviderWebFluxTagsProviderspring-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 httpRequestTags(ServerWebExchange exchange, Throwable ex); }WebFluxTagsProvider接口定义了httpRequestTags方法DefaultWebFluxTagsProviderspring-boot-actuator-2.1.5.RELEASE-sources.jar!/org/springframework/boot/actuate/metrics/web/reactive/server/DefaultWebFluxTagsProvider.javapublic class DefaultWebFluxTagsProvider implements WebFluxTagsProvider { @Override public Iterable 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这几个tagWebFluxTagsspring-boot-actuator-2.1.5.RELEASE-sources.jar!/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.javapublic 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常量docWebFluxTagsProvider

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐