{"id":207013,"date":"2025-07-08T09:45:29","date_gmt":"2025-07-08T01:45:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207013\/"},"modified":"2025-07-08T09:45:29","modified_gmt":"2025-07-08T01:45:29","slug":"%e4%b8%ba-golang-prometheus-%e6%94%b6%e9%9b%86%e5%99%a8%e6%b7%bb%e5%8a%a0%e6%a0%87%e7%ad%be","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207013\/","title":{"rendered":"\u4e3a golang prometheus \u6536\u96c6\u5668\u6dfb\u52a0\u6807\u7b7e"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4e3a golang prometheus \u6536\u96c6\u5668\u6dfb\u52a0\u6807\u7b7e<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 14:48:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662fGolang\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u4e3a golang prometheus \u6536\u96c6\u5668\u6dfb\u52a0\u6807\u7b7e\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5f04\u6e05\u695a\u5982\u4f55\u5411\u666e\u7f57\u7c73\u4fee\u65af\u6536\u96c6\u5668\u6dfb\u52a0\u6807\u7b7e\u3002\u6211\u5728\u8fd9\u91cc\u7f3a\u5c11\u4ec0\u4e48\u60f3\u6cd5\u5417\uff1f\u6211\u6709\u4e24\u4e2a\u6587\u4ef6\uff1amain.go \u548ccollector.go<\/p>\n<p>\u6211\u4f7f\u7528\u4ee5\u4e0b\u94fe\u63a5\u4f5c\u4e3a\u6307\u5357\u3002 https:\/\/rsmitty.github.io\/prometheus-exporters\/<\/p>\n<p>\u6211\u6a21\u62df\u4e86\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6240\u4ee5\u6211\u53ef\u4ee5\u628a\u5b83\u53d1\u5e03\u5728\u8fd9\u91cc\u3002\u6211\u6700\u7ec8\u4e0d\u4f1a\u4e3a\u8be5\u547d\u4ee4\u63d0\u53d6\u201cdate +%s\u201d\u3002\u53ea\u662f\u4e0d\u77e5\u9053\u5728\u54ea\u91cc\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<p>\u5bf9\u4e8e\u6807\u7b7e\uff0c\u6211\u5c1d\u8bd5\u6dfb\u52a0\u4e3b\u673a\u540d\uff0c\u56e0\u6b64\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre># help cmd_result shows the cmd result\n# type cmd_result gauge\ncmd_result{host=\"my_device_hostname\"} 1.919256141363144e-76<\/pre>\n<p>\u6211\u5bf9 golang \u4e5f\u5f88\u964c\u751f\uff0c\u6240\u4ee5\u5f88\u6709\u53ef\u80fd\u6211\u7684\u60f3\u6cd5\u90fd\u662f\u9519\u8bef\u7684\uff01\u6211\u6700\u7ec8\u8bd5\u56fe\u8ba9\u666e\u7f57\u7c73\u4fee\u65af\u5728\u6bcf\u6b21\u6293\u53d6\u65f6\u63d0\u53d6 cmd \u7ed3\u679c\u3002<\/p>\n<p><strong>ma\u200b\u200bin.go<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"net\/http\"\n\n    log \"github.com\/sirupsen\/logrus\"\n    \"github.com\/prometheus\/client_golang\/prometheus\"\n    \"github.com\/prometheus\/client_golang\/prometheus\/promhttp\"\n)\n\nfunc main() {\n\n    \/\/create a new instance of the collector and\n    \/\/register it with the prometheus client.\n    cmd := newcollector()\n    prometheus.mustregister(cmd)\n\n    \/\/this section will start the http server and expose\n    \/\/any metrics on the \/metrics endpoint.\n    http.handle(\"\/metrics\", promhttp.handler())\n    log.info(\"beginning to serve on port :8080\")\n    log.fatal(http.listenandserve(\":8080\", nil))\n}<\/pre>\n<p><strong>collector.go<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"encoding\/binary\"\n    \"fmt\"\n    \"math\"\n    \"os\/exec\"\n    \"strings\"\n\n    \"github.com\/prometheus\/client_golang\/prometheus\"\n)\n\ntype cmdCollector struct {\n    cmdMetric *prometheus.Desc\n}\n\nfunc newCollector() *cmdCollector {\n    return &amp;cmdCollector{\n        cmdMetric: prometheus.NewDesc(\"cmd_result\",\n            \"Shows the cmd result\",\n            nil, nil,\n        ),\n    }\n}\n\nfunc (collector *cmdCollector) Describe(ch chan&lt;- *prometheus.Desc) {\n    ch &lt;- collector.cmdMetric\n}\n\nfunc (collector *cmdCollector) Collect(ch chan&lt;- prometheus.Metric) {\n\n    var metricValue float64\n    command := string(\"date +%s\")\n    cmdResult := exeCmd(command)\n    metricValue = cmdResult\n\n    ch &lt;- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue)\n\n}\n\nfunc exeCmd(cmd string) float64 {\n    parts := strings.Fields(cmd)\n    out, err := exec.Command(parts[0], parts[1]).Output()\n    if err != nil {\n        fmt.Println(\"error occured\")\n        fmt.Printf(\"%s\", err)\n    }\n    cmdProcessResult := Float64frombytes(out)\n    return cmdProcessResult\n}\n\nfunc Float64frombytes(bytes []byte) float64 {\n    bits := binary.LittleEndian.Uint64(bytes)\n    float := math.Float64frombits(bits)\n    return float\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u660e\u767d\u4e86\u3002\u6211\u5fc5\u987b\u5728\u8c03\u7528 newdesc \u65b9\u6cd5\u7684\u4f4d\u7f6e\u58f0\u660e\u6807\u7b7e\uff0c\u7136\u540e\u5728 mustnewconstmetric \u65b9\u6cd5\u4e2d\u4f20\u9012\u503c<\/p>\n<p>\u8fd9\u662f\u6211\u7684\u65b0\u201cnewcollector\u201d\uff0c\u5e26\u6709\u201c\u4e3b\u673a\u540d\u201d\u6807\u7b7e\u3002 <\/p>\n<pre>func newcollector() *cmdcollector {\n    return &amp;cmdcollector{\n        cmdmetric: prometheus.newdesc(\"cmd_result\",\n            \"shows the cmd result\",\n            []string{\"hostname\"}, nil,\n        ),\n    }\n}<\/pre>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u5728\u8fd9\u91cc\u4ec5\u6dfb\u52a0\u201c\u53d8\u91cf\u6807\u7b7e\u201d\u3002\u6700\u540e\u4e00\u4e2a\u201cnil\u201d\u7528\u4e8e\u5e38\u91cf\u6807\u7b7e\u3002 <\/p>\n<p>\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4efb\u610f\u6570\u91cf\u7684\u9879\u76ee\uff0c\u4f8b\u5982&#8230;&#8230;<\/p>\n<pre>[]string{\"hostname\", \"another_label\", \"and_another_label\"}<\/pre>\n<p>\u8fd9\u91cc\u4ecb\u7ecd\u4e86\u8fd9\u4e00\u70b9\uff1a <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u53ef\u4ee5\u5728\u8c03\u7528\u201cmustnewconstmetric\u201d\u65b9\u6cd5\u65f6\u6dfb\u52a0\u8fd9\u4e9b\u503c\u3002<\/p>\n<pre>ch &lt;- prometheus.mustnewconstmetric(collector.cmdmetric, prometheus.gaugevalue, metricvalue, hostname)<\/pre>\n<p>\u6574\u4e2a\u8857\u533a&#8230;<\/p>\n<pre>func (collector *cmdcollector) collect(ch chan&lt;- prometheus.metric) {\n\n    var metricvalue float64\n    command := string(\"date +%s\")\n    cmdresult := execmd(command)\n    metricvalue = cmdresult\n\n    ch &lt;- prometheus.mustnewconstmetric(collector.cmdmetric, prometheus.gaugevalue, metricvalue, hostname)\n\n}<\/pre>\n<p>\u5982\u679c\u6211\u4f20\u9012\u591a\u4e2a\u6807\u7b7e\uff1b\u6bd4\u5982\u6211\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u5b83\u770b\u8d77\u6765\u66f4\u50cf\u662f\u8fd9\u6837&#8230;&#8230;<\/p>\n<pre>ch &lt;- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue, hostname, anotherLabel\", \"andAnotherLabel)<\/pre>\n<p>\u8fd9\u91cc\u4ecb\u7ecd\u4e86\u8fd9\u4e00\u70b9\uff1a <\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207013","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207013"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207013\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}