|
1 | 1 | ---
|
2 | 2 | title: '@metrics/daemon'
|
| 3 | +tableOfContents: |
| 4 | + maxHeadingLevel: 4 |
3 | 5 | ---
|
4 | 6 |
|
5 |
| -asdfasd |
| 7 | +Daemon for collecting metrics over a network. Provides a stream for further piping of metrics. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +$ npm install @metrics/daemon |
| 13 | +``` |
| 14 | + |
| 15 | +## Example |
| 16 | + |
| 17 | +Set up a daemon with UDP on port 6000 as transport and pipes the incomming metrics into the [`@metrics/client`](https://github.com/metrics-js/client): |
| 18 | + |
| 19 | +```js |
| 20 | +const Daemon = require('@metrics/daemon'); |
| 21 | +const Client = require('@metrics/client'); |
| 22 | + |
| 23 | +const daemon = new Daemon('udp', { port: 6000 }); |
| 24 | +const client = new Client(); |
| 25 | + |
| 26 | +daemon.pipe(client); |
| 27 | + |
| 28 | +daemon.listen(); |
| 29 | +``` |
| 30 | + |
| 31 | +## Description |
| 32 | + |
| 33 | +This module makes it possible to create a socket one can recieve metrics over. The socket can |
| 34 | +be of different protocols (UDP, TCP etc) but the data trasmitted over it is expected to be of |
| 35 | +the [`@metrics/metric`](https://github.com/metrics-js/metric) type. The recieved metrics can |
| 36 | +be piped to other metric modules for further handling. |
| 37 | + |
| 38 | +For sending metrics over a socket one are expected to use the [`@metrics/emitter`](https://github.com/metrics-js/emitter) module. |
| 39 | + |
| 40 | +The main purpose of this is to be able to collect metrics from multiple processes. Here is a |
| 41 | +simplified example of each worker in a cluster pushing metrics to the master and the master |
| 42 | +pushing the metric further: |
| 43 | + |
| 44 | +```js |
| 45 | +const master = () => { |
| 46 | + const daemon = new Daemon('udp', { port: 6000 }); |
| 47 | + const client = new Client(); |
| 48 | + |
| 49 | + daemon.pipe(client); |
| 50 | + |
| 51 | + daemon.listen(); |
| 52 | +}; |
| 53 | + |
| 54 | +const worker = () => { |
| 55 | + const emitter = new Emitter('udp', { port: 6000 }); |
| 56 | + const client = new Client(); |
| 57 | + |
| 58 | + client.pipe(emitter); |
| 59 | + |
| 60 | + client.metric({ |
| 61 | + name: `worker_${cluster.worker.id}`, |
| 62 | + description: `Worker number: ${cluster.worker.id}`, |
| 63 | + value: 1, |
| 64 | + }); |
| 65 | +}; |
| 66 | + |
| 67 | +const workers = []; |
| 68 | + |
| 69 | +if (cluster.isMaster) { |
| 70 | + for (let i = 0; i < (os.cpus().length - 1); i++) { |
| 71 | + workers.push(cluster.fork()); |
| 72 | + } |
| 73 | + master(); |
| 74 | +} |
| 75 | + |
| 76 | +if (cluster.isWorker) { |
| 77 | + worker(); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +See the [cluster example](https://github.com/metrics-js/daemon/tree/master/example/cluster.js) |
| 82 | +in examples for a full example. |
| 83 | + |
| 84 | +## Constructor |
| 85 | + |
| 86 | +Create a new Daemon instance. |
| 87 | + |
| 88 | +```js |
| 89 | +const Daemon = require('@metrics/daemon'); |
| 90 | +const daemon = new Daemon(transport, options); |
| 91 | +``` |
| 92 | + |
| 93 | +### transport (required) |
| 94 | + |
| 95 | +What type of transport to use. Supported values are: |
| 96 | + |
| 97 | + * `udp` - For UDP transport. |
| 98 | + |
| 99 | +### options (optional) |
| 100 | + |
| 101 | +An Object containing misc configuration for the selected transport. Please see each |
| 102 | +transport for which option which can be passed in. |
| 103 | + |
| 104 | +### returns |
| 105 | + |
| 106 | +Returns a Readable stream in object mode. |
| 107 | + |
| 108 | +## API |
| 109 | + |
| 110 | +The Daemon instance has the following API: |
| 111 | + |
| 112 | +### .listen() |
| 113 | + |
| 114 | +Starts the daemon with the selected transport. |
| 115 | + |
| 116 | +## Transports |
| 117 | + |
| 118 | +The following transports is supported: |
| 119 | + |
| 120 | +### UDP |
| 121 | + |
| 122 | +UDP as a transport can be enabled by providing `udp` to the transport argument on the |
| 123 | +Daemon constructor. The UDP transport takes the following options (passed to the options |
| 124 | +argument on the Daemon constructor): |
| 125 | + |
| 126 | + * **port** - `Number` - The port to bind to. Default: 40400. |
| 127 | + * **address** - `String` - The address to bind to. Default: localhost. |
| 128 | + * **logger** - `Function` - Any logger that implements the methods `trace`, `debug`, `info`, `warn`, `error` and `fatal`. Under the hood [abslog](https://www.npmjs.com/package/abslog) is used. See that module for further information. If no logger is passed in, nothing will be logged. |
0 commit comments