33const http = require ( 'http' ) ;
44const https = require ( 'https' ) ;
55const os = require ( 'os' ) ;
6- const url_lib = require ( 'url' ) ;
6+ const urls = require ( 'url' ) ;
77const usage_loggers = require ( './usage_loggers' ) ;
88const zlib = require ( 'zlib' ) ;
99
@@ -49,12 +49,40 @@ class BaseLogger {
4949 this . _enabled = false ;
5050 }
5151
52- // mark immutable properties
52+ // parse and cache url properties
53+ if ( this . _url != null ) {
54+ try {
55+ const target = urls . parse ( this . _url ) ;
56+ this . _url_library = target . protocol === 'https' ? https : http ;
57+ this . _url_options = {
58+ host : target . host . split ( ':' ) [ 0 ] ,
59+ port : target . port ,
60+ path : target . path ,
61+ method : 'POST' ,
62+ headers : { 'Content-Encoding' : this . _skip_compression ? 'identity' : 'deflated' }
63+ } ;
64+ } catch ( e ) {
65+ this . _url = null ;
66+ this . _url_library = null ;
67+ this . _url_options = null ;
68+ this . _enabled = false ;
69+ }
70+ }
71+
72+ // finalize internal properties
5373 this . _enableable = ( this . _queue !== null ) || ( this . _url !== null ) ;
74+ this . _submit_failures = new Uint32Array ( new SharedArrayBuffer ( Int32Array . BYTES_PER_ELEMENT ) ) ;
75+ this . _submit_successes = new Uint32Array ( new SharedArrayBuffer ( Int32Array . BYTES_PER_ELEMENT ) ) ;
76+
77+ // mark immutable properties
5478 Object . defineProperty ( this , '_agent' , { configurable : false , writable : false } ) ;
5579 Object . defineProperty ( this , '_host' , { configurable : false , writable : false } ) ;
5680 Object . defineProperty ( this , '_queue' , { configurable : false , writable : false } ) ;
81+ Object . defineProperty ( this , '_submit_failures' , { configurable : false , writable : false } ) ;
82+ Object . defineProperty ( this , '_submit_successes' , { configurable : false , writable : false } ) ;
5783 Object . defineProperty ( this , '_url' , { configurable : false , writable : false } ) ;
84+ Object . defineProperty ( this , '_url_library' , { configurable : false , writable : false } ) ;
85+ Object . defineProperty ( this , '_url_options' , { configurable : false , writable : false } ) ;
5886 Object . defineProperty ( this , '_version' , { configurable : false , writable : false } ) ;
5987 }
6088
@@ -157,23 +185,19 @@ class BaseLogger {
157185 } else {
158186 return new Promise ( ( resolve , reject ) => {
159187 try {
160- const target = url_lib . parse ( this . _url ) ;
161- const options = {
162- host : target . host . split ( ':' ) [ 0 ] ,
163- port : target . port ,
164- path : target . path ,
165- method : 'POST' ,
166- headers : { 'Content-Encoding' : this . _skip_compression ? 'identity' : 'deflated' }
167- } ;
168- const lib = target . protocol === 'https' ? https : http ;
169- const request = lib . request ( options , ( response ) => {
188+ const request = this . _url_library . request ( this . _url_options , ( response ) => {
170189 if ( response . statusCode === 204 ) {
190+ Atomics . add ( this . _submit_successes , 0 , 1 ) ;
171191 resolve ( true ) ;
172192 } else {
173- resolve ( true ) ; // todo count as error
193+ Atomics . add ( this . _submit_failures , 0 , 1 ) ;
194+ resolve ( true ) ;
174195 }
175196 } ) ;
176- request . on ( 'error' , ( ) => resolve ( true ) ) ; // todo count as error
197+ request . on ( 'error' , ( ) => {
198+ Atomics . add ( this . _submit_failures , 0 , 1 ) ;
199+ resolve ( true ) ;
200+ } ) ;
177201 if ( this . _skip_compression ) {
178202 request . write ( msg ) ;
179203 request . end ( ) ;
@@ -184,12 +208,27 @@ class BaseLogger {
184208 } ) ;
185209 }
186210 } catch ( e ) {
187- resolve ( true ) ; // todo count as error
211+ Atomics . add ( this . _submit_failures , 0 , 1 ) ;
212+ resolve ( true ) ;
188213 }
189214 } ) ;
190215 }
191216 }
192217
218+ /**
219+ * Returns count of submissions that failed.
220+ */
221+ get submit_failures ( ) {
222+ return Atomics . load ( this . _submit_failures , 0 ) ;
223+ }
224+
225+ /**
226+ * Returns count of submissions that succeeded.
227+ */
228+ get submit_successes ( ) {
229+ return Atomics . load ( this . _submit_successes , 0 ) ;
230+ }
231+
193232 /**
194233 * Returns url destination where messages are sent.
195234 */
0 commit comments