spring WebClientExchangeTags 源码
springboot WebClientExchangeTags 代码
文件路径:/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTags.java
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.metrics.web.reactive.client;
import java.io.IOException;
import java.util.regex.Pattern;
import io.micrometer.core.instrument.Tag;
import org.springframework.boot.actuate.metrics.http.Outcome;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
/**
* Factory methods for creating {@link Tag Tags} related to a request-response exchange
* performed by a {@link WebClient}.
*
* @author Brian Clozel
* @author Nishant Raut
* @since 2.1.0
*/
public final class WebClientExchangeTags {
private static final String URI_TEMPLATE_ATTRIBUTE = WebClient.class.getName() + ".uriTemplate";
private static final Tag IO_ERROR = Tag.of("status", "IO_ERROR");
private static final Tag CLIENT_ERROR = Tag.of("status", "CLIENT_ERROR");
private static final Pattern PATTERN_BEFORE_PATH = Pattern.compile("^https?://[^/]+/");
private static final Tag CLIENT_NAME_NONE = Tag.of("client.name", "none");
private WebClientExchangeTags() {
}
/**
* Creates a {@code method} {@code Tag} for the {@link ClientHttpRequest#getMethod()
* method} of the given {@code request}.
* @param request the request
* @return the method tag
*/
public static Tag method(ClientRequest request) {
return Tag.of("method", request.method().name());
}
/**
* Creates a {@code uri} {@code Tag} for the URI path of the given {@code request}.
* @param request the request
* @return the uri tag
*/
public static Tag uri(ClientRequest request) {
String uri = (String) request.attribute(URI_TEMPLATE_ATTRIBUTE).orElseGet(() -> request.url().toString());
return Tag.of("uri", extractPath(uri));
}
private static String extractPath(String url) {
String path = PATTERN_BEFORE_PATH.matcher(url).replaceFirst("");
return (path.startsWith("/") ? path : "/" + path);
}
/**
* Creates a {@code status} {@code Tag} derived from the
* {@link ClientResponse#statusCode()} of the given {@code response} if available, the
* thrown exception otherwise, or considers the request as Cancelled as a last resort.
* @param response the response
* @param throwable the exception
* @return the status tag
* @since 2.3.0
*/
public static Tag status(ClientResponse response, Throwable throwable) {
if (response != null) {
return Tag.of("status", String.valueOf(response.statusCode().value()));
}
if (throwable != null) {
return (throwable instanceof IOException) ? IO_ERROR : CLIENT_ERROR;
}
return CLIENT_ERROR;
}
/**
* Create a {@code client.name} {@code Tag} derived from the
* {@link java.net.URI#getHost host} of the {@link ClientRequest#url() URL} of the
* given {@code request}.
* @param request the request
* @return the client.name tag
*/
public static Tag clientName(ClientRequest request) {
String host = request.url().getHost();
if (host == null) {
return CLIENT_NAME_NONE;
}
return Tag.of("client.name", host);
}
/**
* Creates an {@code outcome} {@code Tag} derived from the
* {@link ClientResponse#statusCode() status} of the given {@code response}.
* @param response the response
* @return the outcome tag
* @since 2.2.0
*/
public static Tag outcome(ClientResponse response) {
Outcome outcome = (response != null) ? Outcome.forStatus(response.statusCode().value()) : Outcome.UNKNOWN;
return outcome.asTag();
}
}
相关信息
相关文章
spring DefaultWebClientExchangeTagsProvider 源码
spring MetricsWebClientCustomizer 源码
spring MetricsWebClientFilterFunction 源码
0
赞
- 所属分类: 后端技术
- 本文标签: Spring Boot Spring Java
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