mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
新增监控日志
This commit is contained in:
@@ -7,9 +7,21 @@ import lombok.Data;
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class ErrorDefinition {
|
public class ErrorDefinition {
|
||||||
|
/**
|
||||||
|
* 接口名
|
||||||
|
*/
|
||||||
private String name;
|
private String name;
|
||||||
|
/**
|
||||||
|
* 版本号
|
||||||
|
*/
|
||||||
private String version;
|
private String version;
|
||||||
|
/**
|
||||||
|
* 服务名
|
||||||
|
*/
|
||||||
private String serviceId;
|
private String serviceId;
|
||||||
|
/**
|
||||||
|
* 错误内容
|
||||||
|
*/
|
||||||
private String errorMsg;
|
private String errorMsg;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -23,7 +23,12 @@ public class DefaultServiceErrorManager implements ServiceErrorManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveError(ErrorDefinition errorDefinition) {
|
public void saveBizError(ErrorDefinition errorDefinition) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveUnknownError(ErrorDefinition errorDefinition) {
|
||||||
boolean hasCapacity = store.size() < ApiConfig.getInstance().getStoreErrorCapacity();
|
boolean hasCapacity = store.size() < ApiConfig.getInstance().getStoreErrorCapacity();
|
||||||
// 这里还可以做其它事情,比如错误量到达一定数目后,自动发送邮件/微信给开发人员,方便及时获取异常情况
|
// 这里还可以做其它事情,比如错误量到达一定数目后,自动发送邮件/微信给开发人员,方便及时获取异常情况
|
||||||
String id = this.buildId(errorDefinition);
|
String id = this.buildId(errorDefinition);
|
||||||
|
@@ -11,10 +11,16 @@ import java.util.Collection;
|
|||||||
public interface ServiceErrorManager {
|
public interface ServiceErrorManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存错误信息
|
* 保存业务错误,一般由开发人员自己throw的异常
|
||||||
* @param errorDefinition
|
* @param errorDefinition
|
||||||
*/
|
*/
|
||||||
void saveError(ErrorDefinition errorDefinition);
|
void saveBizError(ErrorDefinition errorDefinition);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存未知的错误信息
|
||||||
|
* @param errorDefinition
|
||||||
|
*/
|
||||||
|
void saveUnknownError(ErrorDefinition errorDefinition);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清除日志
|
* 清除日志
|
||||||
|
@@ -78,11 +78,12 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
|||||||
jsonObjectService.put(GATEWAY_MSG_NAME, SUCCESS_META.getError().getMsg());
|
jsonObjectService.put(GATEWAY_MSG_NAME, SUCCESS_META.getError().getMsg());
|
||||||
} else if (responseStatus == SopConstants.BIZ_ERROR_STATUS) {
|
} else if (responseStatus == SopConstants.BIZ_ERROR_STATUS) {
|
||||||
// 如果是业务出错
|
// 如果是业务出错
|
||||||
|
this.storeError(request, ErrorType.BIZ);
|
||||||
jsonObjectService = JSON.parseObject(serviceResult);
|
jsonObjectService = JSON.parseObject(serviceResult);
|
||||||
jsonObjectService.put(GATEWAY_CODE_NAME, ISP_BIZ_ERROR.getCode());
|
jsonObjectService.put(GATEWAY_CODE_NAME, ISP_BIZ_ERROR.getCode());
|
||||||
jsonObjectService.put(GATEWAY_MSG_NAME, ISP_BIZ_ERROR.getError().getMsg());
|
jsonObjectService.put(GATEWAY_MSG_NAME, ISP_BIZ_ERROR.getError().getMsg());
|
||||||
} else {
|
} else {
|
||||||
this.storeError(request);
|
this.storeError(request, ErrorType.UNKNOWN);
|
||||||
// 微服务端有可能返回500错误
|
// 微服务端有可能返回500错误
|
||||||
// {"path":"/book/getBook3","error":"Internal Server Error","message":"id不能为空","timestamp":"2019-02-13T07:41:00.495+0000","status":500}
|
// {"path":"/book/getBook3","error":"Internal Server Error","message":"id不能为空","timestamp":"2019-02-13T07:41:00.495+0000","status":500}
|
||||||
jsonObjectService = new JSONObject();
|
jsonObjectService = new JSONObject();
|
||||||
@@ -97,13 +98,18 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
|||||||
*
|
*
|
||||||
* @param request
|
* @param request
|
||||||
*/
|
*/
|
||||||
protected void storeError(T request) {
|
protected void storeError(T request, ErrorType errorType) {
|
||||||
ApiInfo apiInfo = this.getApiInfo(request);
|
ApiInfo apiInfo = this.getApiInfo(request);
|
||||||
String errorMsg = this.getResponseErrorMessage(request);
|
String errorMsg = this.getResponseErrorMessage(request);
|
||||||
ErrorDefinition errorDefinition = new ErrorDefinition();
|
ErrorDefinition errorDefinition = new ErrorDefinition();
|
||||||
BeanUtils.copyProperties(apiInfo, errorDefinition);
|
BeanUtils.copyProperties(apiInfo, errorDefinition);
|
||||||
errorDefinition.setErrorMsg(errorMsg);
|
errorDefinition.setErrorMsg(errorMsg);
|
||||||
ApiConfig.getInstance().getServiceErrorManager().saveError(errorDefinition);
|
if (errorType == ErrorType.UNKNOWN) {
|
||||||
|
ApiConfig.getInstance().getServiceErrorManager().saveUnknownError(errorDefinition);
|
||||||
|
}
|
||||||
|
if (errorType == ErrorType.BIZ) {
|
||||||
|
ApiConfig.getInstance().getServiceErrorManager().saveBizError(errorDefinition);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -223,4 +229,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
|||||||
private BaseRouteDefinition baseRouteDefinition;
|
private BaseRouteDefinition baseRouteDefinition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ErrorType {
|
||||||
|
UNKNOWN, BIZ
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user