Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
urvesh-educloud opened this issue Sep 23, 2022 · 4 comments
Labels

Comments

@urvesh-educloud
Copy link

urvesh-educloud commented Sep 23, 2022

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",
	})
}
@iamemilio
Copy link
Contributor

Take a look at this example code and just double check that your implementation is following best practices: https://github.com/newrelic/go-agent/blob/master/v3/examples/server/main.go

If you're still having trouble let us know

@urvesh-educloud
Copy link
Author

urvesh-educloud commented Sep 29, 2022

Hi @iamemilio,
How can I execute this?
Can you provide the example so I can refer?
I want to create the app as well as the event. (as shown in the example) when I run the go project using "go run main.go"

@iamemilio
Copy link
Contributor

iamemilio commented Sep 29, 2022

When you wrap an http handler with new relic, we pass a transaction into the context. You should use that transaction to call the methods of the go agent application. To run this, first do go mod init, then go mod tidy, and finally go run main.go.

package main

import (
	"fmt"
	"io"
	"net/http"
	"os"

	"github.com/newrelic/go-agent/v3/newrelic"
)

func customEvent(w http.ResponseWriter, r *http.Request) {
	txn := newrelic.FromContext(r.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",
	})
}

func main() {
	app, err := newrelic.NewApplication(
		newrelic.ConfigAppName("MyAppMain"),
		newrelic.ConfigLicense("YOUR_NEW_RELIC_LICENSE_KEY"),
		newrelic.ConfigAppLogForwardingEnabled(true),
	)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	
	http.HandleFunc(newrelic.WrapHandleFunc(app, "/custom_event", customEvent))
	http.ListenAndServe(":8000", nil)
}

@iamemilio
Copy link
Contributor

hi @urvesh-educloud I am going to close this and assume that the solution provided worked for you. If that is not the case, please re-open the ticket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants