首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新文物与围棋的整合:我无法用新的文物来监控我的围棋项目。

新文物与围棋的整合:我无法用新的文物来监控我的围棋项目。
EN

Stack Overflow用户
提问于 2022-09-23 12:13:52
回答 1查看 98关注 0票数 1

我无法用新的来监视go项目

我可以使用JAVA进行监控

我遵循文档步骤:https://docs.newrelic.com/docs/apm/agents/go-agent/installation/install-new-relic-go/

  1. 来自 github.com/newrelic/go-agent,使用您的首选进程;例如: bash命令go get github.com/newrelic/go-agent/v3/newrelic
  2. 在应用程序中导入github.com/newrelic/go-agent/v3/newrelic包。 import github.com/newrelic/go-agent/v3/newrelic
  3. 通过在主函数或init块中添加以下内容初始化Go代理: newrelic.ConfigLicense("YOUR_NEW_RELIC_LICENSE_KEY"),:= newrelic.NewApplication( newrelic.ConfigAppName(“您的应用程序名称”))

注意:我也注意到了所有的问题解决方法。

main.go

代码语言:javascript
复制
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",
    })
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-23 14:33:05

您不必将应用程序存储在全局变量中并覆盖现有变量,这将导致问题,还需要启动web服务器。我更新了代码:

代码语言:javascript
复制
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",
    })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73827558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档