Skip to content
Draft

V2 #10

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
17 changes: 17 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
env:
browser: true
es2021: true
extends:
- 'eslint:recommended'
- 'plugin:@typescript-eslint/recommended'
parser: '@typescript-eslint/parser'
parserOptions:
ecmaVersion: 12
sourceType: module
plugins:
- '@typescript-eslint'
rules:
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/explicit-module-boundary-types": "off"
ignorePatterns:
- "dist"
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @naueramant
19 changes: 19 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: CI

on: [pull_request]

jobs:
CI:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: install node v14
uses: actions/setup-node@v1
with:
node-version: 14
- name: yarn install
run: yarn install --frozen-lockfile
- name: yarn lint
run: yarn lint
- name: yarn build
run: yarn build
73 changes: 38 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,65 @@ yarn add socket.io-prometheus-metrics
Basic usage

```ts
import * as http from 'http';
import * as io from 'socket.io';
import * as prometheus from 'socket.io-prometheus-metrics';
import * as http from "http";
import * as io from "socket.io";
import * as prom from "socket.io-prometheus-metrics";

const server = http.createServer();
const io = io(server);

prometheus.metrics(io);
prom.collect(io);

server.listen(3000);
```

Metrics is then available at `localhost:9090/metrics`.

Prometheus default metrics can also be enabled by setting the `collectDefaultMetrics` option to `true`

```ts
prometheus.metrics(io, {
collectDefaultMetrics: true
prom.collect(io, {
collectDefaultMetrics: true,
});
```

If you wish to serve the metrics yourself the `createServer` options can be set to `false` and metrics can be collected from the register

```ts
const ioMetrics = prometheus.metrics(io, {
createServer: false
const collector = prom.collect(io, {
createServer: false,
});

const metrics = ioMetrics.register.metrics();
const metrics = collector.getMetrics();
```

## Options

| Option | Default | Description |
| ------------------------- | --------------| ------------------------------------------------------------ |
| `path` | "/metrics" | Metrics path |
| `port` | 9090 | Metrics port |
| `createServer` | true | Auto create http server |
| `collectDefaultMetrics` | false | Collect prometheus default metrics |
| `checkForNewNamespaces` | true | Collect metrics for namespaces that will be added at runtime |
| Option | Default | Description |
| ----------------------- | ---------- | ------------------------------------------------------------ |
| `path` | "/metrics" | Metrics path |
| `port` | 9090 | Metrics port |
| `createServer` | true | Auto create http server |
| `collectDefaultMetrics` | false | Collect prometheus default metrics |
| `checkForNewNamespaces` | true | Collect metrics for namespaces that will be added at runtime |

## Socket.io metrics

> all metrics have `socket_io_` prefix in their names.
> all metrics have `socket_io_` as 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 | `namespace` |
| `socket_io_disconnect_total` | Total count of socket.io disconnections | `namespace` |
| `socket_io_errors_total` | Total count of socket.io errors | `namespace` |
| `socket_io_events_received_total` | Total count of socket.io received events | `event`, `namespace` |
| `socket_io_events_sent_total` | Total count of socket.io sent events | `event`, `namespace` |
| `socket_io_receive_bytes` | Total socket.io bytes received | `event`, `namespace` |
| `socket_io_transmit_bytes` | Total socket.io bytes transmitted | `event`, `namespace` |
| Name | Help | Labels |
| --------------------------- | -------------------------------------------- | -------------------- |
| `socket_io_connected` | Number of currently connected sockets | |
| `socket_io_connects` | Total count of socket.io connection requests | `namespace` |
| `socket_io_disconnects` | Total count of socket.io disconnections | `namespace` |
| `socket_io_errors` | Total count of socket.io errors | `namespace` |
| `socket_io_events_received` | Total count of socket.io received events | `event`, `namespace` |
| `socket_io_events_sent` | Total count of socket.io sent events | `event`, `namespace` |
| `socket_io_bytes_received` | Total socket.io bytes received | `event`, `namespace` |
| `socket_io_bytes_sent` | Total socket.io bytes sent | `event`, `namespace` |

## Prometheus default metrics

> available if `collectDefaultMetrics` is set to `true`

More information [here](https://github.com/siimon/prom-client#default-metrics) and [here](https://prometheus.io/docs/instrumenting/writing_clientlibs/#standard-and-runtime-collectors).
Expand All @@ -79,23 +82,23 @@ More information [here](https://github.com/siimon/prom-client#default-metrics) a

Default namespace has label value of `'/'`.

By default library checks `io.nsps` variable for new namespaces to collect metrics from. This check occurs every 2 seconds.
By default library checks `io.nsps` variable for new namespaces to collect metrics from. This check occurs every 2 seconds.

You can disable this functionality by providing `checkForNewNamespaces` option with `false` value.
For example:

```ts
prometheus.metrics(io, {
checkForNewNamespaces: false
prom.collect(io, {
checkForNewNamespaces: false,
});
```

With this functionality disabled, library will only collect metrics from namespaces that
were available at the moment of call to `prometheus.metrics(io, ...)`,
default namespace is included.
With this functionality disabled, library will only collect metrics from namespaces that
were available at the moment of call to `prometheus.metrics(io, ...)`,
default namespace is included.

More information about socket.io namespaces [here](https://socket.io/docs/rooms-and-namespaces).
More information about socket.io namespaces [here](https://socket.io/docs/rooms-and-namespaces).

## License

Licensed under the Apache 2.0 License. See the LICENSE file for details.
Licensed under the Apache 2.0 License. See the LICENSE file for details.
30 changes: 30 additions & 0 deletions examples/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import express from "express";
import { createServer } from "http";
import * as sio from "socket.io";

import { collectSocketIOMetrics } from "../src";

// Initialize express and socket.io

const app = express();
const server = createServer(app);
const io = sio(server);

// Initialize the Socket.IO metrics collector

const register = collectSocketIOMetrics({ io });

// Start the express server

app.get("/metrics", async (req, res) => {
try {
res.set("Content-Type", register.contentType);
res.end(await register.metrics());
} catch (ex) {
res.status(500).end(ex);
}
});

console.log("Metrics exposed on localhost:3000/metrics");

server.listen(3000);
Loading