Rust अनुप्रयोगों की निगरानी: मीट्रिक्स और अलर्ट A से Z तक
\n\nजब आपका Rust अनुप्रयोग प्रोडक्शन में जाता है, तो कोड सिर्फ कोड नहीं रह जाता। यह एक सेवा बन जाता है जिस पर उपयोगकर्ता और व्यवसाय निर्भर होते हैं। निगरानी के बिना, आप आँख बंद करके उड़ रहे हैं: खराबी शिकायतों से पता चलती है, और प्रदर्शन का आकलन "अंदाज़े से" किया जाता है। इस लेख में, हम Rust के लिए एक पूर्ण निगरानी प्रणाली बनाने पर चर्चा करेंगे - बुनियादी मीट्रिक्स संग्रह से लेकर Prometheus में अलर्ट सेटअप और Grafana में विज़ुअलाइज़ेशन तक।
\n\n1. Rust परियोजनाओं के लिए निगरानी क्यों महत्वपूर्ण है
\nRust प्रदर्शन और मेमोरी सुरक्षा के लिए प्रसिद्ध है, लेकिन यह एप्लिकेशन पर नज़र रखने की आवश्यकता को समाप्त नहीं करता है। प्रोडक्शन में सामान्य समस्याएँ:
\n- \n
- मेमोरी लीक (उदाहरण के लिए,
Arcया चक्रीय संदर्भों के माध्यम से)। \n - async कोड में ब्लॉकिंग (tokio में deadlocks या starvation)। \n
- अप्रत्याशित पैनिक (यहाँ तक कि
catch_unwindके साथ भी)। \n - प्रदर्शन में गिरावट गैर-इष्टतम एल्गोरिदम के कारण। \n
निगरानी प्रश्नों के उत्तर देती है: "अभी क्या हो रहा है?", "यह क्यों हुआ?" और "इसे कैसे रोका जाए?"।
\n\n2. मीट्रिक्स संग्रह: metrics लाइब्रेरी और Prometheus में निर्यात
\nRust के लिए मानक स्टैक - क्रेट metrics (या prometheus) है। हम metrics का उपयोग करते हैं क्योंकि यह निर्यात को अमूर्त करता है और आसानी से बैकएंड बदलता है।
2.1. स्थापना और बुनियादी सेटअप
\nCargo.toml में निर्भरताएँ जोड़ें:
[dependencies]\nmetrics = "0.21"\nmetrics-exporter-prometheus = "0.12"\ntokio = { version = "1", features = ["full"] }\n\n/metrics एंडपॉइंट के लिए एक सरल HTTP सर्वर बनाएँ:
use metrics_exporter_prometheus::PrometheusBuilder;\nuse std::net::SocketAddr;\nuse tokio::net::TcpListener;\n\n#[tokio::main]\nasync fn main() {\n // Prometheus एक्सपोर्टर को आरंभ करें\n let (recorder, exporter) = PrometheusBuilder::new()\n .build()\n .expect("Prometheus एक्सपोर्टर बनाने में विफल");\n\n metrics::set_boxed_recorder(Box::new(recorder))\n .expect("ग्लोबल रिकॉर्डर सेट करने में विफल");\n\n // मीट्रिक्स सर्वर शुरू करें\n let addr: SocketAddr = "0.0.0.0:9090".parse().unwrap();\n let listener = TcpListener::bind(addr).await.unwrap();\n println!("मीट्रिक्स http://{} पर उपलब्ध हैं", addr);\n\n loop {\n let (mut stream, _) = listener.accept().await.unwrap();\n let body = exporter.encode().unwrap();\n let response = format!(\n "HTTP/1.1 200 OK\\r\\nContent-Type: text/plain\\r\\nContent-Length: {}\\r\\n\\r\\n{}",\n body.len(),\n body\n );\n tokio::io::AsyncWriteExt::write_all(&mut stream, response.as_bytes())\n .await\n .unwrap();\n }\n}\n\n2.2. कस्टम मीट्रिक्स का पंजीकरण
\nएक अनुरोध काउंटर और प्रतिक्रिया समय हिस्टोग्राम जोड़ें:
\nuse metrics::{counter, histogram, increment_counter};\nuse std::time::Instant;\n\nasync fn handle_request() {\n let start = Instant::now();\n\n // आपका व्यावसायिक तर्क\n tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;\n\n // मीट्रिक्स रिकॉर्ड करें\n increment_counter!("http_requests_total", "method" => "GET");\n histogram!("http_request_duration_seconds", start.elapsed().as_secs_f64(), "method" => "GET");\n}\nअब /metrics पर आप देखेंगे:
# HELP http_requests_total Total HTTP requests\n# TYPE http_requests_total counter\nhttp_requests_total{method="GET"} 1\n# HELP http_request_duration_seconds HTTP request duration\n# TYPE http_request_duration_seconds histogram\nhttp_request_duration_seconds_bucket{method="GET",le="0.005"} 0\nhttp_request_duration_seconds_bucket{method="GET",le="0.01"} 0\nhttp_request_duration_seconds_bucket{method="GET",le="0.025"} 0\n