44 *
55 * Base class for AWS Lambda based collectors.
66 *
7- * Last message ID: AWSC0010
7+ * Last message ID: AWSC0011
88 * @end
99 * -----------------------------------------------------------------------------
1010 */
@@ -120,31 +120,46 @@ class AlAwsCollector {
120120 done ( error ) {
121121 let context = this . _invokeContext ;
122122 if ( error ) {
123- // The lambda context tires to stringify errors, so we should check if they can be stringified before we pass them to the context
123+ // The lambda context tries to stringify errors,
124+ // so we should check if they can be stringified before we pass them to the context
125+ let errorString ;
124126 try {
125- const stringifiedError = JSON . stringify ( error ) ;
126- return context . fail ( error ) ;
127+ errorString = JSON . stringify ( error ) ;
127128 }
128129 catch ( stringifyError ) {
129130 // Can't stringify the whole error, so lets try and get some useful info from it
130- let errorString ;
131-
132- if ( error . toJSON ) {
133- errorString = error . toJSON ( ) ;
134- } else if ( error . message ) {
135- errorString = error . message ;
136- } else {
137- // when all else fails, stringify it the gross way with inspect
138- errorString = util . inspect ( error ) ;
139- }
140-
141- return context . fail ( errorString ) ;
131+ errorString = error . toJSON ? error . toJSON ( ) :
132+ error . message ? error . message :
133+ // when all else fails, stringify it the gross way with inspect
134+ util . inspect ( error ) ;
142135 }
136+ const status = this . prepareErrorStatus ( errorString ) ;
137+ this . sendStatus ( status , ( sendError ) => {
138+ console . warn ( 'AWSC0011 Collector status send failed' , sendError ) ;
139+ context . fail ( errorString ) ;
140+ } ) ;
143141 } else {
144142 return context . succeed ( ) ;
145143 }
146144 }
147145
146+ prepareErrorStatus ( errorString , streamName = 'error' , collectionType ) {
147+ let cType = collectionType ? collectionType : this . _ingestType ;
148+ return {
149+ stream_name : streamName ,
150+ status_type : 'error' ,
151+ stream_type : 'collector' ,
152+ message_type : 'collector_status' ,
153+ host_uuid : this . _collectorId ,
154+ data : [
155+ { error : errorString }
156+ ] ,
157+ agent_type : this . _collectorType ,
158+ collection_type : cType ,
159+ timestamp : moment ( ) . unix ( )
160+ } ;
161+ }
162+
148163 getProperties ( ) {
149164 return {
150165 awsAccountId : m_alAws . arnToAccId ( this . _arn ) ,
@@ -353,6 +368,28 @@ class AlAwsCollector {
353368 } ) ;
354369 }
355370
371+ sendStatus ( status , callback ) {
372+ let collector = this ;
373+
374+ if ( ! status ) {
375+ return callback ( null ) ;
376+ } else {
377+ zlib . deflate ( JSON . stringify ( [ status ] ) , function ( compressionErr , compressed ) {
378+ if ( compressionErr ) {
379+ return callback ( compressionErr ) ;
380+ } else {
381+ collector . _ingestc . sendAgentstatus ( compressed )
382+ . then ( resp => {
383+ return callback ( null , resp ) ;
384+ } )
385+ . catch ( exception => {
386+ return callback ( exception ) ;
387+ } ) ;
388+ }
389+ } ) ;
390+ }
391+ }
392+
356393 send ( data , compress = true , callback ) {
357394 var collector = this ;
358395
@@ -416,7 +453,7 @@ class AlAwsCollector {
416453 collector . _formatFun ( event , context , asyncCallback ) ;
417454 } ,
418455 function ( formattedData , compress , asyncCallback ) {
419- if ( arguments . length === 2 && typeof compress === " function" ) {
456+ if ( arguments . length === 2 && typeof compress === ' function' ) {
420457 asyncCallback = compress ;
421458 compress = true ;
422459 }
@@ -427,7 +464,7 @@ class AlAwsCollector {
427464 }
428465
429466 processLog ( messages , formatFun , hostmetaElems , callback ) {
430- if ( arguments . length === 3 && typeof hostmetaElems === " function" ) {
467+ if ( arguments . length === 3 && typeof hostmetaElems === ' function' ) {
431468 callback = hostmetaElems ;
432469 hostmetaElems = this . _defaultHostmetaElems ( ) ;
433470 }
0 commit comments