Skip to content

Commit d8d74b1

Browse files
committedFeb 14, 2024
lift content from readmes, needs work
1 parent 9b2bfde commit d8d74b1

File tree

8 files changed

+1736
-12
lines changed

8 files changed

+1736
-12
lines changed
 

‎src/content/docs/reference/client.md

+582-1
Large diffs are not rendered by default.

‎src/content/docs/reference/collectors.md

-5
This file was deleted.

‎src/content/docs/reference/daemon.md

+124-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,128 @@
11
---
22
title: '@metrics/daemon'
3+
tableOfContents:
4+
maxHeadingLevel: 4
35
---
46

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.

‎src/content/docs/reference/emitter.md

+112-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,116 @@
11
---
22
title: '@metrics/emitter'
3+
tableOfContents:
4+
maxHeadingLevel: 4
35
---
46

5-
asdfasd
7+
Emitter for sending metrics over a network to a [daemon](../daemon).
8+
9+
## Installation
10+
11+
```bash
12+
$ npm install @metrics/emitter
13+
```
14+
15+
## Example
16+
17+
Set up an emitter which connects to a daemon on UDP on port 6000 and pipes the metrics from the [`@metrics/client`](https://github.com/metrics-js/client):
18+
19+
```js
20+
const Emitter = require('@metrics/emitter');
21+
const Client = require('@metrics/client');
22+
23+
const emitter = new Emitter('udp', { port: 6000 });
24+
const client = new Client();
25+
26+
client.pipe(emitter);
27+
```
28+
29+
## Description
30+
31+
This module makes it possible stream metrics over a socket to a daemon. The socket can
32+
be of different protocols (UDP, TCP etc) but the data trasmitted over it is expected to be of
33+
the [`@metrics/metric`](https://github.com/metrics-js/metric) type.
34+
35+
For recieving metrics over a socket one are expected to use the [`@metrics/daemon`](https://github.com/metrics-js/daemon) module.
36+
37+
The main purpose of this is to be able to send metrics from multiple processes to a sentral
38+
service (daemon) for further handling. Here is a simplified example of each worker in a
39+
cluster pushing metrics to the master and the master pushing the metric further:
40+
41+
```js
42+
const master = () => {
43+
const daemon = new Daemon('udp', { port: 6000 });
44+
const client = new Client();
45+
46+
daemon.pipe(client);
47+
48+
daemon.listen();
49+
};
50+
51+
const worker = () => {
52+
const emitter = new Emitter('udp', { port: 6000 });
53+
const client = new Client();
54+
55+
client.pipe(emitter);
56+
57+
client.metric({
58+
name: `worker_${cluster.worker.id}`,
59+
description: `Worker number: ${cluster.worker.id}`,
60+
value: 1,
61+
});
62+
};
63+
64+
const workers = [];
65+
66+
if (cluster.isMaster) {
67+
for (let i = 0; i < (os.cpus().length - 1); i++) {
68+
workers.push(cluster.fork());
69+
}
70+
master();
71+
}
72+
73+
if (cluster.isWorker) {
74+
worker();
75+
}
76+
```
77+
78+
See the [cluster example](https://github.com/metrics-js/daemon/tree/master/example/cluster.js)
79+
in daemon for a full example.
80+
81+
## Constructor
82+
83+
Create a new Emitter instance.
84+
85+
```js
86+
const Emitter = require('@metrics/emitter');
87+
const emitter = new Emitter(transport, options);
88+
```
89+
90+
### transport (required)
91+
92+
What type of transport to use. Supported values are:
93+
94+
* `udp` - For UDP transport.
95+
96+
### options (optional)
97+
98+
An Object containing misc configuration for the selected transport. Please see each
99+
transport for which option which can be passed in.
100+
101+
### returns
102+
103+
Returns a Writable stream in object mode.
104+
105+
## Transports
106+
107+
The following transports is supported:
108+
109+
### UDP
110+
111+
UDP as a transport can be enabled by providing `udp` to the transport argument on the
112+
Emitter constructor. The UDP transport takes the following options (passed to the options
113+
argument on the Emitter constructor):
114+
115+
* **port** - `Number` - The port of the daemon. Default: 40400.
116+
* **address** - `String` - The address of the daemon. Default: localhost.

0 commit comments

Comments
 (0)
Please sign in to comment.