-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
164 lines (137 loc) · 3.9 KB
/
index.js
File metadata and controls
164 lines (137 loc) · 3.9 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
let promClient; let
metrics;
function byteLen(payload) {
try {
// NOTE: this could be extended to all types that directly have a .byteLength property.
// See documentation for Buffer.byteLength() for more info.
if (payload instanceof Buffer) {
return Buffer.byteLength(payload);
}
if (typeof payload === 'string') {
return Buffer.byteLength(payload, 'utf-8');
}
return Buffer.byteLength(JSON.stringify(payload), 'utf-8');
} catch (e) {
return 0;
}
}
function beforeHook(obj, methods, hook) {
if (!obj) {
return false;
}
if (!Array.isArray(methods)) {
methods = [methods];
}
methods.forEach((meth) => {
const orig = obj[meth];
if (!orig) {
return;
}
obj[meth] = function() {
try {
hook(arguments);
} catch (e) {
console.error(e);
}
return orig.apply(this, arguments);
};
});
}
function initializeMetrics() {
const { Counter, Gauge } = promClient;
return {
connectedSockets: new Gauge({
name: 'socket_io_connected',
help: 'Number of currently connected sockets',
}),
connectTotal: new Counter({
name: 'socket_io_connect_total',
help: 'Total count of socket.io connection requests',
}),
disconnectTotal: new Counter({
name: 'socket_io_disconnect_total',
help: 'Total count of socket.io disconnections',
}),
eventsReceivedTotal: new Counter({
name: 'socket_io_events_received_total',
help: 'Total count of socket.io recieved events',
labelNames: ['event'],
}),
eventsSentTotal: new Counter({
name: 'socket_io_events_sent_total',
help: 'Total count of socket.io sent events',
labelNames: ['event'],
}),
bytesReceived: new Counter({
name: 'socket_io_recieve_bytes',
help: 'Total socket.io bytes recieved',
labelNames: ['event'],
}),
bytesTransmitted: new Counter({
name: 'socket_io_transmit_bytes',
help: 'Total socket.io bytes transmitted',
labelNames: ['event'],
}),
};
}
function collectMetrics(io) {
if (metrics == null) {
metrics = initializeMetrics();
}
const { connectedSockets } = metrics;
const { connectTotal } = metrics;
const { disconnectTotal } = metrics;
const { eventsReceivedTotal } = metrics;
const { eventsSentTotal } = metrics;
const { bytesReceived } = metrics;
const { bytesTransmitted } = metrics;
// listen to connect events
io.on('connect', (socket) => {
connectTotal.inc();
connectedSockets.inc();
socket.on('disconnect', () => {
connectedSockets.dec();
disconnectTotal.inc();
});
// Sent events
beforeHook(socket, 'emit', ([event, eventStr]) => {
// ignore internal events
if (event === 'newListener') {
return;
}
bytesTransmitted.labels(event).inc(byteLen(eventStr));
eventsSentTotal.labels(event).inc();
});
// Recieved events
beforeHook(socket, ['addListener', 'on'], (args) => {
const event = args[0];
const cbPos = args.length - 1;
// ignore internal events
if (event === 'disconnect') {
return;
}
// get original callback function
const origCb = (typeof args[cbPos] === 'function') ? args[cbPos] : undefined;
if (!origCb) {
return false;
}
args[cbPos] = function() {
const eventStr = Array.prototype.slice.call(arguments)[0];
bytesReceived.labels(event).inc(byteLen(eventStr));
eventsReceivedTotal.labels(event).inc();
return origCb.apply(this, arguments);
};
});
});
}
module.exports = function() {
try {
promClient = require('prom-client');
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
return console.error('`prom-client` module not installed, socket.io metrics will not be collected.');
}
throw e;
}
return collectMetrics.apply(null, Array.from(arguments));
};