溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在spring boot中使用WebFluxTagsProvider

發(fā)布時(shí)間:2021-05-17 17:35:55 來(lái)源:億速云 閱讀:206 作者:Leah 欄目:編程語(yǔ)言

如何在spring boot中使用WebFluxTagsProvider?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

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實(shí)現(xiàn)了WebFluxTagsProvider接口,它返回了method、uri、exception、status、outcome這幾個(gè)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常量

springboot是什么

springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過(guò)程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡(jiǎn)化配置文件。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI