Skip to content

Commit

Permalink
[#897]improve access log (#941) (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Nov 21, 2022
1 parent cdffd6e commit f9b745b
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 106 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,18 @@ public Mono<Void> 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,
exchange.getResponse().getRawStatusCode(),
System.currentTimeMillis() - begin);
}).doOnError(error -> {
accessLogLogger.log(context,
"Gateway finish request(" + error.getClass().getName() + ")",
"Gateway(" + error.getClass().getName() + ")",
request,
source,
service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,13 @@ public Mono<ClientResponse> 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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,17 @@ public Mono<Void> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion spring-cloud-huawei-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<version>1.11.0-2020.0.x-SNAPSHOT</version>
<name>Spring Cloud Huawei::Dependencies</name>
<properties>
<feign-core.version>11.8</feign-core.version>
<feign-core.version>11.10</feign-core.version>
<httpclient.version>4.5.9</httpclient.version>
<jackson.version>2.12.1</jackson.version>
<commons-io.version>2.7</commons-io.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ 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(),
response.status(),
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(),
Expand Down

0 comments on commit f9b745b

Please sign in to comment.