@@ -465,7 +465,8 @@ class SlsMonitor {
465465 responses . push ( newP50 )
466466 }
467467
468- async describeCCMInstanceDatas ( id , instances , startTime , endTime ) {
468+ async describeCCMInstanceDatas ( id , instances , startTime , endTime , i , limit ) {
469+
469470 const client = new TencentCloudClient ( this . credentials , {
470471 host : 'monitor.tencentcloudapi.com' ,
471472 path : '/'
@@ -479,7 +480,20 @@ class SlsMonitor {
479480 EndTime : endTime ,
480481 TypeId : 'SCF'
481482 }
482- return client . doCloudApiRequest ( req )
483+
484+ const timeCost = 1000
485+ let sleep = false
486+ if ( ! ( ( i + 1 ) % limit ) ) {
487+ sleep = true
488+ }
489+ return new Promise ( function ( resolve , rejecte ) {
490+ if ( ! sleep ) {
491+ return resolve ( client . doCloudApiRequest ( req ) )
492+ }
493+ setTimeout ( function ( ) {
494+ resolve ( client . doCloudApiRequest ( req ) )
495+ } , timeCost )
496+ } ) ;
483497 }
484498
485499 async describeAttributes ( offset , limit ) {
@@ -498,7 +512,7 @@ class SlsMonitor {
498512 }
499513
500514 async getCustomMetrics ( region , announceInstance , rangeTime , period ) {
501- const apiQPSLimit = 80
515+ const apiQPSLimit = 100
502516 const metricsRule = [
503517 / ^ ( G E T | P O S T | D E L | D E L E T E | P U T | O P T I O N S | H E A D ) _ ( [ a - z A - Z 0 - 9 ] + ) _ l a t e n c y $ / i,
504518 / ^ ( G E T | P O S T | D E L | D E L E T E | P U T | O P T I O N S | H E A D ) _ ( [ a - z A - Z 0 - 9 ] + ) _ ( \d + ) $ / i,
@@ -538,45 +552,36 @@ class SlsMonitor {
538552 let results
539553 let firstRequestFlag = true
540554 const attributes = await this . describeAttributes ( 0 , 200 )
541- for ( var i = 0 ; i < attributes . Response . Data . TotalCount ; i ++ ) {
542- const metricAttribute = attributes . Response . Data . Data [ i ]
555+ attributes . Response . Data . Data . push ( attributes . Response . Data . Data [ 10 ] )
543556
544- if ( ! filterAttributeName ( metricAttribute . AttributeName , metricsRule ) ) {
545- continue
546- }
547- metricAttributeHash [ metricAttribute . AttributeId ] = metricAttribute
548- requestHandlers . push (
549- this . describeCCMInstanceDatas (
557+ let i = 0
558+ const _this = this
559+ function run ( ) {
560+ if ( attributes . Response . Data . Data . length > 0 ) {
561+ const metricAttribute = attributes . Response . Data . Data . shift ( )
562+ metricAttributeHash [ metricAttribute . AttributeId ] = metricAttribute
563+
564+ return _this . describeCCMInstanceDatas (
550565 metricAttribute . AttributeId ,
551566 announceInstance ,
552567 rangeTime . rangeStart ,
553- rangeTime . rangeEnd
554- )
555- )
556-
557- if ( ! ( ( i + 1 ) % apiQPSLimit ) ) {
558- if ( i + 1 != apiQPSLimit ) {
559- sleep . sleep ( 1 )
560- }
561- firstRequestFlag = false
562- results = await getMetricsResponse ( requestHandlers )
563- responses = responses . concat ( results )
564- requestHandlers = [ ]
568+ rangeTime . rangeEnd ,
569+ i ++ , apiQPSLimit
570+ ) . then ( res => {
571+ responses . push ( res )
572+ return run ( )
573+ } )
565574 }
566575 }
567- if ( requestHandlers . length === 0 ) {
568- responses = this . cleanEmptyMetric ( responses , metricAttributeHash )
576+
577+ const promiseList = Array ( Math . min ( apiQPSLimit , attributes . Response . Data . Data . length ) )
578+ . fill ( Promise . resolve ( ) )
579+ . map ( promise => promise . then ( run ) )
580+
581+ return Promise . all ( promiseList ) . then ( ( ) => {
569582 this . aggrCustomDatas ( responses , period , metricAttributeHash )
570583 return responses
571- }
572- if ( ! firstRequestFlag ) {
573- sleep . sleep ( 1 )
574- }
575- results = await getMetricsResponse ( requestHandlers )
576- results = this . cleanEmptyMetric ( results , metricAttributeHash )
577- responses = responses . concat ( results )
578- this . aggrCustomDatas ( responses , period , metricAttributeHash )
579- return responses
584+ } )
580585 }
581586
582587 cleanEmptyMetric ( datas , metricAttributeHash ) {
0 commit comments