Skip to content

Commit

Permalink
Merge pull request #32 from foodora/newrelic_transaction_manually
Browse files Browse the repository at this point in the history
NewRelic add method to start transaction manually and send error messages to newrelic
  • Loading branch information
guilherme-santos authored Jan 24, 2018
2 parents f920877 + 8a23063 commit e8e1ad4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
43 changes: 37 additions & 6 deletions ranger_metrics/newrelic.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package ranger_metrics

import (
newrelic "github.com/newrelic/go-agent"
"errors"
"net/http"

"github.com/foodora/go-ranger/ranger_logger"
newrelic "github.com/newrelic/go-agent"
)

type NewRelic struct {
Application newrelic.Application
logger ranger_logger.LoggerInterface
}

func NewNewRelic(appName string, license string, logger ranger_logger.LoggerInterface) *NewRelic {
app, err := newrelic.NewApplication(newrelic.NewConfig(
appName,
license),
)
config := newrelic.NewConfig(appName, license)

app, err := newrelic.NewApplication(config)
if err != nil {
logger.Panic("NewRelic error", ranger_logger.LoggerData{"error": err.Error()})
}

return &NewRelic{
Application: app,
logger: logger,
}
}

Expand All @@ -30,8 +32,37 @@ func (newRelic *NewRelic) Middleware(next http.Handler) http.Handler {
txn := newRelic.Application.StartTransaction(r.URL.Path, w, r)
defer txn.End()

next.ServeHTTP(w, r)
next.ServeHTTP(txn, r)
}

return http.HandlerFunc(fn)
}

// StartTransaction start a transaction manually
// * Call the returned function to end the transaction
func (newRelic *NewRelic) StartTransaction(w http.ResponseWriter, r *http.Request) func() {
txn := newRelic.Application.StartTransaction(r.URL.Path, w, r)

return func() {
txn.End()
}
}

// NoticeError send content of err to newrelic using trasaction
// that middleware has started
func (newRelic *NewRelic) NoticeError(w http.ResponseWriter, err error) {
if txn, ok := w.(newrelic.Transaction); ok {
txnErr := txn.NoticeError(err)
if txnErr != nil {
newRelic.logger.Error("Cannot send error to NewRelic", ranger_logger.LoggerData{
"error": err.Error(),
})
}

return
}

newRelic.logger.Error("Cannot send error to NewRelic", ranger_logger.LoggerData{
"error": errors.New("Transaction wasn't started by NewRelic Middleware"),
})
}
41 changes: 40 additions & 1 deletion ranger_metrics/newrelic_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
package ranger_metrics

import "testing"
import (
"errors"
"net/http"
"testing"

"github.com/foodora/go-ranger/ranger_logger"
)

func TestInitNewRelic(t *testing.T) {
//t.Error("@todo TestInitNewRelic")
}

func ExampleStartTransactionManually() {
var logger ranger_logger.LoggerInterface

newRelic := NewNewRelic("appName", "license", logger)

// Start and end manually
closeTxn := newRelic.StartTransaction(nil, nil)
// your code here
closeTxn()

// Start and end with defer
closeTxn = newRelic.StartTransaction(nil, nil)
defer closeTxn()
// your code here

// Start and end with defer in the same line
defer newRelic.StartTransaction(nil, nil)()
// your code here
}

func ExampleNoticeError() {
var logger ranger_logger.LoggerInterface

newRelic := NewNewRelic("appName", "license", logger)

// Ensure that your handler is called by NewRelic.Middleware
handler := newRelic.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
newRelic.NoticeError(w, errors.New("Application failed!"))
}))

http.Handle("/foo", handler)
}

0 comments on commit e8e1ad4

Please sign in to comment.