Skip to content

Commit 35366aa

Browse files
committed
feat: Cube.js agent
1 parent c4298ea commit 35366aa

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
};

packages/cubejs-server-core/core/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const RefreshScheduler = require('./RefreshScheduler');
1212
const FileRepository = require('./FileRepository');
1313
const DevServer = require('./DevServer');
1414
const track = require('./track');
15+
const agentCollect = require('./agentCollect');
1516

1617
const DriverDependencies = {
1718
postgres: '@cubejs-backend/postgres-driver',
@@ -255,6 +256,8 @@ class CubejsServerCore {
255256
}
256257
};
257258

259+
this.initAgent();
260+
258261
if (this.options.devServer) {
259262
this.devServer = new DevServer(this);
260263
const oldLogger = this.logger;
@@ -311,6 +314,23 @@ class CubejsServerCore {
311314
}
312315
}
313316

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+
314334
static create(options) {
315335
return new CubejsServerCore(options);
316336
}

0 commit comments

Comments
 (0)