@@ -7,71 +7,60 @@ var util = require('util'),
77
88 storage = [ ] ,
99
10- // flag that indicates if logs are being sent right now. if false then new batches of messages won't be sent.
11- flag = false ,
12- // deferred checking of logs
13- timeout = 0 ,
14- //number of failed attempts of sending logs
15- fail_counter = 0 ,
10+ // current delay
11+ delay = CONFIG . DELAY . ONE_SECOND_DELAY ,
12+
1613 // hash that contains all the errors and their number logged during the current minute
1714 error_storage = { } ,
1815
19- // automatically send all the messages from the queue after some timeout
20- checkLogs = function ( ) {
21- var length = Math . min ( storage . length , CONFIG . MSG_LIMIT ) ;
22- if ( flag && length ) {
23- sendLogs ( length ) ;
24- }
25- console . log ( 'logs checked' ) ;
26- } ,
2716 // handler for sending logs
28- sendLogs = function ( length ) {
29- var self = this ,
30- data = storage . slice ( 0 , length || CONFIG . MSG_LIMIT ) ,
17+ sendLogs = function ( ) {
18+ var length = storage . length ,
19+ chunk = Math . min ( length , CONFIG . MSG . MAX_BATCH_SIZE ) ,
20+ data = storage . slice ( 0 , chunk ) ,
3121
32- /* if request is succesful remove messages from the queue, change the timeout for the next checkLogs call,
33- switch the flag, send another batch (if there are enough messages in the queue)
22+ /* if request is succesful remove messages from the queue,
23+ send another batch (if there are enough messages in the queue)
3424 */
3525 success = function ( response ) {
36- fail_counter = 0 ;
26+ storage = storage . slice ( chunk ) ; // remove this chunk from the queue
27+ length -= chunk ;
28+ api . last_api_error = 0 ; // reset the last HTTP error
3729
38- storage = storage . slice ( length ) ;
39- console . log ( 'logs sent ') ;
30+ console . log ( chunk , ' sent' ) ;
31+ console . log ( length , ' last ') ;
4032
41- // if there are enough messages for the batch, send another request
33+ if ( length ) {
34+ chunk = Math . min ( length , CONFIG . MSG . MAX_BATCH_SIZE ) ;
35+ data = storage . slice ( 0 , chunk ) ;
4236
43- if ( storage . length >= CONFIG . MSG_LIMIT ) {
44- sendLogs ( ) ;
37+ api . methods . postLogs ( data , success ) ;
4538 } else {
46- flag = true ;
47-
48- // reset previous timeout, define the new one
49- if ( timeout ) {
50- clearTimeout ( timeout ) ;
51- }
52-
53- timeout = setTimeout ( checkLogs , CONFIG . SCAN_TIMER ) ;
39+ console . log ( 'all sent' ) ;
40+ // full batch is sent, set timeout for the next request
41+ setTimeout ( sendLogs , delay ) ;
5442 }
55- } ,
56-
57- // retry the attempt after some timeout
58- fail = function ( ) {
59- flag = false ;
60- fail_counter += 1 ;
61-
62- setTimeout ( function ( ) {
63- api . methods . postLogs ( data , success , fail ) ;
64- } , CONFIG . REQUEST_TIMER ) ;
6543 } ;
6644
67- if ( fail_counter <= CONFIG . REQUEST_ATTEMPTS ) {
68- api . methods . postLogs ( data , success , fail ) ;
69- flag = false ;
45+ // count the delay
46+ if ( length >= CONFIG . MSG . MAX_BATCH_SIZE ) {
47+ delay = Math . max ( Math . round ( delay / 2.0 ) , CONFIG . DELAY . ONE_SECOND_DELAY ) ;
48+ } else {
49+ delay = Math . min ( Math . round ( 1.25 * delay ) , CONFIG . DELAY . FIVE_SECONDS_DELAY ) ;
50+ }
51+
52+ if ( data . length && CONFIG . APIKEY ) {
53+ api . methods . postLogs ( data , success ) ;
54+ } else {
55+ console . log ( 'no messages' ) ;
56+ console . log ( 'delay ' , delay ) ;
57+ // queue is empty, set timeout for the next request
58+ setTimeout ( sendLogs , delay ) ;
7059 }
7160 } ,
7261 /**
73- Check for duplicate error messages. If the same error message logged more than configurated limit in one minute
74- don't push it to the queue
62+ * Check for duplicate error messages. If the same error message logged more than configurated limit in one minute
63+ * don't push it to the queue
7564 */
7665 checkErrorLimitMessage = function checkErrorLimitMessage ( ex ) {
7766 var min = new Date ( ) . getMinutes ( ) ,
@@ -93,14 +82,14 @@ var util = require('util'),
9382 error_storage [ min ] [ key ] = 1 ;
9483 }
9584
96- return ( error_storage [ min ] [ key ] < CONFIG . MAX_DUP_ERROR_PER_MINUTE ) ? true : false ;
85+ return ( error_storage [ min ] [ key ] < CONFIG . MSG . MAX_DUP_ERROR_PER_MINUTE ) ? true : false ;
9786 } ;
9887
9988module . exports . storage = storage ;
10089
10190module . exports . methods = {
10291 // create the message object, push it to the queue and execute sendLogs call if messages cap is exceeded
103- push : function push ( level , msg , meta ) {
92+ push : function push ( level , msg , meta , req ) {
10493 var err = new Error ( ) ,
10594 getStack = error . getStackTraceItem ( err ) ,
10695 rec = {
@@ -117,7 +106,7 @@ module.exports.methods = {
117106 if ( level . toLowerCase ( ) === 'error' ) {
118107 data = helpers . parseMeta ( meta , true ) ;
119108 if ( data . ex ) {
120- rec . Ex = error . formatEx ( data . ex ) ;
109+ rec . Ex = error . formatEx ( data . ex , req ) ;
121110 }
122111 } else {
123112 data = helpers . parseMeta ( meta ) ;
@@ -137,27 +126,16 @@ module.exports.methods = {
137126 console . log ( 'logged' , storage . length ) ;
138127
139128 // remove the earliest message from the queue if message cap is exceeded
140- if ( storage . length === CONFIG . MSG_CAP ) {
129+ if ( storage . length === CONFIG . MSG . QUEUE_CAP ) {
141130 storage . shift ( ) ;
142131 }
143132 }
144-
145- if ( flag && storage . length >= CONFIG . MSG_LIMIT && CONFIG . APIKEY ) {
146- sendLogs ( ) ;
147- }
148133 } ,
149134
150- /* check the queue after IdentifyApp call is done, switch the flag, set the timeout for the next checking the queue,
151- set environment details for already logged messages
135+ /* check the queue after IdentifyApp call is done
152136 */
153- init : function init ( ) {
154- flag = true ;
155-
156- timeout = setTimeout ( checkLogs , CONFIG . SCAN_TIMER ) ;
157-
158- if ( storage . length >= CONFIG . MSG_LIMIT ) {
159- sendLogs ( exc . excCaught ? storage . length : null ) ;
160- }
137+ send : function send ( ) {
138+ setTimeout ( sendLogs , delay ) ;
161139 } ,
162140
163141 log : function log ( level , msg , meta ) {
0 commit comments