lib/service.js
function attachMonitoringEmitter(request) {
    var attemptTimestamp; //timestamp marking the beginning of a request attempt
    var attemptStartRealTime; //Start time of request attempt. Used to calculating attemptLatency
    var attemptLatency; //latency from request sent out to http response reaching SDK
    var callStartRealTime; //Start time of API call. Used to calculating API call latency
    var attemptCount = 0; //request.retryCount is not reliable here
    var region; //region cache region for each attempt since it can be updated in plase (e.g. s3)
    var callTimestamp; //timestamp when the request is created
    var self = this;
    var addToHead = true;

    request.on('validate', function () {
      callStartRealTime = AWS.util.realClock.now();
      callTimestamp = Date.now();
    }, addToHead);
    request.on('sign', function () {
      attemptStartRealTime = AWS.util.realClock.now();
      attemptTimestamp = Date.now();
      region = request.httpRequest.region;
      attemptCount++;
    }, addToHead);
    request.on('validateResponse', function() {
      attemptLatency = Math.round(AWS.util.realClock.now() - attemptStartRealTime);
    });
    request.addNamedListener('API_CALL_ATTEMPT', 'success', function API_CALL_ATTEMPT() {
      var apiAttemptEvent = self.apiAttemptEvent(request);
      apiAttemptEvent.Timestamp = attemptTimestamp;
      apiAttemptEvent.AttemptLatency = attemptLatency >= 0 ? attemptLatency : 0;
      apiAttemptEvent.Region = region;
      self.emit('apiCallAttempt', [apiAttemptEvent]);
    });
    request.addNamedListener('API_CALL_ATTEMPT_RETRY', 'retry', function API_CALL_ATTEMPT_RETRY() {
      var apiAttemptEvent = self.attemptFailEvent(request);
      apiAttemptEvent.Timestamp = attemptTimestamp;
      //attemptLatency may not be available if fail before response
      attemptLatency = attemptLatency ||
        Math.round(AWS.util.realClock.now() - attemptStartRealTime);
      apiAttemptEvent.AttemptLatency = attemptLatency >= 0 ? attemptLatency : 0;
      apiAttemptEvent.Region = region;
      self.emit('apiCallAttempt', [apiAttemptEvent]);
    });
    request.addNamedListener('API_CALL', 'complete', function API_CALL() {
      var apiCallEvent = self.apiCallEvent(request);
      apiCallEvent.AttemptCount = attemptCount;
      if (apiCallEvent.AttemptCount <= 0) return;
      apiCallEvent.Timestamp = callTimestamp;
      var latency = Math.round(AWS.util.realClock.now() - callStartRealTime);
      apiCallEvent.Latency = latency >= 0 ? latency : 0;
      var response = request.response;
      if (
        typeof response.retryCount === 'number' &&
        typeof response.maxRetries === 'number' &&
        (response.retryCount >= response.maxRetries)
      ) {
        apiCallEvent.MaxRetriesExceeded = 1;
      }
      self.emit('apiCall', [apiCallEvent]);
    });
  }
function attachMonitoringEmitter(request) {
    var attemptTimestamp; //timestamp marking the beginning of a request attempt
    var attemptStartRealTime; //Start time of request attempt. Used to calculating attemptLatency
    var attemptLatency; //latency from request sent out to http response reaching SDK
    var callStartRealTime; //Start time of API call. Used to calculating API call latency
    var attemptCount = 0; //request.retryCount is not reliable here
    var region; //region cache region for each attempt since it can be updated in plase (e.g. s3)
    var callTimestamp; //timestamp when the request is created
    var self = this;
    var addToHead = true;

    request.on('validate', function () {
      callStartRealTime = AWS.util.realClock.now();
      callTimestamp = Date.now();
    }, addToHead);
    request.on('sign', function () {
      attemptStartRealTime = AWS.util.realClock.now();
      attemptTimestamp = Date.now();
      region = request.httpRequest.region;
      attemptCount++;
    }, addToHead);
    request.on('validateResponse', function() {
      attemptLatency = Math.round(AWS.util.realClock.now() - attemptStartRealTime);
    });
    request.addNamedListener('API_CALL_ATTEMPT', 'success', function API_CALL_ATTEMPT() {
      var apiAttemptEvent = self.apiAttemptEvent(request);
      apiAttemptEvent.Timestamp = attemptTimestamp;
      apiAttemptEvent.AttemptLatency = attemptLatency >= 0 ? attemptLatency : 0;
      apiAttemptEvent.Region = region;
      self.emit('apiCallAttempt', [apiAttemptEvent]);
    });
    request.addNamedListener('API_CALL_ATTEMPT_RETRY', 'retry', function API_CALL_ATTEMPT_RETRY() {
      var apiAttemptEvent = self.attemptFailEvent(request);
      apiAttemptEvent.Timestamp = attemptTimestamp;
      //attemptLatency may not be available if fail before response
      attemptLatency = attemptLatency ||
        Math.round(AWS.util.realClock.now() - attemptStartRealTime);
      apiAttemptEvent.AttemptLatency = attemptLatency >= 0 ? attemptLatency : 0;
      apiAttemptEvent.Region = region;
      self.emit('apiCallAttempt', [apiAttemptEvent]);
    });
    request.addNamedListener('API_CALL', 'complete', function API_CALL() {
      var apiCallEvent = self.apiCallEvent(request);
      apiCallEvent.AttemptCount = attemptCount;
      if (apiCallEvent.AttemptCount <= 0) return;
      apiCallEvent.Timestamp = callTimestamp;
      var latency = Math.round(AWS.util.realClock.now() - callStartRealTime);
      apiCallEvent.Latency = latency >= 0 ? latency : 0;
      var response = request.response;
      if (
        typeof response.retryCount === 'number' &&
        typeof response.maxRetries === 'number' &&
        (response.retryCount >= response.maxRetries)
      ) {
        apiCallEvent.MaxRetriesExceeded = 1;
      }
      self.emit('apiCall', [apiCallEvent]);
    });
  }
