3
3
const http = require ( 'http' ) ;
4
4
const https = require ( 'https' ) ;
5
5
const os = require ( 'os' ) ;
6
- const url_lib = require ( 'url' ) ;
6
+ const urls = require ( 'url' ) ;
7
7
const usage_loggers = require ( './usage_loggers' ) ;
8
8
const zlib = require ( 'zlib' ) ;
9
9
@@ -49,12 +49,40 @@ class BaseLogger {
49
49
this . _enabled = false ;
50
50
}
51
51
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
53
73
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
54
78
Object . defineProperty ( this , '_agent' , { configurable : false , writable : false } ) ;
55
79
Object . defineProperty ( this , '_host' , { configurable : false , writable : false } ) ;
56
80
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 } ) ;
57
83
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 } ) ;
58
86
Object . defineProperty ( this , '_version' , { configurable : false , writable : false } ) ;
59
87
}
60
88
@@ -157,23 +185,19 @@ class BaseLogger {
157
185
} else {
158
186
return new Promise ( ( resolve , reject ) => {
159
187
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 ) => {
170
189
if ( response . statusCode === 204 ) {
190
+ Atomics . add ( this . _submit_successes , 0 , 1 ) ;
171
191
resolve ( true ) ;
172
192
} else {
173
- resolve ( true ) ; // todo count as error
193
+ Atomics . add ( this . _submit_failures , 0 , 1 ) ;
194
+ resolve ( true ) ;
174
195
}
175
196
} ) ;
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
+ } ) ;
177
201
if ( this . _skip_compression ) {
178
202
request . write ( msg ) ;
179
203
request . end ( ) ;
@@ -184,12 +208,27 @@ class BaseLogger {
184
208
} ) ;
185
209
}
186
210
} catch ( e ) {
187
- resolve ( true ) ; // todo count as error
211
+ Atomics . add ( this . _submit_failures , 0 , 1 ) ;
212
+ resolve ( true ) ;
188
213
}
189
214
} ) ;
190
215
}
191
216
}
192
217
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
+
193
232
/**
194
233
* Returns url destination where messages are sent.
195
234
*/
0 commit comments