From 66046506e5bbcfedf4541ef22bb6120016333a4f Mon Sep 17 00:00:00 2001 From: liubao68 Date: Mon, 21 Nov 2022 18:39:48 +0800 Subject: [PATCH] [#897]improve access log (#941) --- .../feign/AccessLogRequestInterceptor.java | 59 ------------------- .../adapters/feign/FeignConfiguration.java | 7 --- .../gateway/AccessLogGlobalFilter.java | 12 +--- ...AccessLogClientHttpRequestInterceptor.java | 6 +- .../AccessLogExchangeFilterFunction.java | 8 +-- .../adapters/webflux/AccessLogWebFilter.java | 13 +--- .../adapters/webmvc/AccessLogFilter.java | 10 ++-- spring-cloud-huawei-dependencies/pom.xml | 2 +- ...rnanceFeignBlockingLoadBalancerClient.java | 4 +- 9 files changed, 15 insertions(+), 106 deletions(-) delete mode 100644 spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/AccessLogRequestInterceptor.java diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/AccessLogRequestInterceptor.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/AccessLogRequestInterceptor.java deleted file mode 100644 index 780f92acd..000000000 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/AccessLogRequestInterceptor.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - - * Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved. - - * 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 - * - * http://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 com.huaweicloud.common.adapters.feign; - -import org.springframework.core.Ordered; - -import com.huaweicloud.common.access.AccessLogLogger; -import com.huaweicloud.common.configration.dynamic.ContextProperties; -import com.huaweicloud.common.context.InvocationContext; -import com.huaweicloud.common.context.InvocationContextHolder; - -import feign.RequestInterceptor; -import feign.RequestTemplate; - -public class AccessLogRequestInterceptor implements RequestInterceptor, Ordered { - private final ContextProperties contextProperties; - - private final AccessLogLogger accessLogLogger; - - public AccessLogRequestInterceptor(ContextProperties contextProperties, - AccessLogLogger accessLogLogger) { - this.contextProperties = contextProperties; - this.accessLogLogger = accessLogLogger; - } - - @Override - public void apply(RequestTemplate requestTemplate) { - if (!contextProperties.isEnableTraceInfo()) { - return; - } - - InvocationContext context = InvocationContextHolder.getOrCreateInvocationContext(); - accessLogLogger.log(context, "Feign start request", - requestTemplate.request().url(), - null, - requestTemplate.feignTarget().name(), - 0, 0); - } - - @Override - public int getOrder() { - return Ordered.HIGHEST_PRECEDENCE + 2; - } -} diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/FeignConfiguration.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/FeignConfiguration.java index f7aa5cb55..09182bb7b 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/FeignConfiguration.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/FeignConfiguration.java @@ -24,7 +24,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import com.huaweicloud.common.access.AccessLogLogger; import com.huaweicloud.common.configration.dynamic.ContextProperties; import feign.RequestInterceptor; @@ -49,10 +48,4 @@ public RequestInterceptor invocationContextRequestInterceptor() { public RequestInterceptor serializeContextOrderedRequestInterceptor() { return new SerializeContextOrderedRequestInterceptor(); } - - @Bean - public RequestInterceptor accessLogRequestInterceptor(ContextProperties contextProperties, - AccessLogLogger accessLogLogger) { - return new AccessLogRequestInterceptor(contextProperties, accessLogLogger); - } } diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/gateway/AccessLogGlobalFilter.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/gateway/AccessLogGlobalFilter.java index ab5cad5fb..35c258712 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/gateway/AccessLogGlobalFilter.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/gateway/AccessLogGlobalFilter.java @@ -59,18 +59,10 @@ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { assert context != null; String request = exchange.getRequest().getPath().value(); String source = exchange.getRequest().getRemoteAddress().getHostString(); - accessLogLogger.log(context, - "Gateway start request", - request, - source, - service, - 0, - 0L); - long begin = System.currentTimeMillis(); return chain.filter(exchange).doOnSuccess(v -> { accessLogLogger.log(context, - "Gateway finish request", + "Gateway", request, source, service, @@ -78,7 +70,7 @@ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { System.currentTimeMillis() - begin); }).doOnError(error -> { accessLogLogger.log(context, - "Gateway finish request(" + error.getClass().getName() + ")", + "Gateway(" + error.getClass().getName() + ")", request, source, service, diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/AccessLogClientHttpRequestInterceptor.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/AccessLogClientHttpRequestInterceptor.java index 6b5ea20fc..fe6598685 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/AccessLogClientHttpRequestInterceptor.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/AccessLogClientHttpRequestInterceptor.java @@ -52,18 +52,16 @@ public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttp InvocationContext context = InvocationContextHolder.getOrCreateInvocationContext(); String url = request.getURI().getPath(); String target = request.getURI().getHost() + ":" + request.getURI().getPort(); - accessLogLogger.log(context, "RestTemplate start request", url, - null, target, 0, 0); long begin = System.currentTimeMillis(); try { ClientHttpResponse response = execution.execute(request, body); - accessLogLogger.log(context, "RestTemplate finish request", url, + accessLogLogger.log(context, "RestTemplate", url, null, target, response.getRawStatusCode(), System.currentTimeMillis() - begin); return response; } catch (Throwable error) { - accessLogLogger.log(context, "RestTemplate finish request(" + error.getClass().getName() + ")", url, + accessLogLogger.log(context, "RestTemplate(" + error.getClass().getName() + ")", url, null, target, -1, System.currentTimeMillis() - begin); throw error; diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webclient/AccessLogExchangeFilterFunction.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webclient/AccessLogExchangeFilterFunction.java index 1c080771d..4d413d78f 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webclient/AccessLogExchangeFilterFunction.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webclient/AccessLogExchangeFilterFunction.java @@ -57,17 +57,13 @@ public Mono filter(ClientRequest request, ExchangeFunction next) (InvocationContext) request.attribute(InvocationContextHolder.ATTRIBUTE_KEY).get() : new InvocationContext(); String url = request.url().getPath(); String target = request.url().getHost() + ":" + request.url().getPort(); - accessLogLogger.log(context, - "WebClient start request", url, - null, target, 0, 0); - long begin = System.currentTimeMillis(); return next.exchange(request).doOnSuccess(response -> { - accessLogLogger.log(context, "WebClient finish request", url, + accessLogLogger.log(context, "WebClient", url, null, target, response.rawStatusCode(), System.currentTimeMillis() - begin); }).doOnError(error -> { - accessLogLogger.log(context, "WebClient finish request(" + error.getClass().getName() + ")", url, + accessLogLogger.log(context, "WebClient(" + error.getClass().getName() + ")", url, null, target, -1, System.currentTimeMillis() - begin); }); diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webflux/AccessLogWebFilter.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webflux/AccessLogWebFilter.java index 94f80b5b5..c804890b4 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webflux/AccessLogWebFilter.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webflux/AccessLogWebFilter.java @@ -57,26 +57,17 @@ public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { ? exchange.getRequest().getRemoteAddress().getHostString() : context.getContext(InvocationContext.CONTEXT_MICROSERVICE_NAME); String request = exchange.getRequest().getPath().value(); - - accessLogLogger.log(context, - "WebFlux receive request", - request, - source, - null, - 0, - 0L); - long begin = System.currentTimeMillis(); return chain.filter(exchange) .doOnSuccess(v -> accessLogLogger.log(context, - "WebFlux finish request", + "WebFlux", request, source, null, exchange.getResponse().getRawStatusCode(), System.currentTimeMillis() - begin)) .doOnError(error -> accessLogLogger.log(context, - "WebFlux finish request(" + error.getClass().getName() + ")", + "WebFlux(" + error.getClass().getName() + ")", request, source, null, diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webmvc/AccessLogFilter.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webmvc/AccessLogFilter.java index 4842f437b..10b9b66d2 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webmvc/AccessLogFilter.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webmvc/AccessLogFilter.java @@ -61,16 +61,14 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha String source = context.getContext(InvocationContext.CONTEXT_MICROSERVICE_NAME) == null ? request.getRemoteAddr() : context.getContext(InvocationContext.CONTEXT_MICROSERVICE_NAME); - accessLogLogger.log(context, "WebMVC receive request", req, source, null, 0, 0); - long begin = System.currentTimeMillis(); try { chain.doFilter(request, response); - accessLogLogger.log(context, "WebMVC finish request", - req, source, null, ((HttpServletResponse) response).getStatus(), System.currentTimeMillis() - begin); + accessLogLogger.log(context, "WebMVC", req, source, null, + ((HttpServletResponse) response).getStatus(), System.currentTimeMillis() - begin); } catch (Throwable error) { - accessLogLogger.log(context, "WebMVC finish request(" + error.getClass().getName() + ")", - req, source, null, -1, System.currentTimeMillis() - begin); + accessLogLogger.log(context, "WebMVC(" + error.getClass().getName() + ")", req, source, null, + -1, System.currentTimeMillis() - begin); throw error; } } diff --git a/spring-cloud-huawei-dependencies/pom.xml b/spring-cloud-huawei-dependencies/pom.xml index 71c5c4c5e..11e7e177f 100644 --- a/spring-cloud-huawei-dependencies/pom.xml +++ b/spring-cloud-huawei-dependencies/pom.xml @@ -33,7 +33,7 @@ Spring Cloud Huawei::Dependencies - 11.8 + 11.10 4.5.9 2.12.1 2.7 diff --git a/spring-cloud-huawei-governance/src/main/java/com/huaweicloud/governance/adapters/feign/GovernanceFeignBlockingLoadBalancerClient.java b/spring-cloud-huawei-governance/src/main/java/com/huaweicloud/governance/adapters/feign/GovernanceFeignBlockingLoadBalancerClient.java index 665d5416b..25ce99e78 100644 --- a/spring-cloud-huawei-governance/src/main/java/com/huaweicloud/governance/adapters/feign/GovernanceFeignBlockingLoadBalancerClient.java +++ b/spring-cloud-huawei-governance/src/main/java/com/huaweicloud/governance/adapters/feign/GovernanceFeignBlockingLoadBalancerClient.java @@ -149,7 +149,7 @@ public Response execute(Request request, Request.Options options) { long begin = System.currentTimeMillis(); try { Response response = decorateWithFault(request, options, originalUri, governanceRequest); - accessLogLogger.log(context, "Feign finish request", + accessLogLogger.log(context, "Feign", governanceRequest.getUri(), null, request.requestTemplate().feignTarget().name(), @@ -157,7 +157,7 @@ public Response execute(Request request, Request.Options options) { System.currentTimeMillis() - begin); return response; } catch (Throwable error) { - accessLogLogger.log(context, "Feign finish request(" + error.getClass().getName() + ")", + accessLogLogger.log(context, "Feign(" + error.getClass().getName() + ")", governanceRequest.getUri(), null, request.requestTemplate().feignTarget().name(),