Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ app.get('/metrics', (req, res) => {

> all metrics have `socket_io_` prefix in their names.

| Name | Help | Labels |
| --------------------------------- | ---------------------------------------------| ------- |
| `socket_io_connected` | Number of currently connected sockets | |
| `socket_io_connect_total` | Total count of socket.io connection requests | |
| `socket_io_disconnect_total` | Total count of socket.io disconnections | |
| `socket_io_events_received_total` | Total count of socket.io recieved events | `event` |
| `socket_io_events_sent_total` | Total count of socket.io sent events | `event` |
| `socket_io_recieve_bytes` | Total socket.io bytes recieved | `event` |
| `socket_io_transmit_bytes` | Total socket.io bytes transmitted | `event` |
| Name | Help | Labels |
|-----------------------------------|----------------------------------------------|----------------------|
| `socket_io_connected` | Number of currently connected sockets | `namespace` |
| `socket_io_connect_total` | Total count of socket.io connection requests | `namespace` |
| `socket_io_disconnect_total` | Total count of socket.io disconnections | `namespace` |
| `socket_io_events_received_total` | Total count of socket.io recieved events | `namespace`, `event` |
| `socket_io_events_sent_total` | Total count of socket.io sent events | `namespace`, `event` |
| `socket_io_recieve_bytes` | Total socket.io bytes recieved | `namespace`, `event` |
| `socket_io_transmit_bytes` | Total socket.io bytes transmitted | `namespace`, `event` |

## License

Expand Down
37 changes: 18 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,43 @@ function initializeMetrics() {
connectedSockets: new Gauge({
name: 'socket_io_connected',
help: 'Number of currently connected sockets',
labelNames: ['namespace'],
}),

connectTotal: new Counter({
name: 'socket_io_connect_total',
help: 'Total count of socket.io connection requests',
labelNames: ['namespace'],
}),

disconnectTotal: new Counter({
name: 'socket_io_disconnect_total',
help: 'Total count of socket.io disconnections',
labelNames: ['namespace'],
}),

eventsReceivedTotal: new Counter({
name: 'socket_io_events_received_total',
help: 'Total count of socket.io recieved events',
labelNames: ['event'],
labelNames: ['event', 'namespace'],
}),

eventsSentTotal: new Counter({
name: 'socket_io_events_sent_total',
help: 'Total count of socket.io sent events',
labelNames: ['event'],
labelNames: ['event', 'namespace'],
}),

bytesReceived: new Counter({
name: 'socket_io_recieve_bytes',
help: 'Total socket.io bytes recieved',
labelNames: ['event'],
labelNames: ['event', 'namespace'],
}),

bytesTransmitted: new Counter({
name: 'socket_io_transmit_bytes',
help: 'Total socket.io bytes transmitted',
labelNames: ['event'],
labelNames: ['event', 'namespace'],
}),
};
}
Expand All @@ -93,21 +96,17 @@ function collectMetrics(io) {
metrics = initializeMetrics();
}

const { connectedSockets } = metrics;
const { connectTotal } = metrics;
const { disconnectTotal } = metrics;
const { eventsReceivedTotal } = metrics;
const { eventsSentTotal } = metrics;
const { bytesReceived } = metrics;
const { bytesTransmitted } = metrics;
const { connectedSockets, connectTotal, disconnectTotal, eventsReceivedTotal, eventsSentTotal, bytesReceived, bytesTransmitted } = metrics;

const namespace = io.name ?? null

// listen to connect events
io.on('connect', (socket) => {
connectTotal.inc();
connectedSockets.inc();
connectTotal.labels({namespace}).inc();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I tried to manually, with my eyes reading code, verify that this syntax is compatible with prom-client 10.x and I believe this is the case. I haven't tried running this to verify

connectedSockets.labels({namespace}).inc();
socket.on('disconnect', () => {
connectedSockets.dec();
disconnectTotal.inc();
connectedSockets.labels({namespace}).dec();
disconnectTotal.labels({namespace}).inc();
});

// Sent events
Expand All @@ -117,8 +116,8 @@ function collectMetrics(io) {
return;
}

bytesTransmitted.labels(event).inc(byteLen(eventStr));
eventsSentTotal.labels(event).inc();
bytesTransmitted.labels({event, namespace}).inc(byteLen(eventStr));
eventsSentTotal.labels({event, namespace}).inc();
});

// Recieved events
Expand All @@ -140,8 +139,8 @@ function collectMetrics(io) {
args[cbPos] = function() {
const eventStr = Array.prototype.slice.call(arguments)[0];

bytesReceived.labels(event).inc(byteLen(eventStr));
eventsReceivedTotal.labels(event).inc();
bytesReceived.labels({event, namespace}).inc(byteLen(eventStr));
eventsReceivedTotal.labels({event, namespace}).inc();

return origCb.apply(this, arguments);
};
Expand Down