聊聊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
1 2 3 4 5 6 7 8 9 10 11 12 | @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
1 2 3 4 5 6 7 8 9 10 11 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
Java中HashMap和Hashtable及HashSet的区别
以下是对Java中HashMap和Hashtable及HashSet的区别进行了详细的分析介绍,需要的朋友可以过来参考下2013-09-09Java中的位运算符号解读(&、|、^、~、<<、>>、>>>)
这篇文章主要介绍了Java中的位运算符号(&、|、^、~、<<、>>、>>>),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-08-08如何使用@Value和@PropertySource注入外部资源
这篇文章主要介绍了如何使用@Value和@PropertySource注入外部资源的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06
最新评论