当前位置: > > > > New relic 与 go 集成:我无法使用 new relic 监控我的 go 项目
New relic 与 go 集成:我无法使用 new relic 监控我的 go 项目
来源:stackoverflow
2024-04-20 12:33:46
0浏览
收藏
大家好,我们又见面了啊~本文《New relic 与 go 集成:我无法使用 new relic 监控我的 go 项目》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~
问题内容
我无法使用新的 relic 监控我的 go 项目
我可以使用java进行监控
我已遵循文档步骤:https://docs.newrelic.com/docs/apm/agents/go-agent/installation/install-new-relic-go/
-
来自 github.com/newrelic/go-agent,使用您首选的流程;例如: bash 命令
go 获取 github.com/newrelic/go-agent/v3/newrelic -
在您的应用程序中导入 github.com/newrelic/go-agent/v3/newrelic 包。
导入 github.com/newrelic/go-agent/v3/newrelic -
通过在 main 函数或 init 块中添加以下内容来初始化 go 代理:
app, err := newrelic.newapplication( newrelic.configappname("your application name"), newrelic.configlicense("your_new_relic_license_key") )
注意:我也遵循了所有的故障排除。
main.go
package main
import (
"fmt"
"io"
"net/http"
"github.com/newrelic/go-agent/v3/newrelic"
)
var newrelicApp *newrelic.Application
func main() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("MyAppMain"),
newrelic.ConfigLicense("<YOUR_NEW_RELIC_LICENSE_KEY>"),
newrelic.ConfigAppLogForwardingEnabled(true),
)
if err != nil {
fmt.Printf("error is " + err.Error())
} else {
newrelicApp = app
http.HandleFunc(newrelic.WrapHandleFunc(app, "/test", customEvent))
}
}
func customEvent(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "recording a custom event")
newrelicApp.RecordCustomEvent("MyAppMainEvent", map[string]interface{}{
"text": "Hello VP",
"env": "go_local",
"alertType": "error",
"priority": "Critical",
"source": "MyAppMain",
})
}
正确答案
您不必将应用程序存储在全局变量中并覆盖现有变量,这会导致问题,而且您还需要启动网络服务器。 我更新了代码:
package main
import (
"fmt"
"io"
"net/http"
"github.com/newrelic/go-agent/v3/newrelic"
)
func main() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("MyAppMain"),
newrelic.ConfigLicense("280d644a5b4fc4859d5fa75b8897f4422bb5NRAL"),
newrelic.ConfigAppLogForwardingEnabled(true),
)
if err != nil {
fmt.Printf("error is " + err.Error())
} else {
http.HandleFunc(newrelic.WrapHandleFunc(app, "/test", customEvent))
}
http.ListenAndServe(":8090", nil)
}
func customEvent(w http.ResponseWriter, req *http.Request) {
txn := newrelic.FromContext(req.Context())
io.WriteString(w, "recording a custom event")
txn.Application().RecordCustomEvent("MyAppMainEvent",
map[string]interface{}{
"text": "Hello VP",
"env": "go_local",
"alertType": "error",
"priority": "Critical",
"source": "MyAppMain",
})
}
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注米云公众号,一起学习编程~