function attachMonitoringEmitter ( request ) { var attemptTimestamp ; //timestamp marking the beginning of a request attempt var attemptStartRealTime ; //Start time of request attempt. Used to calculating attemptLatency var attemptLatency ; //latency from request sent out to http response reaching SDK var callStartRealTime ; //Start time of API call. Used to calculating API call latency var attemptCount = 0 ; //request.retryCount is not reliable here var region ; //region cache region for each attempt since it can be updated in plase (e.g. s3) var callTimestamp ; //timestamp when the request is created var self = this ; var addToHead = true ; request . on ( 'validate' , function ( ) { callStartRealTime = AWS . util . realClock . now ( ) ; callTimestamp = Date . now ( ) ; } , addToHead ) ; request . on ( 'sign' , function ( ) { attemptStartRealTime = AWS . util . realClock . now ( ) ; attemptTimestamp = Date . now ( ) ; region = request . httpRequest . region ; attemptCount ++ ; } , addToHead ) ; request . on ( 'validateResponse' , function ( ) { attemptLatency = Math . round ( AWS . util . realClock . now ( ) - attemptStartRealTime ) ; } ) ; request . addNamedListener ( 'API_CALL_ATTEMPT' , 'success' , function API_CALL_ATTEMPT ( ) { var apiAttemptEvent = self . apiAttemptEvent ( request ) ; apiAttemptEvent . Timestamp = attemptTimestamp ; apiAttemptEvent . AttemptLatency = attemptLatency >= 0 ? attemptLatency : 0 ; apiAttemptEvent . Region = region ; self . emit ( 'apiCallAttempt' , [ apiAttemptEvent ] ) ; } ) ; request . addNamedListener ( 'API_CALL_ATTEMPT_RETRY' , 'retry' , function API_CALL_ATTEMPT_RETRY ( ) { var apiAttemptEvent = self . attemptFailEvent ( request ) ; apiAttemptEvent . Timestamp = attemptTimestamp ; //attemptLatency may not be available if fail before response attemptLatency = attemptLatency || Math . round ( AWS . util . realClock . now ( ) - attemptStartRealTime ) ; apiAttemptEvent . AttemptLatency = attemptLatency >= 0 ? attemptLatency : 0 ; apiAttemptEvent . Region = region ; self . emit ( 'apiCallAttempt' , [ apiAttemptEvent ] ) ; } ) ; request . addNamedListener ( 'API_CALL' , 'complete' , function API_CALL ( ) { var apiCallEvent = self . apiCallEvent ( request ) ; apiCallEvent . AttemptCount = attemptCount ; if ( apiCallEvent . AttemptCount <= 0 ) return ; apiCallEvent . Timestamp = callTimestamp ; var latency = Math . round ( AWS . util . realClock . now ( ) - callStartRealTime ) ; apiCallEvent . Latency = latency >= 0 ? latency : 0 ; var response = request . response ; if ( typeof response . retryCount === 'number' && typeof response . maxRetries === 'number' && ( response . retryCount >= response . maxRetries ) ) { apiCallEvent . MaxRetriesExceeded = 1 ; } self . emit ( 'apiCall' , [ apiCallEvent ] ) ; } ) ; }
Attach listeners to request object to fetch metrics of each request
and emit data object through \'ApiCall\' and \'ApiCallAttempt\' events.
@api private
Attach listeners to request object to fetch metrics of each request and emit data object through \ ApiCall \ and \ ApiCallAttempt \ events .