Skip to content

Commit 747d370

Browse files
committed
Added cube
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 0bbff2e commit 747d370

6 files changed

Lines changed: 952 additions & 7 deletions

File tree

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const {Client} = require('./lib/client')
2-
31
module.exports = {
4-
Client,
2+
Client: require('./lib/client').Client,
3+
APIError: require('./lib/client').APIError,
4+
express: require('./lib/express'),
5+
koa: require('./lib/koa')
56
}

lib/cube.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const os = require('os')
2+
const pusage = require('pidusage')
3+
const got = require('got')
4+
const microtime = require('microtime')
5+
6+
class Cube {
7+
constructor(apiKey, options) {
8+
this.apiKey = apiKey
9+
options = options || {}
10+
this.node = options.node || os.hostname()
11+
this.batchSize = options.batchSize || 60
12+
this.dispatchInterval = options.dispatchInterval || 60
13+
this.tags = options.tags
14+
this.startTime = Date.now()
15+
this.uptime = 0
16+
this.cpu = 0.0
17+
this.memory = 0
18+
this.activeRequests = 0
19+
this.requests = []
20+
21+
// Dispatch daemon
22+
setInterval(() => {
23+
this._dispatch()
24+
}, this.dispatchInterval * 1000)
25+
26+
// System daemon
27+
setInterval(() => {
28+
pusage.stat(process.pid, (err, stat) => {
29+
this.uptime = (Date.now() - this.startTime) / 1000
30+
this.cpu = stat.cpu
31+
this.memory = stat.memory
32+
})
33+
}, 1000)
34+
}
35+
36+
_dispatch() {
37+
if (!this.requests.length) {
38+
return
39+
}
40+
41+
// TODO: handler error
42+
got.post('https://api.labstack.com/cube', {
43+
headers: {
44+
'User-Agent': 'labstack/cube',
45+
'Authorization': `Bearer ${this.apiKey}`
46+
},
47+
body: this.requests,
48+
json: true
49+
}).then(response => {
50+
}).catch(error => {
51+
console.error(error)
52+
// if (error instanceof got.HTTPError) {
53+
// const body = JSON.parse(error.response.body)
54+
// throw new Error(body.code, body.message)
55+
// } else {
56+
// throw new APIError(0, error)
57+
// }
58+
})
59+
60+
// Reset requests
61+
this.requests.length = 0
62+
}
63+
64+
start(request) {
65+
this.activeRequests++
66+
67+
request.time = microtime.now()
68+
request.active = this.activeRequests
69+
request.node = this.node
70+
request.uptime = this.uptime
71+
request.cpu = this.cpu
72+
request.memory = this.memory
73+
request.tags = this.tags
74+
this.requests.push(request)
75+
76+
return request
77+
}
78+
79+
stop(request) {
80+
this.activeRequests--
81+
82+
request.latency = microtime.now() - request.time
83+
84+
// Dispatch batch
85+
if (this.requests.length >= this.batchSize) {
86+
this._dispatch()
87+
}
88+
}
89+
}
90+
91+
// class CubeError extends Error {
92+
// constructor(code, message) {
93+
// super(message)
94+
// this.code = code
95+
// Error.captureStackTrace(this, APIError)
96+
// this.name = this.constructor.name
97+
// }
98+
// }
99+
100+
module.exports = {
101+
Cube
102+
}

lib/express.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const {Cube} = require('./cube')
2+
3+
function cube(key, options) {
4+
const c = new Cube(key, options)
5+
6+
return (request, response, next) => {
7+
const r = c.start({
8+
id: request.get('X-Request-ID'),
9+
host: request.hostname,
10+
path: request.path,
11+
method: request.method,
12+
bytes_in: parseInt(request.get('Content-Length') || 0),
13+
remote_ip: request.ip,
14+
client_id: request.get('X-Client-ID'),
15+
user_agent: request.get('User-Agent')
16+
})
17+
18+
next()
19+
const err = response.locals.error
20+
if (err) {
21+
r.error = err.message
22+
r.stack_trace = err.stack
23+
}
24+
25+
r.id = r.id || response.get('x-request-id')
26+
r.status = response.statusCode
27+
r.bytes_out = parseInt(response.get('content-length')) || 0
28+
c.stop(r)
29+
}
30+
}
31+
32+
module.exports = {
33+
cube
34+
}

lib/koa.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const {Cube} = require('./cube')
2+
3+
function cube(key, options) {
4+
const c = new Cube(key, options)
5+
6+
return async (ctx, next) => {
7+
const r = c.start({
8+
id: ctx.request.get('x-request-id'),
9+
host: ctx.hostname,
10+
path: ctx.path,
11+
method: ctx.method,
12+
bytes_in: parseInt(ctx.request.get('content-length')) || 0,
13+
remote_ip: ctx.ip,
14+
client_id: ctx.request.get('x-client-id'),
15+
user_agent: ctx.request.get('user-agent')
16+
})
17+
18+
try {
19+
await next()
20+
} catch (err) {
21+
r.error = err.message
22+
r.stack_trace = err.stack
23+
ctx.status = err.status || 500
24+
ctx.body = err.message
25+
ctx.app.emit('error', err, ctx)
26+
}
27+
28+
r.id = r.id || ctx.response.get('x-request-id')
29+
r.status = ctx.status
30+
r.bytes_out = ctx.length
31+
c.stop(r)
32+
}
33+
}
34+
35+
module.exports = {
36+
cube
37+
}

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "labstack",
3-
"version": "0.31.4",
3+
"version": "0.31.5",
44
"description": "Official Node.js client library for the LabStack platform",
55
"main": "index.js",
66
"scripts": {
@@ -18,8 +18,12 @@
1818
},
1919
"homepage": "https://github.com/labstack/labstack-node#readme",
2020
"dependencies": {
21+
"express": "^4.16.3",
2122
"form-data": "^2.3.1",
22-
"got": "^7.1.0"
23+
"got": "^7.1.0",
24+
"koa": "^2.5.1",
25+
"microtime": "^2.1.8",
26+
"pidusage": "^2.0.6"
2327
},
2428
"engines": {
2529
"node": ">= 6"

0 commit comments

Comments
 (0)