New relic integration with go : I am unable to monitor my go project with new relic

702 views Asked by At

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

There are 1 answers

4
no_dir_rahou On

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