Skip to content

Commit 12b3cd9

Browse files
author
wuxw7
committed
日志中加入耗时字段,开始测试
1 parent 7afa326 commit 12b3cd9

File tree

7 files changed

+59
-35
lines changed

7 files changed

+59
-35
lines changed

CenterService/src/main/java/com/java110/center/smo/impl/CenterServiceSMOImpl.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,19 @@ public String service(String reqJson, Map<String, String> headers) throws SMOExc
133133
//添加耗时
134134
//DataFlowFactory.addCostTime(dataFlow, "service", "业务处理总耗时", dataFlow.getStartDate(), dataFlow.getEndDate());
135135

136-
//这里保存耗时,以及日志
137-
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestHeaders(),dataFlow.getReqJson().toJSONString()),
138-
LogAgent.createLogMessage(dataFlow.getResponseHeaders(),dataFlow.getResJson().toJSONString()));
136+
139137

140138
//保存耗时
141139
//saveCostTimeLogMessage(dataFlow);
142140
//处理返回报文鉴权
143141
AuthenticationFactory.putSign(dataFlow, responseJson);
142+
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestHeaders(),dataFlow.getReqJson().toJSONString()),
143+
LogAgent.createLogMessage(dataFlow.getResponseHeaders(),dataFlow.getResJson().toJSONString()),endDate.getTime()-dataFlow.getStartDate().getTime());
144144

145145
}
146146
resJson = encrypt(responseJson.toJSONString(),headers);
147-
/*LogAgent.sendLog(dataFlow.reBuilder(dataFlow.getTransactionId(),
148-
LogAgent.LOG_TYPE_C,DataTransactionFactory.isSuccessBusiness(responseJson)?
149-
LogAgent.LOG_STATUS_S:LogAgent.LOG_STATUS_F,
150-
dataFlow.getRequestURL(),responseJson.toJSONString(),headers));*/
147+
//这里保存耗时,以及日志
148+
151149
return resJson;
152150

153151
}
@@ -216,7 +214,7 @@ public String serviceTransfer(String reqJson, Map<String, String> headers) throw
216214

217215
//这里保存耗时,以及日志
218216
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestHeaders(),dataFlow.getReqJson().toJSONString()),
219-
LogAgent.createLogMessage(dataFlow.getResponseHeaders(),dataFlow.getResJson().toJSONString()));
217+
LogAgent.createLogMessage(dataFlow.getResponseHeaders(),dataFlow.getResJson().toJSONString()),endDate.getTime()-dataFlow.getStartDate().getTime());
220218

221219
//保存耗时
222220
saveCostTimeLogMessage(dataFlow);
@@ -581,13 +579,15 @@ private void invalidCompletedBusinessSystem(DataFlow dataFlow) throws Exception{
581579
for(AppRoute appRoute :dataFlow.getAppRoutes()){
582580
for(Map completedBusiness : completedBusinesses){
583581
if(completedBusiness.get("business_type_cd").equals(appRoute.getAppService().getBusinessTypeCd())){
582+
long startTime = DateUtil.getCurrentDate().getTime();
584583
//发起撤单
585584
KafkaFactory.sendKafkaMessage(appRoute.getAppService().getMessageQueueName(),"",
586585
DataFlowFactory.getDeleteInstanceTableJson(dataFlow,completedBusiness,appRoute.getAppService()).toJSONString());
587586
//saveLogMessage(DataFlowFactory.getDeleteInstanceTableJson(dataFlow,completedBusiness,appRoute.getAppService()),null);
588587

589588
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),DataFlowFactory.getDeleteInstanceTableJson(dataFlow,completedBusiness,appRoute.getAppService()).toJSONString()),
590-
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS));
589+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS),
590+
DateUtil.getCurrentDate().getTime() - startTime);
591591
}
592592
}
593593
}
@@ -725,7 +725,8 @@ public void receiveBusinessSystemNotifyMessage(String receiveJson) throws SMOExc
725725
}finally{
726726
DataFlowFactory.addCostTime(dataFlow, "receiveBusinessSystemNotifyMessage", "接受业务系统通知消息耗时", startDate);
727727
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),dataFlow.getReqJson().toJSONString()),
728-
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS));
728+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS),
729+
DateUtil.getCurrentDate().getTime() - dataFlow.getStartDate().getTime());
729730
}
730731
}
731732

