Mattermost, Inc.

[SOLVED] Mattermost + New relic

Hello
I want use new relic APM in the mattermost application. In order to monitor the performance the application I have added the code (as mentioned in new relic) just above the createpost api request handler in api/post.go file.

func createPost(c *Context, w http.ResponseWriter, r *http.Request) {
    config := newrelic.NewConfig("mylocalstarfp", "####12337")
    app, err1 := newrelic.NewApplication(config)
    fmt.Println("config")
    fmt.Println(config)
    if nil != err1 {
        fmt.Println(err1)
        // os.Exit(1)
    }
    txn := app.StartTransaction("mylocalstar",w, r)
    defer txn.End()
    post := model.PostFromJson(r.Body)
    .....
    .......
}

The application is displayed on new relic dashboard and attributes like CPU and Memory are displayed.But no Response time and Throughput attributes are displayed.

As per new relic documentation (https://github.com/newrelic/go-agent) this code has to be added in main /init block or just at start of function where we need to monitor the performance.

But I am not able to monitor as response time and throughput attributes are not being displayed.
May be I am adding the code at wrong place.
Also I have tried to add the code at beginning of main() function in mattermost.go file. But no success.
Please suggest as to where I have to add the code.

Secondly, they have also mentioned that:
If you are using the standard HTTP library package, you can create transactions by wrapping HTTP requests, as an alternative to instrumenting a function’s code.

Here is a before-and-after example of an HTTP handler being wrapped:

Before:

http.HandleFunc("/users", usersHandler)

After:

http.HandleFunc(newrelic.WrapHandleFunc(app, "/users", usersHandler))

This automatically starts and ends a transaction with the request and response writer.

As per this where should I add the code?

Hello
Got the solution, hence posting for others to refer.
Solved the issue to track each request by this code in mattermost:

BaseRoutes.NeedTeam.Handle(newrelic.WrapHandle(app, "/users", ApiAppHandler(usersHandler))).Methods("POST")

Thanks for posting your solution @Ajita! As you’ve solved your issue, I’ll mark this as ‘solved’.