-
Notifications
You must be signed in to change notification settings - Fork 3
/
nesting.js
132 lines (120 loc) · 3.68 KB
/
nesting.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* In this example we show a possible way to nest multiple instances of the plugin using the
* fastify plugin system and the `routes.prefix` setting of the plugin.
*
* Launch the example
* $ node ./examples/nesting.js
*
* Then make a request to the `/with-metrics` enpoints to see the request stats.
*
* $ curl localhost:3000/fastify-prefix-1/with-metrics
* $ curl -X POST -d '{ "test": true }' -H 'content-type: application/json' localhost:3000/fastify-prefix-1/with-metrics
*
* $ curl localhost:3000/fastify-prefix-2/with-metrics
* $ curl -X POST -d '{ "test": true }' -H 'content-type: application/json' localhost:3000/fastify-prefix-2/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: 'nesting_upd_test',
},
routes: false,
}).then(() => {
app.register(
async function (f) {
// Here we re-use the client already instantiated and track only
// the routes metrics.
f.register(plugin, {
client: app.metrics.client,
routes: {
prefix: 'my-prefix-1',
},
health: false,
});
f.get(
'/with-metrics',
{
config: {
metrics: {
routeId: 'getMetrics',
},
},
},
async function () {
return { metrics: true };
}
);
f.post(
'/with-metrics',
{
config: {
metrics: {
routeId: 'postMetrics',
},
},
},
async function () {
return { metrics: true };
}
);
f.get('/no-metrics', async function () {
return { metrics: false };
});
},
{ prefix: 'fastify-prefix-1' }
);
app.register(
async function (f) {
// Here we re-use the client already instantiated and track only
// the routes metrics.
f.register(plugin, {
client: app.metrics.client,
routes: {
prefix: 'my-prefix-2',
},
health: false,
});
f.get(
'/with-metrics',
{
config: {
metrics: {
routeId: 'getMetrics',
},
},
},
async function () {
return { metrics: true };
}
);
f.post(
'/with-metrics',
{
config: {
metrics: {
routeId: 'postMetrics',
},
},
},
async function () {
return { metrics: true };
}
);
f.get('/no-metrics', async function () {
return { metrics: false };
});
},
{ prefix: 'fastify-prefix-2' }
);
});
start(app, mock, fastifyPort, udpPort);