|
| 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 | +} |
0 commit comments