Commit 89cac1a51b0f2698ad5fbb9154d326a7c9af137a

Authored by miaoguoxin
1 parent 45276d37

api监控指标

gateway-core/src/main/java/com/diligrp/xtrade/core/filters/global/CacheRequestBodyGlobalFilter.java
1 1 package com.diligrp.xtrade.core.filters.global;
2 2  
3 3 import com.diligrp.xtrade.core.common.constant.GatewayConst;
  4 +import org.apache.commons.lang3.StringUtils;
4 5 import org.springframework.cloud.gateway.filter.GatewayFilterChain;
5 6 import org.springframework.cloud.gateway.filter.GlobalFilter;
6 7 import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
... ... @@ -28,7 +29,7 @@ import java.util.List;
28 29 /**
29 30 * @Auther: miaoguoxin
30 31 * @Date: 2020/4/13 11:49
31   - * @Description: 缓存请求body的过滤器,参考
  32 + * @Description: 缓存请求body的过滤器, 参考
32 33 * {@link ModifyRequestBodyGatewayFilterFactory}
33 34 */
34 35 @Component
... ... @@ -38,6 +39,10 @@ public class CacheRequestBodyGlobalFilter implements GlobalFilter, Ordered {
38 39  
39 40 @Override
40 41 public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  42 + String contentType = exchange.getRequest().getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
  43 + if (StringUtils.isNotBlank(contentType) && !contentType.startsWith("multipart")) {
  44 + return chain.filter(exchange);
  45 + }
41 46 ServerRequest serverRequest = ServerRequest.create(exchange,
42 47 messageReaders);
43 48 Mono<String> modifiedBody = serverRequest.bodyToMono(String.class)
... ... @@ -63,7 +68,7 @@ public class CacheRequestBodyGlobalFilter implements GlobalFilter, Ordered {
63 68 }
64 69  
65 70 private ServerHttpRequestDecorator decorate(ServerWebExchange exchange, HttpHeaders headers,
66   - CachedBodyOutputMessage outputMessage) {
  71 + CachedBodyOutputMessage outputMessage) {
67 72 return new ServerHttpRequestDecorator(exchange.getRequest()) {
68 73 @Override
69 74 public HttpHeaders getHeaders() {
... ...
gateway-core/src/main/java/com/diligrp/xtrade/core/support/ApiMetricsInfo.java
... ... @@ -10,10 +10,14 @@ import org.slf4j.Logger;
10 10 import org.slf4j.LoggerFactory;
11 11 import org.springframework.cloud.gateway.route.Route;
12 12 import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
  13 +import org.springframework.http.HttpHeaders;
13 14 import org.springframework.web.server.ServerWebExchange;
14 15  
15 16 import java.net.URI;
16 17 import java.time.LocalDateTime;
  18 +import java.util.LinkedHashMap;
  19 +import java.util.List;
  20 +import java.util.Map;
17 21  
18 22 /**
19 23 * @Auther: miaoguoxin
... ... @@ -30,6 +34,8 @@ public class ApiMetricsInfo {
30 34 private String serviceId;
31 35 /**目标地址*/
32 36 private String url;
  37 + /**请求头*/
  38 + private String requestHeaders;
33 39 /**请求体*/
34 40 private String requestBody;
35 41 /**异常信息*/
... ... @@ -44,6 +50,7 @@ public class ApiMetricsInfo {
44 50 String message,
45 51 String serviceId,
46 52 String url,
  53 + String requestHeaders,
47 54 String requestBody,
48 55 Throwable throwable,
49 56 Long executeTime) {
... ... @@ -51,6 +58,7 @@ public class ApiMetricsInfo {
51 58 this.message = message;
52 59 this.serviceId = serviceId;
53 60 this.url = url;
  61 + this.requestHeaders = requestHeaders;
54 62 this.requestBody = requestBody;
55 63 this.throwable = throwable;
56 64 this.executeTime = executeTime;
... ... @@ -72,6 +80,7 @@ public class ApiMetricsInfo {
72 80 message.getMessage(),
73 81 uri != null ? uri.toString() : "unknown",
74 82 route != null ? route.getId() : "unknown",
  83 + getRequestHeaderJson(exchange.getRequest().getHeaders()),
75 84 requestBody,
76 85 throwable,
77 86 time);
... ... @@ -83,6 +92,12 @@ public class ApiMetricsInfo {
83 92 }
84 93 }
85 94  
  95 + private static String getRequestHeaderJson(HttpHeaders headers){
  96 + Map<String, List<String>> headerMap = new LinkedHashMap<>();
  97 + headerMap.put(HttpHeaders.CONTENT_TYPE,headers.get(HttpHeaders.CONTENT_TYPE));
  98 + return JsonUtils.toJsonString(headerMap);
  99 + }
  100 +
86 101 public Integer getCode() {
87 102 return code;
88 103 }
... ... @@ -103,6 +118,10 @@ public class ApiMetricsInfo {
103 118 return requestBody;
104 119 }
105 120  
  121 + public String getRequestHeaders() {
  122 + return requestHeaders;
  123 + }
  124 +
106 125 public Throwable getThrowable() {
107 126 return throwable;
108 127 }
... ...