我无法用新的来监视go项目
我可以使用JAVA进行监控
我遵循文档步骤:https://docs.newrelic.com/docs/apm/agents/go-agent/installation/install-new-relic-go/
go get github.com/newrelic/go-agent/v3/newrelicimport github.com/newrelic/go-agent/v3/newrelic注意:我也注意到了所有的问题解决方法。
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",
})
}发布于 2022-09-23 14:33:05
您不必将应用程序存储在全局变量中并覆盖现有变量,这将导致问题,还需要启动web服务器。我更新了代码:
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",
})
}https://stackoverflow.com/questions/73827558
复制相似问题