@@ -869,12 +870,14 @@ private void judgeSendToInstance(DataFlow dataFlow) throws Exception{
869870
*/
870871
private void notifyBusinessSystemSuccessMessage(DataFlow dataFlow) throws Exception{
871872

873+
long startTime = DateUtil.getCurrentDate().getTime();
872874
//拼装报文通知业务系统
873875
KafkaFactory.sendKafkaMessage(
874876
DataFlowFactory.getService(dataFlow,dataFlow.getBusinesses().get(0).getServiceCode()).getMessageQueueName(),"",DataFlowFactory.getNotifyBusinessSuccessJson(dataFlow).toJSONString());
875877

876878
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),DataFlowFactory.getNotifyBusinessSuccessJson(dataFlow).toJSONString()),
877-
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS));
879+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_SUCCESS),
880+
DateUtil.getCurrentDate().getTime() - startTime);
878881
}
879882

880883
/**
@@ -883,13 +886,14 @@ private void notifyBusinessSystemSuccessMessage(DataFlow dataFlow) throws Except
883886
* @param dataFlow
884887
*/
885888
private void notifyBusinessSystemErrorMessage(DataFlow dataFlow) throws Exception{
886-
889+
long startTime = DateUtil.getCurrentDate().getTime();
887890
//拼装报文通知业务系统
888891
KafkaFactory.sendKafkaMessage(
889892
DataFlowFactory.getService(dataFlow,dataFlow.getBusinesses().get(0).getServiceCode()).getMessageQueueName(),"",
890893
DataFlowFactory.getNotifyBusinessErrorJson(dataFlow).toJSONString());
891894
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),DataFlowFactory.getNotifyBusinessErrorJson(dataFlow).toJSONString()),
892-
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_ERROR));
895+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),ResponseConstant.RESULT_CODE_ERROR),
896+
DateUtil.getCurrentDate().getTime() - startTime);
893897
}
894898

