/
configuration.ts
147 lines (127 loc) 路 4.2 KB
/
configuration.ts
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
import {
App,
Configuration,
Inject,
MidwayFrameworkType,
} from '@midwayjs/core';
import { Application as SocketApplication } from '@midwayjs/socketio';
import { DataService } from '@midwayjs/prometheus';
import * as prometheus from '@midwayjs/prometheus';
@Configuration({
imports: [prometheus],
namespace: 'prometheus-socket-io',
})
export class PrometheusSocketIOConfiguration {
@App(MidwayFrameworkType.WS_IO)
socketApp: SocketApplication;
@Inject()
dataService: DataService;
async onReady() {
const dataToBytes = (data: any) => {
try {
return Buffer.byteLength(
typeof data === 'string' ? data : JSON.stringify(data) || '',
'utf8'
);
} catch (e) {
return 0;
}
};
this.dataService.define('connectedSockets', 'Gauge', {
name: 'socket_io_connected',
help: 'Number of currently connected sockets',
});
this.dataService.define('connectTotal', 'Counter', {
name: 'socket_io_connect_total',
help: 'Total count of socket.io connection requests',
labelNames: ['namespace'],
});
this.dataService.define('disconnectTotal', 'Counter', {
name: 'socket_io_disconnect_total',
help: 'Total count of socket.io disconnections',
labelNames: ['namespace'],
});
this.dataService.define('bytesTransmitted', 'Counter', {
name: 'socket_io_transmit_bytes',
help: 'Total socket.io bytes transmitted',
labelNames: ['event', 'namespace'],
});
this.dataService.define('eventsSentTotal', 'Counter', {
name: 'socket_io_events_sent_total',
help: 'Total count of socket.io sent events',
labelNames: ['event', 'namespace'],
});
this.dataService.define('errorsTotal', 'Counter', {
name: 'socket_io_errors_total',
help: 'Total socket.io errors',
labelNames: ['namespace'],
});
this.dataService.define('bytesReceived', 'Counter', {
name: 'socket_io_receive_bytes',
help: 'Total socket.io bytes received',
labelNames: ['event', 'namespace'],
});
this.dataService.define('eventsReceivedTotal', 'Counter', {
name: 'socket_io_events_received_total',
help: 'Total count of socket.io received events',
labelNames: ['event', 'namespace'],
});
const labels = { namespace: '/' };
const blacklisted_events = new Set([
'error',
'connect',
'disconnect',
'disconnecting',
'newListener',
'removeListener',
]);
const disconnect = socket => {
if ((socket as any).midwayConnect) {
this.dataService.setDiff('connectedSockets', -1);
(socket as any).midwayConnect = false;
}
};
this.socketApp.on('connection', async socket => {
(socket as any).midwayConnect = true;
this.dataService.inc('connectTotal', labels);
this.dataService.setDiff('connectedSockets', 1);
socket.on('disconnect', () => {
this.dataService.inc('disconnectTotal', labels);
disconnect(socket);
});
const org_emit = socket.emit;
socket.emit = (event: string, ...data: any[]) => {
if (!blacklisted_events.has(event)) {
const labelsWithEvent = { event: event, ...labels };
this.dataService.inc(
'bytesTransmitted',
labelsWithEvent,
dataToBytes(data)
);
this.dataService.inc('eventsSentTotal', labelsWithEvent);
}
return org_emit.apply(socket, [event, ...data]);
};
const org_onevent = (socket as any).onevent;
(socket as any).onevent = (packet: any) => {
if (packet && packet.data) {
const [event, data] = packet.data;
if (event === 'error') {
disconnect(socket);
this.dataService.inc('errorsTotal', labels);
} else if (!blacklisted_events.has(event)) {
const labelsWithEvent = { event: event, ...labels };
this.dataService.inc(
'bytesReceived',
labelsWithEvent,
dataToBytes(data)
);
this.dataService.inc('eventsReceivedTotal', labelsWithEvent);
}
}
return org_onevent.call(socket, packet);
};
});
}
async onStop() {}
}