1

I am unable to monitor my go project with the new relic

I am able to monitor using JAVA

I have follows the documentation steps: https://docs.newrelic.com/docs/apm/agents/go-agent/installation/install-new-relic-go/

  1. From github.com/newrelic/go-agent, use your preferred process; for example: bash command go get github.com/newrelic/go-agent/v3/newrelic

  2. Import the github.com/newrelic/go-agent/v3/newrelic package in your application. import github.com/newrelic/go-agent/v3/newrelic

  3. Initialize the Go agent by adding the following in the main function or in an init block:

    app, err := newrelic.NewApplication(
        newrelic.ConfigAppName("Your Application Name"),
        newrelic.ConfigLicense("YOUR_NEW_RELIC_LICENSE_KEY")
    )
    

NOTE: I have follows all the trouble shooting as well.

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",
    })
}

1 Answers1

0

you don't have to store the app in a global variable and override the existing one, this will cause issues, and also you need to start a web server. i updated the code:

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",
    })
}
no_dir_rahou
  • 81
  • 1
  • 7
  • Thank you for your quick response. It still not working. In my new relic dashboard "MyAppMain" App is not showing. Steps follows : 1. create new main.go file 2. added above code 3. run the go proj with terminal "go run main.go" in VS Code – Urvesh Rathod Sep 24 '22 at 05:05
  • it's not gonna work, you need to start a server and after that invoke the /test endpoint. add `http.ListenAndServe(":8090", nil)` at the end of your main and after running "go run main.go" do a curl to invoke the endpoint "curl 127.0.01:8090/test" – no_dir_rahou Sep 24 '22 at 17:51
  • What I need to do if I want to hit directly while I do"go run main.go"? – Urvesh Rathod Sep 26 '22 at 04:45
  • http.ListenAndServe(":8090", nil) this code will working for me so my app is created but custom events are not created. – Urvesh Rathod Sep 29 '22 at 12:36