File tree Expand file tree Collapse file tree 2 files changed +70
-0
lines changed
packages/cubejs-server-core/core Expand file tree Collapse file tree 2 files changed +70
-0
lines changed Original file line number Diff line number Diff line change
1
+ const fetch = require ( 'node-fetch' ) ;
2
+ const crypto = require ( 'crypto' ) ;
3
+
4
+ let flushPromise = null ;
5
+ let trackEvents = [ ] ;
6
+
7
+ module . exports = async ( event , endpointUrl , logger ) => {
8
+ trackEvents . push ( {
9
+ ...event ,
10
+ id : crypto . randomBytes ( 16 ) . toString ( 'hex' ) ,
11
+ timestamp : new Date ( ) . toJSON ( )
12
+ } ) ;
13
+ const flush = async ( toFlush , retries ) => {
14
+ if ( ! toFlush ) {
15
+ toFlush = trackEvents ;
16
+ trackEvents = [ ] ;
17
+ }
18
+ if ( ! toFlush . length ) {
19
+ return null ;
20
+ }
21
+ if ( retries == null ) {
22
+ retries = 10 ;
23
+ }
24
+ try {
25
+ const sentAt = new Date ( ) . toJSON ( ) ;
26
+ const result = await fetch ( endpointUrl , {
27
+ method : 'post' ,
28
+ body : JSON . stringify ( toFlush . map ( r => ( { ...r , sentAt } ) ) ) ,
29
+ headers : { 'Content-Type' : 'application/json' } ,
30
+ } ) ;
31
+ if ( result . status !== 200 && retries > 0 ) {
32
+ return flush ( toFlush , retries - 1 ) ;
33
+ }
34
+ // console.log(await result.json());
35
+ } catch ( e ) {
36
+ if ( retries > 0 ) {
37
+ return flush ( toFlush , retries - 1 ) ;
38
+ }
39
+ logger ( 'Agent Error' , { error : ( e . stack || e ) . toString ( ) } ) ;
40
+ }
41
+ return null ;
42
+ } ;
43
+ const currentPromise = ( flushPromise || Promise . resolve ( ) ) . then ( ( ) => flush ( ) ) . then ( ( ) => {
44
+ if ( currentPromise === flushPromise ) {
45
+ flushPromise = null ;
46
+ }
47
+ } ) ;
48
+ flushPromise = currentPromise ;
49
+ return flushPromise ;
50
+ } ;
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ const RefreshScheduler = require('./RefreshScheduler');
12
12
const FileRepository = require ( './FileRepository' ) ;
13
13
const DevServer = require ( './DevServer' ) ;
14
14
const track = require ( './track' ) ;
15
+ const agentCollect = require ( './agentCollect' ) ;
15
16
16
17
const DriverDependencies = {
17
18
postgres : '@cubejs-backend/postgres-driver' ,
@@ -255,6 +256,8 @@ class CubejsServerCore {
255
256
}
256
257
} ;
257
258
259
+ this . initAgent ( ) ;
260
+
258
261
if ( this . options . devServer ) {
259
262
this . devServer = new DevServer ( this ) ;
260
263
const oldLogger = this . logger ;
@@ -311,6 +314,23 @@ class CubejsServerCore {
311
314
}
312
315
}
313
316
317
+ initAgent ( ) {
318
+ if ( process . env . CUBEJS_AGENT_ENDPOINT_URL ) {
319
+ const oldLogger = this . logger ;
320
+ this . logger = ( msg , params ) => {
321
+ oldLogger ( msg , params ) ;
322
+ agentCollect (
323
+ {
324
+ msg,
325
+ ...params
326
+ } ,
327
+ process . env . CUBEJS_AGENT_ENDPOINT_URL ,
328
+ oldLogger
329
+ ) ;
330
+ } ;
331
+ }
332
+ }
333
+
314
334
static create ( options ) {
315
335
return new CubejsServerCore ( options ) ;
316
336
}
You can’t perform that action at this time.
0 commit comments