32
32
import com .tencent .cos .xml .exception .CosXmlClientException ;
33
33
import com .tencent .cos .xml .exception .CosXmlServiceException ;
34
34
import com .tencent .cos .xml .model .CosXmlRequest ;
35
+ import com .tencent .cos .xml .model .object .BasePutObjectRequest ;
36
+ import com .tencent .cos .xml .model .object .GetObjectBytesRequest ;
37
+ import com .tencent .cos .xml .model .object .GetObjectRequest ;
35
38
import com .tencent .cos .xml .model .object .ObjectRequest ;
36
39
import com .tencent .cos .xml .transfer .TransferTaskMetrics ;
37
40
import com .tencent .cos .xml .utils .ThrowableUtils ;
@@ -231,21 +234,54 @@ public void reportError(String source, Exception e) {
231
234
* @param request request
232
235
*/
233
236
public void reportRequestSuccess (CosXmlRequest request ) {
234
- reportRequestSuccess (request , null );
237
+ reportRequestSuccess (request , false );
238
+ }
239
+ public void reportRequestSuccess (CosXmlRequest request , boolean internal ) {
240
+ Map <String , String > extra = null ;
241
+ if (request instanceof BasePutObjectRequest ){
242
+ extra = Collections .singletonMap ("request_name" , "UploadTask" );
243
+ } else if (request instanceof GetObjectRequest || request instanceof GetObjectBytesRequest ){
244
+ extra = Collections .singletonMap ("request_name" , "DownloadTask" );
245
+ } else if ("CopyObjectRequest" .equalsIgnoreCase (request .getClass ().getSimpleName ())){
246
+ extra = Collections .singletonMap ("request_name" , "CopyTask" );
247
+ }
248
+ reportRequestSuccess (request , extra , internal );
235
249
}
236
250
237
251
/**
238
252
* 上报base_service事件 ClientException
239
253
*/
240
254
public CosXmlClientException reportRequestClientException (CosXmlRequest request , QCloudClientException clientException ) {
241
- return reportClientException (request , clientException , null );
255
+ return reportRequestClientException (request , clientException , false );
256
+ }
257
+ public CosXmlClientException reportRequestClientException (CosXmlRequest request , QCloudClientException clientException , boolean internal ) {
258
+ Map <String , String > extra = null ;
259
+ if (request instanceof BasePutObjectRequest ){
260
+ extra = Collections .singletonMap ("request_name" , "UploadTask" );
261
+ } else if (request instanceof GetObjectRequest || request instanceof GetObjectBytesRequest ){
262
+ extra = Collections .singletonMap ("request_name" , "DownloadTask" );
263
+ } else if ("CopyObjectRequest" .equalsIgnoreCase (request .getClass ().getSimpleName ())){
264
+ extra = Collections .singletonMap ("request_name" , "CopyTask" );
265
+ }
266
+ return reportClientException (request , clientException , extra , internal );
242
267
}
243
268
244
269
/**
245
270
* 上报base_service事件 ServiceException
246
271
*/
247
272
public CosXmlServiceException reportRequestServiceException (CosXmlRequest request , QCloudServiceException serviceException ) {
248
- return reportServiceException (request , serviceException , null );
273
+ return reportRequestServiceException (request , serviceException , false );
274
+ }
275
+ public CosXmlServiceException reportRequestServiceException (CosXmlRequest request , QCloudServiceException serviceException , boolean internal ) {
276
+ Map <String , String > extra = null ;
277
+ if (request instanceof BasePutObjectRequest ){
278
+ extra = Collections .singletonMap ("request_name" , "UploadTask" );
279
+ } else if (request instanceof GetObjectRequest || request instanceof GetObjectBytesRequest ){
280
+ extra = Collections .singletonMap ("request_name" , "DownloadTask" );
281
+ } else if ("CopyObjectRequest" .equalsIgnoreCase (request .getClass ().getSimpleName ())){
282
+ extra = Collections .singletonMap ("request_name" , "CopyTask" );
283
+ }
284
+ return reportServiceException (request , serviceException , extra , internal );
249
285
}
250
286
251
287
/**
@@ -256,23 +292,23 @@ public CosXmlServiceException reportRequestServiceException(CosXmlRequest reques
256
292
public void reportUploadTaskSuccess (CosXmlRequest request ) {
257
293
// 只需要一个 PutObjectRequest 壳,带上 HttpTaskMetrics 信息
258
294
reportRequestSuccess (request ,
259
- Collections .singletonMap ("request_name" , "UploadTask" ));
295
+ Collections .singletonMap ("request_name" , "UploadTask" ), false );
260
296
}
261
297
262
298
/**
263
299
* 上报cos_upload事件 ClientException
264
300
*/
265
301
public void reportUploadTaskClientException (CosXmlRequest request , QCloudClientException clientException ) {
266
302
reportClientException (request , clientException ,
267
- createTransferExtra ("UploadTask" , request ));
303
+ createTransferExtra ("UploadTask" , request ), false );
268
304
}
269
305
270
306
/**
271
307
* 上报cos_upload事件 ServiceException
272
308
*/
273
309
public void reportUploadTaskServiceException (CosXmlRequest request , QCloudServiceException serviceException ) {
274
310
reportServiceException (request , serviceException ,
275
- createTransferExtra ("UploadTask" , request ));
311
+ createTransferExtra ("UploadTask" , request ), false );
276
312
}
277
313
278
314
/**
@@ -283,23 +319,23 @@ public void reportUploadTaskServiceException(CosXmlRequest request, QCloudServic
283
319
public void reportDownloadTaskSuccess (CosXmlRequest request ) {
284
320
// 只需要一个 GetObjectRequest 壳,带上 HttpTaskMetrics 信息
285
321
reportRequestSuccess (request ,
286
- Collections .singletonMap ("request_name" , "DownloadTask" ));
322
+ Collections .singletonMap ("request_name" , "DownloadTask" ), false );
287
323
}
288
324
289
325
/**
290
326
* 上报cos_download事件 ClientException
291
327
*/
292
328
public void reportDownloadTaskClientException (CosXmlRequest request , QCloudClientException clientException ) {
293
329
reportClientException (request , clientException ,
294
- createTransferExtra ("DownloadTask" , request ));
330
+ createTransferExtra ("DownloadTask" , request ), false );
295
331
}
296
332
297
333
/**
298
334
* 上报cos_download事件 ServiceException
299
335
*/
300
336
public void reportDownloadTaskServiceException (CosXmlRequest request , QCloudServiceException serviceException ) {
301
337
reportServiceException (request , serviceException ,
302
- createTransferExtra ("DownloadTask" , request ));
338
+ createTransferExtra ("DownloadTask" , request ), false );
303
339
}
304
340
305
341
/**
@@ -310,38 +346,36 @@ public void reportDownloadTaskServiceException(CosXmlRequest request, QCloudServ
310
346
public void reportCopyTaskSuccess (CosXmlRequest request ) {
311
347
// 只需要一个 CopyObjectRequest 壳,带上 HttpTaskMetrics 信息
312
348
reportRequestSuccess (request ,
313
- Collections .singletonMap ("request_name" , "CopyTask" ));
349
+ Collections .singletonMap ("request_name" , "CopyTask" ), false );
314
350
}
315
351
316
352
/**
317
353
* 上报cos_copy事件 ClientException
318
354
*/
319
355
public void reportCopyTaskClientException (CosXmlRequest request , CosXmlClientException clientException ) {
320
356
reportClientException (request , clientException ,
321
- createTransferExtra ("CopyTask" , request ));
357
+ createTransferExtra ("CopyTask" , request ), false );
322
358
}
323
359
324
360
/**
325
361
* 上报cos_copy事件 ServiceException
326
362
*/
327
363
public void reportCopyTaskServiceException (CosXmlRequest request , CosXmlServiceException serviceException ) {
328
364
reportServiceException (request , serviceException ,
329
- createTransferExtra ("CopyTask" , request ));
365
+ createTransferExtra ("CopyTask" , request ), false );
330
366
}
331
367
332
368
/**
333
369
* 单个请求,整体任务 成功
334
370
*/
335
- private void reportRequestSuccess (CosXmlRequest request , @ Nullable Map <String , String > extra ) {
371
+ private void reportRequestSuccess (CosXmlRequest request , @ Nullable Map <String , String > extra , boolean internal ) {
336
372
try {
337
373
HttpTaskMetrics taskMetrics = request .getMetrics ();
338
374
339
375
// 添加 Request 参数
340
376
Map <String , String > params = parseCosXmlRequestParams (request );
341
377
// 添加基础参数
342
378
params .putAll (getCommonParams ());
343
- // 添加性能参数
344
- params .putAll (parseHttpTaskMetricsParams (taskMetrics ));
345
379
// 添加服务名称
346
380
if (extra == null || !extra .containsKey ("request_name" )) {
347
381
params .put ("request_name" , request .getClass ().getSimpleName ());
@@ -352,7 +386,11 @@ private void reportRequestSuccess(CosXmlRequest request, @Nullable Map<String, S
352
386
if (extra != null ) {
353
387
params .putAll (extra );
354
388
}
355
- QCloudTrackService .getInstance ().report (EVENT_CODE_QCLOUD_TRACK_COS_SDK , params );
389
+ // 添加性能参数
390
+ params .putAll (parseHttpTaskMetricsParams (taskMetrics , params .get ("request_name" )));
391
+ if (!internal ) {
392
+ QCloudTrackService .getInstance ().report (EVENT_CODE_QCLOUD_TRACK_COS_SDK , params );
393
+ }
356
394
} catch (Exception e ) {
357
395
e .printStackTrace ();
358
396
}
@@ -361,7 +399,7 @@ private void reportRequestSuccess(CosXmlRequest request, @Nullable Map<String, S
361
399
/**
362
400
* 上报客户端异常 单个请求,整体任务
363
401
*/
364
- private CosXmlClientException reportClientException (CosXmlRequest request , QCloudClientException clientException , @ Nullable Map <String , String > extra ) {
402
+ private CosXmlClientException reportClientException (CosXmlRequest request , QCloudClientException clientException , @ Nullable Map <String , String > extra , boolean internal ) {
365
403
ReturnClientException returnClientException = getClientExceptionParams (clientException );
366
404
try {
367
405
if (isReport (returnClientException .exception )) {
@@ -373,8 +411,6 @@ private CosXmlClientException reportClientException(CosXmlRequest request, QClou
373
411
params .putAll (getCommonParams ());
374
412
// 添加错误信息
375
413
params .putAll (returnClientException .params );
376
- // 添加性能参数
377
- params .putAll (parseHttpTaskMetricsParams (taskMetrics ));
378
414
// 添加服务名称
379
415
if (extra == null || !extra .containsKey ("request_name" )) {
380
416
params .put ("request_name" , request .getClass ().getSimpleName ());
@@ -386,7 +422,11 @@ private CosXmlClientException reportClientException(CosXmlRequest request, QClou
386
422
if (extra != null ) {
387
423
params .putAll (extra );
388
424
}
389
- QCloudTrackService .getInstance ().report (EVENT_CODE_QCLOUD_TRACK_COS_SDK , params );
425
+ // 添加性能参数
426
+ params .putAll (parseHttpTaskMetricsParams (taskMetrics , params .get ("request_name" )));
427
+ if (!internal ) {
428
+ QCloudTrackService .getInstance ().report (EVENT_CODE_QCLOUD_TRACK_COS_SDK , params );
429
+ }
390
430
}
391
431
} catch (Exception e ) {
392
432
e .printStackTrace ();
@@ -397,7 +437,7 @@ private CosXmlClientException reportClientException(CosXmlRequest request, QClou
397
437
/**
398
438
* 上报服务端异常 单个请求,整体任务
399
439
*/
400
- private CosXmlServiceException reportServiceException (CosXmlRequest request , QCloudServiceException serviceException , @ Nullable Map <String , String > extra ) {
440
+ private CosXmlServiceException reportServiceException (CosXmlRequest request , QCloudServiceException serviceException , @ Nullable Map <String , String > extra , boolean internal ) {
401
441
ReturnServiceException returnServiceException = getServiceExceptionParams (serviceException );
402
442
try {
403
443
if (isReport (returnServiceException .exception )) {
@@ -407,8 +447,6 @@ private CosXmlServiceException reportServiceException(CosXmlRequest request, QCl
407
447
params .putAll (getCommonParams ());
408
448
// 添加错误信息
409
449
params .putAll (returnServiceException .params );
410
- // 添加性能参数
411
- params .putAll (parseHttpTaskMetricsParams (request .getMetrics ()));
412
450
// 添加服务名称
413
451
if (extra == null || !extra .containsKey ("request_name" )) {
414
452
params .put ("request_name" , request .getClass ().getSimpleName ());
@@ -420,7 +458,11 @@ private CosXmlServiceException reportServiceException(CosXmlRequest request, QCl
420
458
if (extra != null ) {
421
459
params .putAll (extra );
422
460
}
423
- QCloudTrackService .getInstance ().report (EVENT_CODE_QCLOUD_TRACK_COS_SDK , params );
461
+ // 添加性能参数
462
+ params .putAll (parseHttpTaskMetricsParams (request .getMetrics (), params .get ("request_name" )));
463
+ if (!internal ) {
464
+ QCloudTrackService .getInstance ().report (EVENT_CODE_QCLOUD_TRACK_COS_SDK , params );
465
+ }
424
466
}
425
467
} catch (Exception e ){
426
468
e .printStackTrace ();
@@ -431,7 +473,7 @@ private CosXmlServiceException reportServiceException(CosXmlRequest request, QCl
431
473
/**
432
474
* 获取http性能数据字段
433
475
*/
434
- private Map <String , String > parseHttpTaskMetricsParams (@ Nullable HttpTaskMetrics taskMetrics ) {
476
+ private Map <String , String > parseHttpTaskMetricsParams (@ Nullable HttpTaskMetrics taskMetrics , String requestName ) {
435
477
Map <String , String > params = new HashMap <>();
436
478
437
479
if (taskMetrics == null ) {
@@ -449,13 +491,23 @@ private Map<String, String> parseHttpTaskMetricsParams(@Nullable HttpTaskMetrics
449
491
params .put ("http_write_header" , String .valueOf (taskMetrics .writeRequestHeaderTookTime ()));
450
492
params .put ("http_write_body" , String .valueOf (taskMetrics .writeRequestBodyTookTime ()));
451
493
params .put ("http_full_time" , String .valueOf (taskMetrics .fullTaskTookTime ()));
452
- params .put ("http_size" , String .valueOf (taskMetrics .requestBodyByteCount () + taskMetrics .responseBodyByteCount ()));
494
+ if ("UploadTask" .equalsIgnoreCase (requestName ) || "CopyTask" .equalsIgnoreCase (requestName )){
495
+ params .put ("http_size" , String .valueOf (taskMetrics .requestBodyByteCount ()));
496
+ // 速度 每秒传输的数据大小 kb为单位
497
+ params .put ("http_speed" , String .valueOf ((taskMetrics .requestBodyByteCount ()/1024d ) / taskMetrics .httpTaskFullTime ()));
498
+ } else if ("DownloadTask" .equalsIgnoreCase (requestName )){
499
+ params .put ("http_size" , String .valueOf (taskMetrics .responseBodyByteCount ()));
500
+ // 速度 每秒传输的数据大小 kb为单位
501
+ params .put ("http_speed" , String .valueOf ((taskMetrics .responseBodyByteCount ()/1024d ) / taskMetrics .httpTaskFullTime ()));
502
+ } else {
503
+ params .put ("http_size" , String .valueOf (taskMetrics .requestBodyByteCount () + taskMetrics .responseBodyByteCount ()));
504
+ // 速度 每秒传输的数据大小 kb为单位
505
+ params .put ("http_speed" , String .valueOf (((taskMetrics .requestBodyByteCount () + taskMetrics .responseBodyByteCount ())/1024d ) / taskMetrics .httpTaskFullTime ()));
506
+ }
453
507
params .put ("http_retry_times" , String .valueOf (taskMetrics .getRetryCount ()));
454
508
params .put ("http_domain" , taskMetrics .getDomainName () != null ? taskMetrics .getDomainName () : "null" );
455
509
params .put ("http_connect_ip" , taskMetrics .getConnectAddress () != null ? taskMetrics .getConnectAddress ().getHostAddress () : "null" );
456
510
params .put ("http_dns_ips" , taskMetrics .getRemoteAddress () != null ? taskMetrics .getRemoteAddress ().toString () : "null" );
457
- // 速度 每秒传输的数据大小 kb为单位
458
- params .put ("http_speed" , String .valueOf (((taskMetrics .requestBodyByteCount () + taskMetrics .responseBodyByteCount ())/1024d ) / taskMetrics .httpTaskFullTime ()));
459
511
return params ;
460
512
}
461
513
0 commit comments