895899
/**
@@ -982,7 +986,8 @@ private void doComplateOrderAndBusiness(DataFlow dataFlow,List<Business> synchro
982986

983987
DataFlowFactory.addCostTime(dataFlow, business.getServiceCode(), "调用"+business.getServiceName()+"-doComplete耗时", businessStartDate);
984988
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),requestBusinessJson.toJSONString()),
985-
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()));
989+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()),
990+
DateUtil.getCurrentDate().getTime() - businessStartDate.getTime());
986991
}
987992

988993
}
@@ -1012,7 +1017,8 @@ private void doBusinessTableDataInfoToInstanceTable(DataFlow dataFlow, List<Busi
10121017
updateBusinessStatusCdByBId(business.getbId(),StatusConstant.STATUS_CD_COMPLETE);
10131018
DataFlowFactory.addCostTime(dataFlow, business.getServiceCode(), "调用"+business.getServiceName()+"耗时", businessStartDate);
10141019
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),requestBusinessJson.toJSONString()),
1015-
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()));
1020+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()),
1021+
DateUtil.getCurrentDate().getTime() - businessStartDate.getTime());
10161022
}
10171023

10181024
if(dataFlow.getCurrentBusiness() == null){
@@ -1043,7 +1049,8 @@ private void doDeleteBusinessSystemInstanceData(DataFlow dataFlow, List<Business
10431049
JSONObject responseJson = doRequestBusinessSystem(dataFlow, service, requestBusinessJson);
10441050
DataFlowFactory.addCostTime(dataFlow, business.getServiceCode(), "调用"+business.getServiceName()+"-撤单 耗时", businessStartDate);
10451051
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),requestBusinessJson.toJSONString()),
1046-
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()));
1052+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),responseJson.toJSONString()),
1053+
DateUtil.getCurrentDate().getTime() - businessStartDate.getTime());
10471054
}
10481055
}
10491056

@@ -1124,7 +1131,8 @@ private void doSaveDataInfoToBusinessTable(DataFlow dataFlow, List<Business> syn
11241131

11251132
DataFlowFactory.addCostTime(dataFlow, business.getServiceCode(), "调用"+business.getServiceName()+"耗时", businessStartDate);
11261133
saveLogMessage(dataFlow,LogAgent.createLogMessage(dataFlow.getRequestCurrentHeaders(),dataFlow.getRequestBusinessJson().toJSONString()),
1127-
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),dataFlow.getResponseBusinessJson().toJSONString()));
1134+
LogAgent.createLogMessage(dataFlow.getResponseCurrentHeaders(),dataFlow.getResponseBusinessJson().toJSONString()),
1135+
DateUtil.getCurrentDate().getTime()-businessStartDate.getTime());
11281136
}
11291137
}
11301138

@@ -1156,7 +1164,8 @@ private void doAsynchronousBusinesses(DataFlow dataFlow) throws BusinessExceptio
11561164
dataFlow.setResponseBusinessJson(DataTransactionFactory.createOrderResponseJson(dataFlow.getTransactionId(),
11571165
ResponseConstant.RESULT_CODE_SUCCESS, "成功"));
11581166
DataFlowFactory.addCostTime(dataFlow, "doSynchronousBusinesses", "异步调用业务系统总耗时", startDate);
1159-
saveLogMessage(dataFlow,dataFlow.getRequestBusinessJson(),dataFlow.getResponseBusinessJson());
1167+
saveLogMessage(dataFlow,dataFlow.getRequestBusinessJson(),dataFlow.getResponseBusinessJson(),
1168+
DateUtil.getCurrentDate().getTime()-startDate.getTime());
11601169
}
11611170

11621171

@@ -1173,8 +1182,8 @@ private void doAsynchronousBusinesses(DataFlow dataFlow) throws BusinessExceptio
11731182
* "body":""
11741183
* }
11751184
*/
1176-
private void saveLogMessage(DataFlow dataFlow,JSONObject requestJson,JSONObject responseJson){
1177-
LogAgent.sendLog(dataFlow,requestJson,responseJson);
1185+
private void saveLogMessage(DataFlow dataFlow,JSONObject requestJson,JSONObject responseJson,long costTime){
1186+
LogAgent.sendLog(dataFlow,requestJson,responseJson,costTime);
11781187
}
11791188

11801189
/**

java110-config/db/LogService/create_table.db

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ create table l_transaction_log(
1313
service_code varchar(50) comment '服务编码',
1414
service_name varchar(50) comment '服务名称',
1515
timestamp TIMESTAMP NOT NULL comment '日志交互时间,时间戳',
16+
cost_time int not null default 0 comment '耗时',
1617
status_cd varchar(2) not null comment '交互状态 S 成功 F 失败',
1718
month INT NOT NULL default month(CURRENT_DATE) comment '月份',
1819
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',

java110-config/src/main/resources/mapper/log/LogServiceDaoImplMapper.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<!--保存日志-->
88
<insert id="saveTransactionLog" parameterType="java.util.HashMap" >
99
insert into l_transaction_log
10-
(log_id,transaction_id,contract_id,ip,port,src_ip,src_port,app_id,user_id,service_code,service_name,timestamp,status_cd)
10+
(log_id,transaction_id,contract_id,ip,port,src_ip,src_port,app_id,user_id,service_code,service_name,cost_time,timestamp,status_cd)
1111
values
12-
(#{logId},#{transactionId},#{dataFlowId},#{ip},#{port},#{srcIp},#{srcPort},#{appId},#{userId},#{serviceCode},#{serviceName},#{timestamp},#{logStatus})
12+
(#{logId},#{transactionId},#{dataFlowId},#{ip},#{port},#{srcIp},#{srcPort},#{appId},#{userId},#{serviceCode},#{serviceName},#{costTime},#{timestamp},#{logStatus})
1313
</insert>
1414

1515
<!--保存日志(交互报文)-->

java110-core/src/main/java/com/java110/core/context/AbstractTransactionLog.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public abstract class AbstractTransactionLog implements TransactionLog {
2626

2727
private String requestMessage;
2828

29+
private long costTime;
30+
2931
private String responseMessage;
3032

3133
public String getHostIp() {
@@ -61,6 +63,10 @@ public String getResponseMessage() {
6163
return responseMessage;
6264
}
6365

66+
public long getCostTime() {
67+
return costTime;
68+
}
69+
6470
@Override
6571
public String getPort() {
6672
return port;
@@ -83,26 +89,27 @@ protected void preBuilder(String reqInfo, Map<String,String> headerAll){
8389
* 重新构建 TransactionLog 对象 主要用于服务调用方
8490
* @return
8591
*/
86-
public TransactionLog reBuilder(String requestMessage,String responseMessage,String logStatus){
92+
public TransactionLog reBuilder(String requestMessage,String responseMessage,String logStatus,long costTime){
8793

8894
this.logStatus = logStatus;
8995
this.requestMessage = requestMessage;
9096
this.responseMessage = responseMessage;
91-
97+
this.costTime = costTime;
9298
return this;
9399
}
94100

95101
/**
96102
* 重新构建 TransactionLog 对象 主要用于服务提供方
97103
* @return
98104
*/
99-
public TransactionLog reBuilder(String appId,String userId,String requestMessage,String responseMessage,String logStatus){
105+
public TransactionLog reBuilder(String appId,String userId,String requestMessage,String responseMessage,String logStatus,long costTime){
100106

101107
this.logStatus = logStatus;
102108
this.requestMessage = requestMessage;
103109
this.responseMessage = responseMessage;
104110
this.setAppId(appId);
105111
this.setUserId(userId);
112+
this.costTime = costTime;
106113
return this;
107114
}
108115

@@ -121,6 +128,7 @@ public String toString() {
121128
logMessage.put("serviceName",getServiceName());
122129
logMessage.put("timestamp",getTimestamp());
123130
logMessage.put("logStatus",getLogStatus());
131+
logMessage.put("costTime",costTime);
124132
logMessage.put("requestMessage",getRequestMessage());
125133
logMessage.put("responseMessage",getResponseMessage());
126134
return logMessage.toJSONString(logMessage,SerializerFeature.WriteNullStringAsEmpty);

java110-core/src/main/java/com/java110/core/context/TransactionLog.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public interface TransactionLog extends Serializable {
6464
*/
6565
public String getLogStatus();
6666

67+
/**
68+
* 耗时
69+
* @return
70+
*/
71+
public long getCostTime();
72+
6773
/**
6874
* 接口请求消息
6975
* @return
@@ -77,7 +83,7 @@ public interface TransactionLog extends Serializable {
7783
* 重新构建 TransactionLog 对象 主要用于服务调用方
7884
* @return
7985
*/
80-
public TransactionLog reBuilder(String appId,String userId,String requestMessage,String responseMessage,String logStatus);
86+
public TransactionLog reBuilder(String appId,String userId,String requestMessage,String responseMessage,String logStatus,long costTime);
8187

8288
/**
8389
* 重新构建 TransactionLog 对象 主要用于服务提供方
@@ -86,7 +92,7 @@ public interface TransactionLog extends Serializable {
8692
* @param logStatus 数据交互状态
8793
* @return
8894
*/
89-
public TransactionLog reBuilder(String requestMessage,String responseMessage,String logStatus);
95+
public TransactionLog reBuilder(String requestMessage,String responseMessage,String logStatus,long costTime);
9096

9197
/**
9298
* 转换成json模式

java110-logAgent/src/main/java/com/java110/log/agent/LogAgent.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public static boolean sendLog(TransactionLog transactionLog){
5353
* @param logStatus 日志状态
5454
* @return
5555
*/
56-
public static boolean sendLog(DataFlow dataFlow,String requestMessage,String responseMessage,String logStatus){
57-
return sendLog(dataFlow.reBuilder(requestMessage,responseMessage,logStatus));
56+
public static boolean sendLog(DataFlow dataFlow,String requestMessage,String responseMessage,String logStatus,long costTime){
57+
return sendLog(dataFlow.reBuilder(requestMessage,responseMessage,logStatus,costTime));
5858
}
5959

6060
/**
@@ -68,7 +68,7 @@ public static boolean sendLog(DataFlow dataFlow,String requestMessage,String res
6868
* @param responseMessage 返回数据
6969
* @return
7070
*/
71-
public static boolean sendLog(DataFlow dataFlow, JSONObject requestMessage, JSONObject responseMessage){
71+
public static boolean sendLog(DataFlow dataFlow, JSONObject requestMessage, JSONObject responseMessage,long costTime){
7272
Assert.hasKey(responseMessage,"body","返回报文不满足 日志协议要求"+responseMessage.toJSONString());
7373

7474
String body = responseMessage.getString("body");
@@ -82,21 +82,21 @@ public static boolean sendLog(DataFlow dataFlow, JSONObject requestMessage, JSON
8282
if(codeNode != null && ResponseConstant.RESULT_CODE_SUCCESS.equals(codeNode.toString())){
8383
//判断业务是否受理成功个,如果有一个业务受理失败,则认为失败
8484
if(!bodyObj.containsKey("business")){
85-
return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S);
85+
return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S,costTime);
8686
}
8787

8888
if(bodyObj.get("business") instanceof JSONObject){
8989
JSONObject businessObj = bodyObj.getJSONObject("business");
9090
if(businessObj.containsKey("response")&&
9191
ResponseConstant.RESULT_CODE_SUCCESS.equals(businessObj.getJSONObject("response").getString("code"))){
92-
return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S);
92+
return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S,costTime);
9393
}
9494
}
9595

9696
if(bodyObj.get("business") instanceof JSONArray){
9797
JSONArray businessArrays = bodyObj.getJSONArray("business");
9898
if(businessArrays == null || businessArrays.size() == 0){
99-
return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S);
99+
return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S,costTime);
100100
}
101101

102102
logStatus = LOG_STATUS_S;
@@ -116,7 +116,7 @@ public static boolean sendLog(DataFlow dataFlow, JSONObject requestMessage, JSON
116116
if(ResponseConstant.RESULT_CODE_SUCCESS.equals(body)){
117117
logStatus = LOG_STATUS_S;
118118
}
119-
return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),logStatus);
119+
return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),logStatus,costTime);
120120
}
121121

122122

java110-logAgent/src/test/java/com/java110/AppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void shouldAnswerWithTrue()
2626
@Test
2727
public void testDataFlow(){
2828
TransactionLog transactionLog = new DataFlow(new Date(),"0000");
29-
transactionLog.reBuilder("234","234324","F");
29+
transactionLog.reBuilder("234","234324","F",0);
3030

3131
System.out.println(transactionLog.toString());
3232
}

0 commit comments

Comments
 (0)