-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
248 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,10 @@ | ||
{ | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "airbnb-base", | ||
"rules": { | ||
"strict": [0, "global"], | ||
"prefer-const": 1, | ||
"indent": [1, 4], | ||
"class-methods-use-this": [0], | ||
"import/no-extraneous-dependencies": [0], | ||
"arrow-body-style": [0, "always"], | ||
"no-multiple-empty-lines": [0], | ||
"no-underscore-dangle": [0], | ||
"comma-dangle": [0], | ||
"no-plusplus": [0], | ||
"no-console": [0], | ||
"new-cap": [0] | ||
} | ||
"extends": ["eslint:recommended", "plugin:prettier/recommended"], | ||
"plugins": ["prettier"], | ||
"env": { "es6": true, "node": true }, | ||
"parserOptions": { | ||
"requireConfigFile": false, | ||
"ecmaVersion": 2020, | ||
"sourceType": "module" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ tmp/**/* | |
*.log | ||
package-lock.json | ||
.nyc_output | ||
.tap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"singleQuote": false, | ||
"trailingComma": "all", | ||
"useTabs": true, | ||
"printWidth": 120, | ||
"overrides": [ | ||
{ | ||
"files": ["*.yml"], | ||
"options": { | ||
"tabWidth": 2, | ||
"useTabs": false | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,56 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const Emitter = require('@metrics/emitter'); | ||
const cluster = require('cluster'); | ||
const Client = require('@metrics/client'); | ||
const stream = require('readable-stream'); | ||
const os = require('os'); | ||
const Daemon = require('../'); | ||
const Emitter = require("@metrics/emitter"); | ||
const cluster = require("cluster"); | ||
const Client = require("@metrics/client"); | ||
const stream = require("readable-stream"); | ||
const os = require("os"); | ||
const Daemon = require("../"); | ||
|
||
const port = 60600; | ||
|
||
const master = () => { | ||
const dest = new stream.Writable({ | ||
objectMode: true, | ||
write(chunk, encoding, callback) { | ||
console.log(chunk); | ||
callback(); | ||
}, | ||
}); | ||
|
||
const daemon = new Daemon('udp', { port }); | ||
daemon.pipe(dest); | ||
daemon.listen(); | ||
const dest = new stream.Writable({ | ||
objectMode: true, | ||
write(chunk, encoding, callback) { | ||
console.log(chunk); | ||
callback(); | ||
}, | ||
}); | ||
|
||
const daemon = new Daemon("udp", { port }); | ||
daemon.pipe(dest); | ||
daemon.listen(); | ||
}; | ||
|
||
const worker = () => { | ||
const emitter = new Emitter('udp', { port }); | ||
const client = new Client(); | ||
|
||
client.pipe(emitter); | ||
|
||
const interval = Math.floor((Math.random() * 1000) + 1000); | ||
let counter = 0; | ||
setInterval(() => { | ||
client.metric({ | ||
name: `worker_${cluster.worker.id}`, | ||
description: `Worker number: ${cluster.worker.id}`, | ||
value: counter, | ||
}); | ||
|
||
counter += 1; | ||
}, interval); | ||
const emitter = new Emitter("udp", { port }); | ||
const client = new Client(); | ||
|
||
client.pipe(emitter); | ||
|
||
const interval = Math.floor(Math.random() * 1000 + 1000); | ||
let counter = 0; | ||
setInterval(() => { | ||
client.metric({ | ||
name: `worker_${cluster.worker.id}`, | ||
description: `Worker number: ${cluster.worker.id}`, | ||
value: counter, | ||
}); | ||
|
||
counter += 1; | ||
}, interval); | ||
}; | ||
|
||
const workers = []; | ||
|
||
if (cluster.isMaster) { | ||
for (let i = 0; i < (os.cpus().length - 1); i++) { | ||
workers.push(cluster.fork()); | ||
} | ||
master(); | ||
for (let i = 0; i < os.cpus().length - 1; i++) { | ||
workers.push(cluster.fork()); | ||
} | ||
master(); | ||
} | ||
|
||
if (cluster.isWorker) { | ||
worker(); | ||
worker(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const stream = require('readable-stream'); | ||
const Daemon = require('../'); | ||
const stream = require("readable-stream"); | ||
const Daemon = require("../"); | ||
|
||
const dest = new stream.Writable({ | ||
objectMode: true, | ||
write(chunk, encoding, callback) { | ||
console.log(chunk); | ||
callback(); | ||
}, | ||
objectMode: true, | ||
write(chunk, encoding, callback) { | ||
console.log(chunk); | ||
callback(); | ||
}, | ||
}); | ||
|
||
const daemon = new Daemon('udp'); | ||
const daemon = new Daemon("udp"); | ||
daemon.pipe(dest); | ||
daemon.listen(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const stream = require('readable-stream'); | ||
const Udpd = require('./udpd'); | ||
const stream = require("readable-stream"); | ||
const Udpd = require("./udpd"); | ||
|
||
const MetricsDaemon = class MetricsDaemon extends stream.Readable { | ||
constructor(type = 'udp', options = {}) { | ||
super({ objectMode: true, }); | ||
|
||
let transport; | ||
if (type === 'udp') { | ||
transport = new Udpd(options); | ||
} else { | ||
throw new Error(`Transport type "${type || 'undefined'}" is not supported.`); | ||
} | ||
|
||
Object.defineProperty(this, 'server', { | ||
value: transport, | ||
}); | ||
|
||
this.server.on('message', (metric) => { | ||
if (this.readableFlowing) { | ||
this.push(metric); | ||
} | ||
}); | ||
} | ||
|
||
get [Symbol.toStringTag]() { | ||
return 'MetricsDaemon'; | ||
} | ||
|
||
_read() { | ||
// nothiong to do, push happens in on message | ||
} | ||
|
||
listen() { | ||
this.server.listen(); | ||
} | ||
constructor(type = "udp", options = {}) { | ||
super({ objectMode: true }); | ||
|
||
let transport; | ||
if (type === "udp") { | ||
transport = new Udpd(options); | ||
} else { | ||
throw new Error(`Transport type "${type || "undefined"}" is not supported.`); | ||
} | ||
|
||
Object.defineProperty(this, "server", { | ||
value: transport, | ||
}); | ||
|
||
this.server.on("message", (metric) => { | ||
if (this.readableFlowing) { | ||
this.push(metric); | ||
} | ||
}); | ||
} | ||
|
||
get [Symbol.toStringTag]() { | ||
return "MetricsDaemon"; | ||
} | ||
|
||
_read() { | ||
// nothiong to do, push happens in on message | ||
} | ||
|
||
listen() { | ||
this.server.listen(); | ||
} | ||
}; | ||
|
||
module.exports = MetricsDaemon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const EventEmitter = require('events'); | ||
const Metric = require('@metrics/metric'); | ||
const abslog = require('abslog'); | ||
const dgram = require('dgram'); | ||
const EventEmitter = require("events"); | ||
const Metric = require("@metrics/metric"); | ||
const abslog = require("abslog"); | ||
const dgram = require("dgram"); | ||
|
||
const MetricsDaemonUdpd = class MetricsDaemonUdpd extends EventEmitter { | ||
constructor({ port = 40400, address, logger } = {}) { | ||
super(); | ||
|
||
Object.defineProperty(this, 'socket', { | ||
value: dgram.createSocket('udp4'), | ||
}); | ||
|
||
Object.defineProperty(this, 'address', { | ||
value: address, | ||
}); | ||
|
||
Object.defineProperty(this, 'port', { | ||
value: port, | ||
}); | ||
|
||
Object.defineProperty(this, 'log', { | ||
value: abslog(logger), | ||
}); | ||
|
||
this.socket.on('error', (error) => { | ||
this.log.error(error); | ||
this.socket.close(); | ||
}); | ||
|
||
this.socket.on('message', (data, info) => { | ||
try { | ||
const obj = JSON.parse(data); | ||
const metric = new Metric(obj); | ||
this.emit('message', metric); | ||
} catch (err) { | ||
this.log.error(`Recieved non metric object from ${info.address}:${info.port}`); | ||
} | ||
}); | ||
|
||
this.socket.on('listening', () => { | ||
const info = this.socket.address(); | ||
this.log.info(`Daemon listening on UDP on ${info.address}:${info.port}`); | ||
}); | ||
} | ||
|
||
get [Symbol.toStringTag]() { | ||
return 'MetricsDaemonUdpd'; | ||
} | ||
|
||
listen() { | ||
this.socket.bind(this.port, this.address); | ||
} | ||
|
||
destroy() { | ||
this.socket.close(); | ||
} | ||
constructor({ port = 40400, address, logger } = {}) { | ||
super(); | ||
|
||
Object.defineProperty(this, "socket", { | ||
value: dgram.createSocket("udp4"), | ||
}); | ||
|
||
Object.defineProperty(this, "address", { | ||
value: address, | ||
}); | ||
|
||
Object.defineProperty(this, "port", { | ||
value: port, | ||
}); | ||
|
||
Object.defineProperty(this, "log", { | ||
value: abslog(logger), | ||
}); | ||
|
||
this.socket.on("error", (error) => { | ||
this.log.error(error); | ||
this.socket.close(); | ||
}); | ||
|
||
this.socket.on("message", (data, info) => { | ||
try { | ||
const obj = JSON.parse(data); | ||
const metric = new Metric(obj); | ||
this.emit("message", metric); | ||
} catch (err) { | ||
this.log.error(`Recieved non metric object from ${info.address}:${info.port}`); | ||
} | ||
}); | ||
|
||
this.socket.on("listening", () => { | ||
const info = this.socket.address(); | ||
this.log.info(`Daemon listening on UDP on ${info.address}:${info.port}`); | ||
}); | ||
} | ||
|
||
get [Symbol.toStringTag]() { | ||
return "MetricsDaemonUdpd"; | ||
} | ||
|
||
listen() { | ||
this.socket.bind(this.port, this.address); | ||
} | ||
|
||
destroy() { | ||
this.socket.close(); | ||
} | ||
}; | ||
|
||
module.exports = MetricsDaemonUdpd; |
Oops, something went wrong.