Commit 89cac1a51b0f2698ad5fbb9154d326a7c9af137a

Authored by miaoguoxin
1 parent 45276d37

api监控指标

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