-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdefault_udp.js
67 lines (58 loc) · 1.47 KB
/
default_udp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* In this example the default metrics are enabled using a udp connection.
*
* Launch the example
* $ node ./examples/default_udp.js
*
* Then make a request to the `/with-metrics` enpoint to see the request stats.
*
* $ curl localhost:3000/with-metrics
* $ curl -X POST -d '{ "test": true }' -H 'content-type: application/json' localhost:3000/with-metrics
*/
'use strict';
const fastify = require('fastify');
const plugin = require('..');
const { UdpMock, dumpMetric } = require('./fixtures/statsd');
const { start } = require('./util');
const udpPort = 10000;
const fastifyPort = 3000;
const mock = new UdpMock();
mock.on('metric', dumpMetric);
const app = fastify();
app.register(plugin, {
client: {
host: `udp://127.0.0.1:${udpPort}`,
namespace: 'default_upd_test',
},
}).then(() => {
app.get(
'/with-metrics',
{
config: {
metrics: {
routeId: 'getMetrics',
},
},
},
async function () {
return { metrics: true };
}
);
app.post(
'/with-metrics',
{
config: {
metrics: {
routeId: 'postMetrics',
},
},
},
async function () {
return { metrics: true };
}
);
app.get('/no-metrics', async function () {
return { metrics: false };
});
});
start(app, mock, fastifyPort, udpPort);