npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

socket.io-prom-metrics

v1.0.14

Published

Prometheus metrics for socket.io

Downloads

418

Readme

socket.io-prom-metrics

Exposes a metrics endpoint for prometheus to collect data about socket.io.

Installation

npm install socket.io-prom-metrics

Usage

Basic usage

// server.ts

import { createServer } from "http";
import { Server, Socket } from "socket.io";
import * as prometheus from "socket.io-prom-metrics";


const server = createServer();
const io = new Server(server);


io.on("connection", (socket: Socket) => {
    console.log(`${socket.id} is connected.`)
    // Add event here
});


io.of("/custom-namespace").on("connection", (socket: Socket) => {
    console.log(`${socket.id} is connected.`)
    // Add event here
});


// init prometheus
prometheus.metrics(io); // running on port 9090 (default port) | http://localhost:9090/metrics


// start server
server.listen(3000, () => {
    console.log("prometheus running on port 9090 : http://localhost:9090/metrics");
    console.log("server running on port 3000")
});

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

const ioMetrics = prometheus.metrics(io, {
    createServer: false
});

const metrics = ioMetrics.register.metrics();

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 |

Socket.io metrics

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 | 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 |

Prometheus default metrics

available if collectDefaultMetrics is set to true

More information here and here.

Namespaces support

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.

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

prometheus.metrics(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.

More information about socket.io namespaces here.

License

Licensed under the Apache 2.0 License. See the LICENSE file for details.