Prometheus मेट्रिक्स TypeScript में: पूर्ण गाइड
Prometheus एक ओपन-सोर्स मॉनिटरिंग और अलर्टिंग सिस्टम है जो आपके ऐप से रियल-टाइम में मेट्रिक्स इकट्ठा करता है। इस लेख में हम सीखेंगे कि कैसे prom-client लाइब्रेरी का उपयोग करके TypeScript ऐप में Prometheus मेट्रिक्स को इंटीग्रेट किया जाए। आप जानेंगे कि मेट्रिक्स कैसे बनाएं, रजिस्टर करें, एक्सपोर्ट करें और डेटा कलेक्शन के लिए एंडपॉइंट सेटअप करें।
Prometheus मेट्रिक्स क्या हैं?
Prometheus पुल-मॉडल पर काम करता है: यह आपके ऐप के एंडपॉइंट को समय-समय पर scrape करके मेट्रिक्स लेता है। मेट्रिक्स मुख्य रूप से चार प्रकार के होते हैं:
- Counter (काउंटर) — केवल बढ़ता है (जैसे, कुल रिक्वेस्ट की संख्या)।
- Gauge (गेज) — बढ़ और घट सकता है (जैसे, एक्टिव यूज़र्स की वर्तमान संख्या)।
- Histogram (हिस्टोग्राम) — वैल्यू के वितरण को मापता है (जैसे, रिस्पॉन्स टाइम)।
- Summary (समरी) — हिस्टोग्राम जैसा, लेकिन क्लाइंट साइड पर क्वांटाइल कैलकुलेट करता है।
TypeScript में prom-client इंस्टॉल करना
TypeScript में Prometheus के लिए हम आधिकारिक prom-client लाइब्रेरी का उपयोग करेंगे। इसे npm से इंस्टॉल करें:
npm install prom-client
TypeScript के लिए टाइप्स भी इंस्टॉल करें:
npm install @types/prom-client --save-dev
मेट्रिक्स बनाना और रजिस्टर करना
आइए एक बेसिक उदाहरण देखें। metrics.ts फ़ाइल बनाएं:
import client from 'prom-client';
// मेट्रिक्स के लिए रजिस्टर बनाएं (डिफ़ॉल्ट ग्लोबल रजिस्टर का उपयोग होता है)
const register = new client.Registry();
// डिफ़ॉल्ट मेट्रिक्स जोड़ें (जैसे, मेमोरी, CPU)
client.collectDefaultMetrics({ register });
// रिक्वेस्ट गिनने के लिए Counter बनाएं
const httpRequestsTotal = new client.Counter({
name: 'http_requests_total',
help: 'HTTP रिक्वेस्ट की कुल संख्या',
labelNames: ['method', 'status', 'path'],
registers: [register],
});
// एक्टिव कनेक्शन के लिए Gauge बनाएं
const activeConnections = new client.Gauge({
name: 'active_connections',
help: 'एक्टिव कनेक्शन की संख्या',
registers: [register],
});
// रिस्पॉन्स टाइम के लिए Histogram बनाएं
const httpRequestDuration = new client.Histogram({
name: 'http_request_duration_seconds',
help: 'HTTP रिक्वेस्ट की अवधि सेकंड में',
labelNames: ['method', 'path'],
buckets: [0.01, 0.05, 0.1, 0.5, 1, 5],
registers: [register],
});
export { register, httpRequestsTotal, activeConnections, httpRequestDuration };
Express ऐप में मेट्रिक्स का उपयोग
अब एक सरल Express ऐप TypeScript में बनाएं जो हमारे मेट्रिक्स का उपयोग करे:
import express from 'express';
import { register, httpRequestsTotal, activeConnections, httpRequestDuration } from './metrics';
const app = express();
const port = 3000;
// रिक्वेस्ट टाइम मापने के लिए Middleware
app.use((req, res, next) => {
const end = httpRequestDuration.startTimer({ method: req.method, path: req.path });
activeConnections.inc();
res.on('finish', () => {
httpRequestsTotal.inc({ method: req.method, status: res.statusCode.toString(), path: req.path });
activeConnections.dec();
end();
});
next();
});
// Prometheus के लिए एंडपॉइंट
app.get('/metrics', async (req, res) => {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
});
// उदाहरण एंडपॉइंट
app.get('/', (req, res) => {
res.send('नमस्ते, Prometheus!');
});
app.listen(port, () => {
console.log(`सर्वर http://localhost:${port} पर चल रहा है`);
});
चलाएं और जांचें
ऐप चलाएं:
npx ts-node app.ts
ब्राउज़र में http://localhost:3000/metrics खोलें। आपको कुछ इस तरह दिखेगा:
# HELP