经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
聊聊spring boot的WebFluxTagsProvider的使用
来源:jb51  时间:2019/7/12 8:47:52  对本文有异议


本文主要研究一下webflux的WebFluxTagsProvider

WebFluxTagsProvider

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

  1. @FunctionalInterface
  2. public interface WebFluxTagsProvider {
  3.  
  4. /**
  5. * Provides tags to be associated with metrics for the given {@code exchange}.
  6. * @param exchange the exchange
  7. * @param ex the current exception (may be {@code null})
  8. * @return tags to associate with metrics for the request and response exchange
  9. */
  10. Iterable<Tag> httpRequestTags(ServerWebExchange exchange, Throwable ex);
  11.  
  12. }
  13.  

WebFluxTagsProvider接口定义了httpRequestTags方法

DefaultWebFluxTagsProvider

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

  1. public class DefaultWebFluxTagsProvider implements WebFluxTagsProvider {
  2.  
  3. @Override
  4. public Iterable<Tag> httpRequestTags(ServerWebExchange exchange,
  5. Throwable exception) {
  6. return Arrays.asList(WebFluxTags.method(exchange), WebFluxTags.uri(exchange),
  7. WebFluxTags.exception(exception), WebFluxTags.status(exchange),
  8. WebFluxTags.outcome(exchange));
  9. }
  10.  
  11. }
  12.  

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. public final class WebFluxTags {
  2.  
  3. private static final Tag URI_NOT_FOUND = Tag.of("uri", "NOT_FOUND");
  4.  
  5. private static final Tag URI_REDIRECTION = Tag.of("uri", "REDIRECTION");
  6.  
  7. private static final Tag URI_ROOT = Tag.of("uri", "root");
  8.  
  9. private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN");
  10.  
  11. private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");
  12.  
  13. private static final Tag OUTCOME_UNKNOWN = Tag.of("outcome", "UNKNOWN");
  14.  
  15. private static final Tag OUTCOME_INFORMATIONAL = Tag.of("outcome", "INFORMATIONAL");
  16.  
  17. private static final Tag OUTCOME_SUCCESS = Tag.of("outcome", "SUCCESS");
  18.  
  19. private static final Tag OUTCOME_REDIRECTION = Tag.of("outcome", "REDIRECTION");
  20.  
  21. private static final Tag OUTCOME_CLIENT_ERROR = Tag.of("outcome", "CLIENT_ERROR");
  22.  
  23. private static final Tag OUTCOME_SERVER_ERROR = Tag.of("outcome", "SERVER_ERROR");
  24.  
  25. private WebFluxTags() {
  26. }
  27.  
  28. public static Tag method(ServerWebExchange exchange) {
  29. return Tag.of("method", exchange.getRequest().getMethodValue());
  30. }
  31.  
  32. public static Tag status(ServerWebExchange exchange) {
  33. HttpStatus status = exchange.getResponse().getStatusCode();
  34. if (status == null) {
  35. status = HttpStatus.OK;
  36. }
  37. return Tag.of("status", String.valueOf(status.value()));
  38. }
  39.  
  40. public static Tag uri(ServerWebExchange exchange) {
  41. PathPattern pathPattern = exchange
  42. .getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
  43. if (pathPattern != null) {
  44. return Tag.of("uri", pathPattern.getPatternString());
  45. }
  46. HttpStatus status = exchange.getResponse().getStatusCode();
  47. if (status != null) {
  48. if (status.is3xxRedirection()) {
  49. return URI_REDIRECTION;
  50. }
  51. if (status == HttpStatus.NOT_FOUND) {
  52. return URI_NOT_FOUND;
  53. }
  54. }
  55. String path = getPathInfo(exchange);
  56. if (path.isEmpty()) {
  57. return URI_ROOT;
  58. }
  59. return URI_UNKNOWN;
  60. }
  61.  
  62. private static String getPathInfo(ServerWebExchange exchange) {
  63. String path = exchange.getRequest().getPath().value();
  64. String uri = StringUtils.hasText(path) ? path : "/";
  65. return uri.replaceAll("//+", "/").replaceAll("/$", "");
  66. }
  67.  
  68. public static Tag exception(Throwable exception) {
  69. if (exception != null) {
  70. String simpleName = exception.getClass().getSimpleName();
  71. return Tag.of("exception", StringUtils.hasText(simpleName) ? simpleName
  72. : exception.getClass().getName());
  73. }
  74. return EXCEPTION_NONE;
  75. }
  76.  
  77. public static Tag outcome(ServerWebExchange exchange) {
  78. HttpStatus status = exchange.getResponse().getStatusCode();
  79. if (status != null) {
  80. if (status.is1xxInformational()) {
  81. return OUTCOME_INFORMATIONAL;
  82. }
  83. if (status.is2xxSuccessful()) {
  84. return OUTCOME_SUCCESS;
  85. }
  86. if (status.is3xxRedirection()) {
  87. return OUTCOME_REDIRECTION;
  88. }
  89. if (status.is4xxClientError()) {
  90. return OUTCOME_CLIENT_ERROR;
  91. }
  92. return OUTCOME_SERVER_ERROR;
  93. }
  94. return OUTCOME_UNKNOWN;
  95. }
  96.  
  97. }
  98.  

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号