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

lorem-performance-radiator

v1.0.2

Published

Here's your updated README based on the provided code changes:

Downloads

198

Readme

Here's your updated README based on the provided code changes:


Performance Radiator

performance-radiator is a Node.js package for monitoring and exposing critical application performance metrics. Built using Prometheus's prom-client, it offers a flexible solution for developers to track application health, manage rate limits, and customize metrics as needed.


Features

  • Core Metrics: Tracks essential performance indicators such as HTTP requests, errors, concurrent users, and rate-limited requests.
  • Dynamic Metrics: Allows developers to add counters, gauges, and histograms at runtime.
  • Rate Limiting: Includes configurable rate-limiting middleware to manage incoming traffic.
  • Prometheus Integration: Exposes metrics in a Prometheus-compatible format.
  • Easy Integration: Quickly set up and monitor your Express-based applications.

Installation

npm install performance-radiator

Usage

Initialization

  1. Set Up Middleware
    Integrate performance-radiator into your Express app:

    const express = require("express");
    const createPerformanceMonitor = require("performance-radiator");
    
    const app = express();
    
    const {
      router: metricsRouter,
      applyRateLimiter,
      httpRequestsTotal,
    } = createPerformanceMonitor({
      sequelize: require("./models").sequelize, // Optional Sequelize integration
      rateLimitOptions: { windowMs: 60000, max: 100 }, // Rate limit config
      customMetrics: [
        {
          type: "counter",
          name: "custom_event_count",
          help: "Custom metric example",
        },
      ],
    });
    
    // Apply rate limiter globally or to specific routes
    applyRateLimiter(app, "/");
    
    // Attach metrics endpoint
    app.use("/metrics", metricsRouter);
    
    app.listen(3000, () => console.log("Server running on port 3000"));

Adding Custom Metrics

Add metrics dynamically via the customMetrics option or programmatically.

const { httpRequestsTotal } = require("performance-radiator");

// Add a custom counter metric
httpRequestsTotal.inc({ method: "GET", route: "/", status: 200 });

Built-In Metrics

The package provides these default metrics:

HTTP Metrics

  • http_requests_total: Total HTTP requests (labels: method, route, status).
  • http_errors_total: Total HTTP errors (labels: method, route).
  • http_request_throughput: Current request throughput.
  • http_rate_limit_total: Requests blocked by rate limiter (labels: route).

User Metrics

  • http_concurrent_users: Number of concurrent users.

Database Metrics (via Sequelize, if configured)

  • Metrics for query durations, active connections, and transactions.

Rate Limiting

Use rateLimitOptions to configure rate limits.

applyRateLimiter(app, "/api", { windowMs: 60000, max: 50 });

Default options:

  • windowMs: Time window in milliseconds.
  • max: Max requests per window.

Prometheus Integration

Expose the /metrics endpoint for Prometheus to scrape metrics. Add this configuration to Prometheus:

scrape_configs:
  - job_name: 'my_app'
    static_configs:
      - targets: ['localhost:3000']

Full Example

const express = require("express");
const createPerformanceMonitor = require("performance-radiator");
const sequelize = require("./models").sequelize;

const app = express();

const { router: metricsRouter, applyRateLimiter } = createPerformanceMonitor({
  sequelize,
  rateLimitOptions: { windowMs: 60000, max: 100 },
  customMetrics: [
    {
      type: "gauge",
      name: "active_sessions",
      help: "Tracks active user sessions",
    },
  ],
});

applyRateLimiter(app);
app.use("/metrics", metricsRouter);

app.get("/", (req, res) => {
  res.send("Welcome to Performance Radiator!");
});

app.listen(3000, () => console.log("Server running on port 3000"));

License

This project is licensed under the MIT License.


This updated README includes key changes in functionality and maintains clarity for users integrating performance-radiator.