{"id":535,"date":"2025-11-21T07:30:44","date_gmt":"2025-11-20T23:30:44","guid":{"rendered":"https:\/\/www.idcyz.com\/blog\/?p=535"},"modified":"2025-11-21T07:30:44","modified_gmt":"2025-11-20T23:30:44","slug":"%e5%85%a8%e9%9d%a2%e4%ba%86%e8%a7%a3-prometheus-%e6%95%99%e7%a8%8b%e7%9a%84%e5%85%b3%e9%94%ae%e7%89%b9%e6%80%a7%e4%b8%8e%e4%bd%bf%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.idcyz.com\/blog\/535.html","title":{"rendered":"\u5168\u9762\u4e86\u89e3 Prometheus \u6559\u7a0b\u7684\u5173\u952e\u7279\u6027\u4e0e\u4f7f\u7528\u6307\u5357"},"content":{"rendered":"<p><p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idcyz.com\/blog\/wp-content\/uploads\/2025\/07\/qvT2KacU9.jpg\" alt=\"\u5168\u9762\u4e86\u89e3 Prometheus \u6559\u7a0b\u7684\u5173\u952e\u7279\u6027\u4e0e\u4f7f\u7528\u6307\u5357\" title=\"\u5168\u9762\u4e86\u89e3 Prometheus \u6559\u7a0b\u7684\u5173\u952e\u7279\u6027\u4e0e\u4f7f\u7528\u6307\u5357\"><\/p>\n<p><h3>1. \u57fa\u672c\u6982\u8ff0<\/h3>\n<\/p>\n<p>Prometheus \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u76d1\u63a7\u548c\u62a5\u8b66\u7cfb\u7edf\uff0c\u4e13\u4e3a\u5927\u89c4\u6a21\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u800c\u8bbe\u8ba1\u3002\u5176\u4e3b\u8981\u529f\u80fd\u5305\u62ec\u5b9e\u65f6\u76d1\u63a7\u548c\u5ea6\u91cf\u805a\u5408\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\u548c\u591a\u4e2a\u6570\u636e\u6e90\u3002Prometheus \u7684\u6838\u5fc3\u7ec4\u4ef6\u5305\u62ec\u6570\u636e\u6a21\u578b\u3001\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u5e93\u3001\u67e5\u8be2\u8bed\u8a00\u4ee5\u53ca\u62a5\u8b66\u7ba1\u7406\u3002\u5b83\u80fd\u591f\u6536\u96c6\u591a\u79cd\u6307\u6807\u6570\u636e\uff0c\u5e76\u4f7f\u5f97\u8fd9\u4e9b\u6570\u636e\u5728\u53ef\u89c6\u5316\u5e73\u53f0\u4e0a\u663e\u793a\uff0c\u4e3a\u8fd0\u7ef4\u548c\u5f00\u53d1\u56e2\u961f\u63d0\u4f9b\u53ef\u9760\u7684\u6570\u636e\u652f\u6301\u3002<\/p>\n<\/p>\n<p>\n<h3>2. \u4e3b\u8981\u7279\u6027<\/h3>\n<\/p>\n<p>Prometheus \u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u51e0\u4e2a\u7279\u6027\uff1a<\/p>\n<ul>\n<li>\u591a\u7ef4\u6570\u636e\u6a21\u578b\uff1a\u91c7\u7528\u5e26\u6709\u6807\u7b7e\u7684\u65f6\u95f4\u5e8f\u5217\uff0c\u4f7f\u5f97\u67e5\u8be2\u548c\u8fc7\u6ee4\u5341\u5206\u7075\u6d3b\u3002<\/li>\n<li>\u5f3a\u5927\u7684\u67e5\u8be2\u8bed\u8a00\uff1aPromQL \u662f Prometheus \u67e5\u8be2\u8bed\u8a00\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u67e5\u8be2\u548c\u5206\u6790\u529f\u80fd\u3002<\/li>\n<li>\u65e0\u987b\u4f9d\u8d56\u5916\u90e8\u5b58\u50a8\uff1aPrometheus \u81ea\u5e26\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u5e93\uff0c\u65b9\u4fbf\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u3002<\/li>\n<li>\u6709\u6548\u7684\u62a5\u8b66\u529f\u80fd\uff1a\u5185\u7f6e\u7684\u62a5\u8b66\u7ba1\u7406\u5668\u80fd\u591f\u6839\u636e\u7528\u6237\u5b9a\u4e49\u7684\u89c4\u5219\u8fdb\u884c\u62a5\u8b66\u3002<\/li>\n<li>\u81ea\u52a8\u53d1\u73b0\u670d\u52a1\uff1a\u652f\u6301\u591a\u79cd\u670d\u52a1\u53d1\u73b0\u673a\u5236\uff0c\u7b80\u5316\u914d\u7f6e\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<\/p>\n<p>\n<h3>3. \u5b89\u88c5\u4e0e\u914d\u7f6e<\/h3>\n<\/p>\n<p>\u8981\u5b89\u88c5 Prometheus\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4e0b\u8f7d\u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236\u5305\u3002\u5b89\u88c5\u8fc7\u7a0b\u5176\u5b9e\u76f8\u5bf9\u7b80\u5355\uff0c\u4ec5\u9700\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<p><pre><code><\/p>\r\n<p># \u4e0b\u8f7dPrometheus<\/p>\r\n<p>wget https:\/\/github.com\/prometheus\/prometheus\/releases\/download\/v2.30.0\/prometheus-2.30.0.linux-amd64.tar.gz<\/p>\r\n<p># \u89e3\u538b<\/p>\r\n<p>tar -xzvf prometheus-2.30.0.linux-amd64.tar.gz<\/p>\r\n<p># \u8fdb\u5165\u76ee\u5f55<\/p>\r\n<p>cd prometheus-2.30.0.linux-amd64<\/p>\r\n<p># \u542f\u52a8Prometheus<\/p>\r\n<p>.\/prometheus --config.file=prometheus.yml<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>Prometheus \u542f\u52a8\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/localhost:9090 \u67e5\u770b\u76d1\u63a7\u9762\u677f\u3002<\/p>\n<\/p>\n<p>\n<h3>4. \u6570\u636e\u6536\u96c6<\/h3>\n<\/p>\n<p>Prometheus \u4f7f\u7528\u201c\u62c9\u201d\u7684\u65b9\u5f0f\u6765\u91c7\u96c6\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4f1a\u5b9a\u671f\u5411\u88ab\u76d1\u63a7\u7684\u670d\u52a1\u53d1\u9001 HTTP \u8bf7\u6c42\uff0c\u6536\u96c6\u6307\u6807\u4fe1\u606f\u3002\u4e3a\u4e86\u8ba9 Prometheus \u6210\u529f\u62c9\u53d6\u6570\u636e\uff0c\u9700\u8981\u5728\u88ab\u76d1\u63a7\u5e94\u7528\u4e2d\u66b4\u9732\u4e00\u4e2a Metrics \u63a5\u53e3\u3002\u901a\u5e38\uff0c\u5f00\u53d1\u8005\u4f1a\u5728\u5e94\u7528\u4e2d\u96c6\u6210 Prometheus \u63d0\u4f9b\u7684\u5ba2\u6237\u7aef\u5e93\uff0c\u7c7b\u4f3c\u5982\u4e0b\u7684\u4ee3\u7801\uff1a<\/p>\n<p><pre><code><\/p>\r\n<p>from prometheus_client import start_http_server, Counter<\/p>\r\n\r\n<p># \u5b9a\u4e49\u4e00\u4e2a\u8ba1\u6570\u5668<\/p>\r\n<p>REQUEST_COUNT = Counter('request_count', 'Total number of requests')<\/p>\r\n\r\n<p>def process_request():<\/p>\r\n<p>    # \u6a21\u62df\u5904\u7406\u8bf7\u6c42<\/p>\r\n<p>    REQUEST_COUNT.inc()<\/p>\r\n\r\n<p>if __name__ == '__main__':<\/p>\r\n<p>    start_http_server(8000)  # \u542f\u52a8HTTP\u670d\u52a1\u5668<\/p>\r\n<p>    while True:<\/p>\r\n<p>        process_request()  # \u6301\u7eed\u5904\u7406\u8bf7\u6c42<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\n<h3>5. \u67e5\u8be2\u4e0e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p>\u901a\u8fc7 PromQL\uff0c\u7528\u6237\u53ef\u4ee5\u8fdb\u884c\u591a\u6837\u7684\u67e5\u8be2\u3002\u4e3e\u4e2a\u7b80\u5355\u4f8b\u5b50\uff0c\u83b7\u53d6\u8fc7\u53bb\u4e94\u5206\u949f\u5185\u6bcf\u79d2\u7684\u8bf7\u6c42\u603b\u6570\uff1a<\/p>\n<p><pre><code><\/p>\r\n<p>sum(rate(request_count[5m])) by (method)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\u7ed3\u679c\u53ef\u4ee5\u501f\u52a9 Grafana \u7b49\u53ef\u89c6\u5316\u5de5\u5177\u8fdb\u884c\u5c55\u793a\uff0cGrafana \u5bf9 Prometheus \u7684\u652f\u6301\u975e\u5e38\u597d\uff0c\u4e24\u8005\u7ed3\u5408\u4e3a\u76d1\u63a7\u63d0\u4f9b\u4e86\u5f3a\u6709\u529b\u7684\u652f\u6491\u3002<\/p>\n<\/p>\n<p>\n<h3>6. \u62a5\u8b66\u8bbe\u7f6e<\/h3>\n<\/p>\n<p>Prometheus \u63d0\u4f9b\u62a5\u8b66\u89c4\u5219\u7684\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u5728\u6307\u6807\u8fbe\u5230\u67d0\u4e2a\u9608\u503c\u65f6\u8fdb\u884c\u62a5\u8b66\u3002\u7528\u6237\u53ef\u4ee5\u5728 prometheus.yml \u4e2d\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<p><pre><code><\/p>\r\n<p>groups:<\/p>\r\n<p>- name: example<\/p>\r\n<p>  rules:<\/p>\r\n<p>  - alert: HighRequestRate<\/p>\r\n<p>    expr: rate(request_count[1m]) > 10<\/p>\r\n<p>    for: 5m<\/p>\r\n<p>    labels:<\/p>\r\n<p>      severity: critical<\/p>\r\n<p>    annotations:<\/p>\r\n<p>      summary: \"High request rate detected\"<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\u62a5\u8b66\u89c4\u5219\u4f1a\u5728\u6307\u5b9a\u7684\u6761\u4ef6\u4e0b\u89e6\u53d1\uff0c\u5e76\u53d1\u9001\u901a\u77e5\u5230\u914d\u7f6e\u7684\u62a5\u8b66\u7cfb\u7edf\uff0c\u4f8b\u5982 Slack \u6216\u90ae\u7bb1\u3002<\/p>\n<\/p>\n<p>\n<h3>7. \u76d1\u63a7\u4e0e\u7ef4\u62a4<\/h3>\n<\/p>\n<p>Prometheus \u63d0\u4f9b\u4e86\u81ea\u6211\u76d1\u63a7\u7684\u80fd\u529b\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684 \/metrics \u7aef\u70b9\u76d1\u63a7 Prometheus \u81ea\u8eab\u7684\u6027\u80fd\u548c\u72b6\u6001\u3002\u901a\u8fc7\u76d1\u63a7\u6570\u636e\uff0c\u7528\u6237\u80fd\u53ca\u65f6\u53d1\u73b0\u95ee\u9898\u5e76\u8fdb\u884c\u7ef4\u62a4\uff0c\u786e\u4fdd\u76d1\u63a7\u7cfb\u7edf\u7684\u53ef\u9760\u6027\u3002\u5efa\u8bae\u5b9a\u671f\u67e5\u770b Prometheus \u7684\u76d1\u63a7\u6570\u636e\u548c\u62a5\u8b66\u8bb0\u5f55\uff0c\u786e\u4fdd\u4e00\u5207\u8fd0\u4f5c\u6b63\u5e38\u3002<\/p>\n<\/p>\n<p>\n<h3>8. \u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p>Prometheus \u9002\u7528\u4e8e\u591a\u4e2a\u573a\u666f\uff0c\u6bd4\u5982\u5fae\u670d\u52a1\u67b6\u6784\u3001\u5bb9\u5668\u90e8\u7f72\u548c\u4e91\u57fa\u7840\u8bbe\u65bd\u7684\u76d1\u63a7\u3002\u5b83\u80fd\u5e2e\u52a9\u8fd0\u7ef4\u4eba\u5458\u638c\u63e1\u7cfb\u7edf\u72b6\u6001\uff0c\u53ca\u65f6\u6392\u67e5\u95ee\u9898\uff0c\u63d0\u9ad8\u7cfb\u7edf\u7684\u53ef\u7528\u6027\u4e0e\u7a33\u5b9a\u6027\u3002\u5c24\u5176\u662f\u5728\u5927\u578b\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0cPrometheus \u7684\u591a\u7ef4\u6570\u636e\u6536\u96c6\u548c\u67e5\u8be2\u80fd\u529b\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p>\n<h3>9. \u5e38\u89c1\u95ee\u9898\u89e3\u7b54<\/h3>\n<\/p>\n<p><b>Prometheus \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/b> Prometheus \u662f\u7528\u6765\u76d1\u63a7\u548c\u62a5\u8b66\u7684\u4e00\u79cd\u5de5\u5177\uff0c\u80fd\u591f\u63d0\u4f9b\u7cfb\u7edf\u548c\u5e94\u7528\u6307\u6807\u7684\u5b9e\u65f6\u6570\u636e\uff0c\u5e2e\u52a9\u7528\u6237\u53ca\u65f6\u53d1\u73b0\u6027\u80fd\u74f6\u9888\u548c\u6545\u969c\u3002<\/p>\n<p><b>\u4e3a\u4ec0\u4e48\u9009\u62e9 Prometheus \u800c\u975e\u5176\u4ed6\u76d1\u63a7\u5de5\u5177\uff1f<\/b> Prometheus \u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u7684\u67e5\u8be2\u80fd\u529b\u4f7f\u5176\u533a\u522b\u4e8e\u8bb8\u591a\u76d1\u63a7\u7cfb\u7edf\u3002\u5b83\u7684\u65e0\u4f9d\u8d56\u67b6\u6784\u3001\u6613\u4e8e\u6269\u5c55\u7684\u8bbe\u8ba1\u3001\u4ee5\u53ca\u826f\u597d\u7684\u793e\u533a\u652f\u6301\u8ba9\u7528\u6237\u4f53\u9a8c\u66f4\u52a0\u51fa\u8272\u3002<\/p>\n<p><b>\u5982\u4f55\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff1f<\/b> \u6570\u636e\u53ef\u89c6\u5316\u53ef\u4ee5\u4f7f\u7528 Grafana \u7b49\u5de5\u5177\u3002Grafana \u80fd\u591f\u4e0e Prometheus \u65e0\u7f1d\u96c6\u6210\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u4e30\u5bcc\u7684\u56fe\u8868\u9009\u9879\u548c\u5b9e\u65f6\u6570\u636e\u5c55\u793a\uff0c\u6781\u5927\u63d0\u5347\u76d1\u63a7\u7684\u53ef\u8bfb\u6027\u548c\u5206\u6790\u80fd\u529b\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u57fa\u672c\u6982\u8ff0 Prometheus \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u76d1\u63a7\u548c\u62a5\u8b66\u7cfb\u7edf\uff0c\u4e13\u4e3a\u5927\u89c4\u6a21\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u800c\u8bbe\u8ba1\u3002\u5176\u4e3b\u8981\u529f\u80fd\u5305\u62ec\u5b9e\u65f6\u76d1\u63a7\u548c\u5ea6\u91cf\u805a\u5408\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\u548c\u591a\u4e2a\u6570\u636e\u6e90\u3002Prometheus \u7684\u6838\u5fc3\u7ec4\u4ef6\u5305\u62ec\u6570\u636e\u6a21\u578b\u3001\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u5e93\u3001\u67e5\u8be2\u8bed\u8a00\u4ee5\u53ca\u62a5\u8b66\u7ba1\u7406\u3002&#8230;<\/p>\n","protected":false},"author":1,"featured_media":536,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[283,285,284],"topic":[],"class_list":["post-535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jiushu","tag-prometheus","tag-285","tag-284"],"_links":{"self":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/comments?post=535"}],"version-history":[{"count":1,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/535\/revisions"}],"predecessor-version":[{"id":538,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/535\/revisions\/538"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media\/536"}],"wp:attachment":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media?parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/categories?post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/tags?post=535"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/topic?post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}