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

v0.0.3-alpha.4

Published

Prometheus metrics tracker for socket.io servers.

Downloads

31

Readme

socket.io-prometheus-tracker

npm version build status npm downloads

Track metrics for a Socket.IO server for use in Prometheus.

Note that by its nature this library relies on monkey-patching the socket.io server instance to hook into functions like socket.emit. Bear this in mind if you're applying any monkey-patches of your own, and please read the source code to ensure compatibility.

Library aims:

  • Cover all Socket.IO events in the default namespace, sent or received.
  • Just track metrics. You can do what you want with them - serve via express, periodically write to a file, whatever.

Useful links:

Contents

  1. Intro
  2. Configuration
  3. Metrics
  4. Examples
  5. Contributing
  6. Feedback and Support

Configuration

When instantiating SocketIOPrometheusTracker, you can optionally pass a configuration object. Available parameters are listed below:

Option | Default Value | Summary :--- | :--- | :--- collectDefaultMetrics | false | Should the library also collect metrics recommended by Prometheus. Note that this will increase the number of exposed metrics beyond those listed in Metrics. See: https://github.com/siimon/prom-client#default-metrics for more details. prometheusClient | undefined | An instance of prom-client the library uses to determine the registry for storing metrics. If not provided, the default registry will be used. trackSocketId | false | Should a label be included for additionally tracking the socket id where appropriate. This may be useful for debugging, but is not recommended for use in production. This will lead to the size of the registry growing indefinitely over time. Events affected by this value are noted in Metrics.

Metrics

Name | Labels | Summary :--- | :--- | :--- socketio_bytes_received_total | ['event', 'socketid*'] | Total number of bytes received by the server. socketio_bytes_sent_total | ['event'] | Total number of bytes sent by the server. socketio_connects_current | [] | Current number of clients connected to the server. socketio_connects_length | ['socketid*'] | The length in seconds of connections to the server. socketio_connects_total | [] | Total number of connections that have been made to the server. socketio_disconnects_total | [] | Total number of disconnections from the server. socketio_events_received_total | ['event', 'socketid*'] | Total number of events received by the server. socketio_events_sent_total | ['event'] | Total number of events sent by the server.

* Field socketid will only be included if configuration option trackSocketId is set to true.

Things to be aware of

Namespaces

Events emitted outbound via the Server instance should be tracked successfully regardless of the namespace used. Events emitted outbound via a Socket instance are currently only tracked in the default namespace.

The library exports all functions used, so feel free to setup hooks against any custom namespaces you want on top of the default behavior.

Examples

Basic Use

The tracker can be instantiated by passing an instance of a Socket.IO server.

const ioPrometheus = new SocketIOPrometheusTracker(io);

Metrics can then be accessed using: ioPrometheus.register.metrics();

Configuring Instances

const ioPrometheus = new SocketIOPrometheusTracker(io, {
  collectDefaultMetrics: true,
  trackSocketId: true,
});

Express Application

An example project using the library can be found in ./examples/express.

Contributing

While contributions are appreciated, they may be rejected if not in line with the intended project direction. It's recommended to check before undertaking a major piece of work and/or refactoring.

Contributions should be based off the develop branch, and any pull requests made into develop.

Pull requests should include a corresponding entry in CHANGELOG.md.

Useful Snippets

// Install dependencies
npm ci

// Run tests
npm run lint
npm run test

// Build the library
npm run build

Feedback and Support

For suggestions, issues, and/or support raise a GitHub issue!