聊聊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 publicinterfaceWebFluxTagsProvider{ /** *Providestagstobeassociatedwithmetricsforthegiven{@codeexchange}. *@paramexchangetheexchange *@paramexthecurrentexception(maybe{@codenull}) *@returntagstoassociatewithmetricsfortherequestandresponseexchange */ IterablehttpRequestTags(ServerWebExchangeexchange,Throwableex); }
WebFluxTagsProvider接口定义了httpRequestTags方法
DefaultWebFluxTagsProvider
spring-boot-actuator-2.1.5.RELEASE-sources.jar!/org/springframework/boot/actuate/metrics/web/reactive/server/DefaultWebFluxTagsProvider.java
publicclassDefaultWebFluxTagsProviderimplementsWebFluxTagsProvider{ @Override publicIterablehttpRequestTags(ServerWebExchangeexchange, Throwableexception){ returnArrays.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
publicfinalclassWebFluxTags{ privatestaticfinalTagURI_NOT_FOUND=Tag.of("uri","NOT_FOUND"); privatestaticfinalTagURI_REDIRECTION=Tag.of("uri","REDIRECTION"); privatestaticfinalTagURI_ROOT=Tag.of("uri","root"); privatestaticfinalTagURI_UNKNOWN=Tag.of("uri","UNKNOWN"); privatestaticfinalTagEXCEPTION_NONE=Tag.of("exception","None"); privatestaticfinalTagOUTCOME_UNKNOWN=Tag.of("outcome","UNKNOWN"); privatestaticfinalTagOUTCOME_INFORMATIONAL=Tag.of("outcome","INFORMATIONAL"); privatestaticfinalTagOUTCOME_SUCCESS=Tag.of("outcome","SUCCESS"); privatestaticfinalTagOUTCOME_REDIRECTION=Tag.of("outcome","REDIRECTION"); privatestaticfinalTagOUTCOME_CLIENT_ERROR=Tag.of("outcome","CLIENT_ERROR"); privatestaticfinalTagOUTCOME_SERVER_ERROR=Tag.of("outcome","SERVER_ERROR"); privateWebFluxTags(){ } publicstaticTagmethod(ServerWebExchangeexchange){ returnTag.of("method",exchange.getRequest().getMethodValue()); } publicstaticTagstatus(ServerWebExchangeexchange){ HttpStatusstatus=exchange.getResponse().getStatusCode(); if(status==null){ status=HttpStatus.OK; } returnTag.of("status",String.valueOf(status.value())); } publicstaticTaguri(ServerWebExchangeexchange){ PathPatternpathPattern=exchange .getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); if(pathPattern!=null){ returnTag.of("uri",pathPattern.getPatternString()); } HttpStatusstatus=exchange.getResponse().getStatusCode(); if(status!=null){ if(status.is3xxRedirection()){ returnURI_REDIRECTION; } if(status==HttpStatus.NOT_FOUND){ returnURI_NOT_FOUND; } } Stringpath=getPathInfo(exchange); if(path.isEmpty()){ returnURI_ROOT; } returnURI_UNKNOWN; } privatestaticStringgetPathInfo(ServerWebExchangeexchange){ Stringpath=exchange.getRequest().getPath().value(); Stringuri=StringUtils.hasText(path)?path:"/"; returnuri.replaceAll("//+","/").replaceAll("/$",""); } publicstaticTagexception(Throwableexception){ if(exception!=null){ StringsimpleName=exception.getClass().getSimpleName(); returnTag.of("exception",StringUtils.hasText(simpleName)?simpleName :exception.getClass().getName()); } returnEXCEPTION_NONE; } publicstaticTagoutcome(ServerWebExchangeexchange){ HttpStatusstatus=exchange.getResponse().getStatusCode(); if(status!=null){ if(status.is1xxInformational()){ returnOUTCOME_INFORMATIONAL; } if(status.is2xxSuccessful()){ returnOUTCOME_SUCCESS; } if(status.is3xxRedirection()){ returnOUTCOME_REDIRECTION; } if(status.is4xxClientError()){ returnOUTCOME_CLIENT_ERROR; } returnOUTCOME_SERVER_ERROR; } returnOUTCOME_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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。