{"id":207140,"date":"2025-07-08T10:15:34","date_gmt":"2025-07-08T02:15:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207140\/"},"modified":"2025-07-08T10:15:34","modified_gmt":"2025-07-08T02:15:34","slug":"new-relic-%e4%b8%8e-go-%e9%9b%86%e6%88%90%ef%bc%9a%e6%88%91%e6%97%a0%e6%b3%95%e4%bd%bf%e7%94%a8-new-relic-%e7%9b%91%e6%8e%a7%e6%88%91%e7%9a%84-go-%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207140\/","title":{"rendered":"New relic \u4e0e go \u96c6\u6210\uff1a\u6211\u65e0\u6cd5\u4f7f\u7528 new relic \u76d1\u63a7\u6211\u7684 go \u9879\u76ee"},"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>New relic \u4e0e go \u96c6\u6210\uff1a\u6211\u65e0\u6cd5\u4f7f\u7528 new relic \u76d1\u63a7\u6211\u7684 go \u9879\u76ee<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 12:33:46<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aNew relic \u4e0e go \u96c6\u6210\uff1a\u6211\u65e0\u6cd5\u4f7f\u7528 new relic \u76d1\u63a7\u6211\u7684 go \u9879\u76ee\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p><strong>\u6211\u65e0\u6cd5\u4f7f\u7528\u65b0\u7684 relic \u76d1\u63a7\u6211\u7684 go \u9879\u76ee<\/strong><\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528java\u8fdb\u884c\u76d1\u63a7<\/strong><\/p>\n<p>\u6211\u5df2\u9075\u5faa\u6587\u6863\u6b65\u9aa4\uff1ahttps:\/\/docs.newrelic.com\/docs\/apm\/agents\/go-agent\/installation\/install-new-relic-go\/<\/p>\n<ol>\n<li>\n<p><strong>\u6765\u81ea github.com\/newrelic\/go-agent\uff0c\u4f7f\u7528\u60a8\u9996\u9009\u7684\u6d41\u7a0b\uff1b\u4f8b\u5982\uff1a<\/strong> bash \u547d\u4ee4 <code>go \u83b7\u53d6 github.com\/newrelic\/go-agent\/v3\/newrelic<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u5728\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5bfc\u5165 github.com\/newrelic\/go-agent\/v3\/newrelic \u5305\u3002<\/strong> <code> \u5bfc\u5165 github.com\/newrelic\/go-agent\/v3\/newrelic<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7\u5728 main \u51fd\u6570\u6216 init \u5757\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\u6765\u521d\u59cb\u5316 go \u4ee3\u7406\uff1a<\/strong><\/p>\n<pre>app, err := newrelic.newapplication(\n    newrelic.configappname(\"your application name\"),\n    newrelic.configlicense(\"your_new_relic_license_key\")\n)<\/pre>\n<\/li>\n<\/ol>\n<p><strong>\u6ce8\u610f\uff1a\u6211\u4e5f\u9075\u5faa\u4e86\u6240\u6709\u7684\u6545\u969c\u6392\u9664\u3002<\/strong><\/p>\n<p><strong>ma\u200b\u200bin.go<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"io\"\n    \"net\/http\"\n\n    \"github.com\/newrelic\/go-agent\/v3\/newrelic\"\n)\n\nvar newrelicApp *newrelic.Application\n\nfunc main() {\n    app, err := newrelic.NewApplication(\n        newrelic.ConfigAppName(\"MyAppMain\"),\n        newrelic.ConfigLicense(\"&lt;YOUR_NEW_RELIC_LICENSE_KEY&gt;\"),\n        newrelic.ConfigAppLogForwardingEnabled(true),\n    )\n    if err != nil {\n        fmt.Printf(\"error is \" + err.Error())\n    } else {\n        newrelicApp = app\n        http.HandleFunc(newrelic.WrapHandleFunc(app, \"\/test\", customEvent))\n    }\n}\n\nfunc customEvent(w http.ResponseWriter, req *http.Request) {\n    io.WriteString(w, \"recording a custom event\")\n\n    newrelicApp.RecordCustomEvent(\"MyAppMainEvent\", map[string]interface{}{\n        \"text\":      \"Hello VP\",\n        \"env\":       \"go_local\",\n        \"alertType\": \"error\",\n        \"priority\":  \"Critical\",\n        \"source\":    \"MyAppMain\",\n    })\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u4e0d\u5fc5\u5c06\u5e94\u7528\u7a0b\u5e8f\u5b58\u50a8\u5728\u5168\u5c40\u53d8\u91cf\u4e2d\u5e76\u8986\u76d6\u73b0\u6709\u53d8\u91cf\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u800c\u4e14\u60a8\u8fd8\u9700\u8981\u542f\u52a8\u7f51\u7edc\u670d\u52a1\u5668\u3002 \u6211\u66f4\u65b0\u4e86\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"io\"\n    \"net\/http\"\n\n    \"github.com\/newrelic\/go-agent\/v3\/newrelic\"\n)\n\nfunc main() {\n    app, err := newrelic.NewApplication(\n        newrelic.ConfigAppName(\"MyAppMain\"),\n        newrelic.ConfigLicense(\"280d644a5b4fc4859d5fa75b8897f4422bb5NRAL\"),\n        newrelic.ConfigAppLogForwardingEnabled(true),\n    )\n    if err != nil {\n        fmt.Printf(\"error is \" + err.Error())\n    } else {\n\n        http.HandleFunc(newrelic.WrapHandleFunc(app, \"\/test\", customEvent))\n    }\n\n    http.ListenAndServe(\":8090\", nil)\n\n}\n\nfunc customEvent(w http.ResponseWriter, req *http.Request) {\n    txn := newrelic.FromContext(req.Context())\n    io.WriteString(w, \"recording a custom event\")\n\n    txn.Application().RecordCustomEvent(\"MyAppMainEvent\", \n    map[string]interface{}{\n        \"text\":      \"Hello VP\",\n        \"env\":       \"go_local\",\n        \"alertType\": \"error\",\n        \"priority\":  \"Critical\",\n        \"source\":    \"MyAppMain\",\n    })\n}<\/pre>\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-207140","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207140","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=207140"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207140\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}