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

Echo v4 integration #88

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions _integrations/nrecho/v4/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

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

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/_integrations/nrecho/v4"
)

func mustGetEnv(key string) string {
if val := os.Getenv(key); "" != val {
return val
}
panic(fmt.Sprintf("environment variable %s unset", key))
}

func getUser(c echo.Context) error {
id := c.Param("id")

if txn := nrecho.FromContext(c); nil != txn {
txn.AddAttribute("userId", id)
}

return c.String(http.StatusOK, id)
}

func main() {
cfg := newrelic.NewConfig("Echo App", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
app, err := newrelic.NewApplication(cfg)
if nil != err {
fmt.Println(err)
os.Exit(1)
}

// Echo instance
e := echo.New()

// The New Relic Middleware should be the first middleware registered
e.Use(nrecho.Middleware(app))

// Routes
e.GET("/home", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})

// Groups
g := e.Group("/user")
g.Use(middleware.Gzip())
g.GET("/:id", getUser)

// Start server
e.Start(":8000")
}
82 changes: 82 additions & 0 deletions _integrations/nrecho/v4/nrecho.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Package nrecho introduces to support for the echo framework.
//
// Example: https://github.com/newrelic/go-agent/tree/master/_integrations/nrecho/v4/example/main.go
package nrecho

import (
"net/http"
"reflect"

"github.com/labstack/echo/v4"
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/internal"
)

func init() { internal.TrackUsage("integration", "framework", "echo", "v4") }

// FromContext returns the Transaction from the context if present, and nil
// otherwise.
func FromContext(c echo.Context) newrelic.Transaction {
return newrelic.FromContext(c.Request().Context())
}

func handlerPointer(handler echo.HandlerFunc) uintptr {
return reflect.ValueOf(handler).Pointer()
}

func transactionName(c echo.Context) string {
ptr := handlerPointer(c.Handler())
if ptr == handlerPointer(echo.NotFoundHandler) {
return "NotFoundHandler"
}
if ptr == handlerPointer(echo.MethodNotAllowedHandler) {
return "MethodNotAllowedHandler"
}
return c.Path()
}

// Middleware creates Echo middleware that instruments requests.
//
// e := echo.New()
// e.Use(nrecho.Middleware(app))
//
func Middleware(app newrelic.Application) func(echo.HandlerFunc) echo.HandlerFunc {

if nil == app {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return next
}
}

return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
rw := c.Response().Writer
txn := app.StartTransaction(transactionName(c), rw, c.Request())
defer txn.End()

c.Response().Writer = txn

// Add txn to c.Request().Context()
c.SetRequest(c.Request().WithContext(newrelic.NewContext(c.Request().Context(), txn)))

err = next(c)

// Record the response code. The response headers are not captured
// in this case because they are set after this middleware returns.
// Designed to mimic the logic in echo.DefaultHTTPErrorHandler.
if nil != err && !c.Response().Committed {

txn.SetWebResponse(nil)
c.Response().Writer = rw

if httperr, ok := err.(*echo.HTTPError); ok {
txn.WriteHeader(httperr.Code)
} else {
txn.WriteHeader(http.StatusInternalServerError)
}
}

return
}
}
}
Loading