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
Set Up Middleware
Integrateperformance-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
.