0.5.0 • Published 6 months ago

@metriq/express v0.5.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

@metriq/express

npm version License

Express middleware adapter for Metriq metrics library.

📦 Installation

npm install @metriq/express

🚀 Quick Start

import express from "express";
import { metriq } from "metriq";
import { prometheus } from "@metriq/express";

// Initialize metrics
const metrics = metriq();

// Create a counter
metrics.createCounter("http_requests_total", "Total HTTP requests");

// Create Express app
const app = express();

// Add metrics endpoint
app.get("/metrics", prometheus(metrics));

app.listen(3000);

🔧 Request Monitoring Example

import express from "express";
import { metriq } from "metriq";
import { prometheus } from "@metriq/express";

const app = express();
const metrics = metriq();

// Create metrics
const requests = metrics.createCounter<{
    method: string;
    path: string;
    status: string;
}>("http_requests_total", "Total HTTP requests");

const latency = metrics.createHistogram<{
    method: string;
    path: string;
}>("http_request_duration_seconds", "Request duration in seconds", {
    buckets: [0.1, 0.5, 1, 2, 5],
});

// Middleware to track requests
app.use((req, res, next) => {
    const start = process.hrtime();

    res.on("finish", () => {
        const [seconds, nanoseconds] = process.hrtime(start);
        const duration = seconds + nanoseconds / 1e9;

        const labels = {
            method: req.method,
            path: req.route?.path ?? "unknown",
            status: res.statusCode.toString(),
        };

        requests.increment(labels);
        latency.observe(
            {
                method: labels.method,
                path: labels.path,
            },
            duration,
        );
    });

    next();
});

// Metrics endpoint
app.get("/metrics", prometheus(metrics));

app.listen(3000);
0.5.0

6 months ago

0.4.0

6 months ago

0.3.1

6 months ago

0.3.0

8 months ago

0.2.2

9 months ago

0.2.1

9 months ago