Skip to content
Merged
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
71 changes: 71 additions & 0 deletions end-to-end-tests/gochi/expect.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--- a/main.go
+++ b/main.go
@@ -4,9 +4,12 @@
"io"
"log/slog"
"net/http"
+ "time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
+ "github.com/newrelic/go-agent/v3/integrations/nrgochi"
+ "github.com/newrelic/go-agent/v3/newrelic"
)

func endpoint404(w http.ResponseWriter, r *http.Request) {
@@ -15,9 +15,16 @@
}

func basicExternal(w http.ResponseWriter, r *http.Request) {
+ nrTxn := newrelic.FromContext(r.Context())
+
+ // the "http.Get()" net/http method can not be instrumented and its outbound traffic can not be traced
+ // please see these examples of code patterns for external http calls that can be instrumented:
+ // https://docs.newrelic.com/docs/apm/agents/go-agent/configuration/distributed-tracing-go-agent/#make-http-requests
+ //
// Make an http request to an external address
resp, err := http.Get("https://example.com")
if err != nil {
+ nrTxn.NoticeError(err)
slog.Error(err.Error())
io.WriteString(w, err.Error())
return
@@ -28,15 +32,31 @@
}

func main() {
+ NewRelicAgent, agentInitError := newrelic.NewApplication(newrelic.ConfigFromEnvironment())
+ if agentInitError != nil {
+ panic(agentInitError)
+ }
+
r := chi.NewRouter()
+ r.Use(nrgochi.Middleware(NewRelicAgent))
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
+ nrTxn := newrelic.FromContext(r.Context())
+
+ defer nrTxn.StartSegment("GET:/").End()
+
w.Write([]byte("welcome"))
})
r.Get("/404", endpoint404)
r.Get("/external", basicExternal)
r.Get("/literal", func(w http.ResponseWriter, r *http.Request) {
+ nrTxn := newrelic.FromContext(r.Context())

+ defer nrTxn.StartSegment("GET:/literal").End()
+
+ // the "http.Get()" net/http method can not be instrumented and its outbound traffic can not be traced
+ // please see these examples of code patterns for external http calls that can be instrumented:
+ // https://docs.newrelic.com/docs/apm/agents/go-agent/configuration/distributed-tracing-go-agent/#make-http-requests
_, err := http.Get("https://newrelic.com")
if err != nil {
slog.Error(err.Error())
@@ -44,4 +55,6 @@
w.Write([]byte("function literal example"))
})
http.ListenAndServe(":3000", r)
+
+ NewRelicAgent.Shutdown(5 * time.Second)
}
5 changes: 5 additions & 0 deletions end-to-end-tests/gochi/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gochi

go 1.25

require github.com/go-chi/chi/v5 v5.2.2
2 changes: 2 additions & 0 deletions end-to-end-tests/gochi/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
47 changes: 47 additions & 0 deletions end-to-end-tests/gochi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"io"
"log/slog"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)

func endpoint404(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("returning 404"))
}

func basicExternal(w http.ResponseWriter, r *http.Request) {
// Make an http request to an external address
resp, err := http.Get("https://example.com")
if err != nil {
slog.Error(err.Error())
io.WriteString(w, err.Error())
return
}

defer resp.Body.Close()
io.Copy(w, resp.Body)
}

func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome"))
})
r.Get("/404", endpoint404)
r.Get("/external", basicExternal)
r.Get("/literal", func(w http.ResponseWriter, r *http.Request) {

_, err := http.Get("https://newrelic.com")
if err != nil {
slog.Error(err.Error())
}
w.Write([]byte("function literal example"))
})
http.ListenAndServe(":3000", r)
}
65 changes: 35 additions & 30 deletions end-to-end-tests/testcases.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
{
"tests": [
{
"name": "http web app",
"dir": "end-to-end-tests/http-app"
},
{
"name": "http-mux web app",
"dir": "end-to-end-tests/http-mux-app"
},
{
"name": "grpc app",
"dir": "end-to-end-tests/grpc",
"builds": [
"end-to-end-tests/grpc/server",
"end-to-end-tests/grpc/client"
]
},
{
"name": "gin - basic",
"dir": "end-to-end-tests/gin-examples/basic"
},
{
"name": "gin - multiple services",
"dir": "end-to-end-tests/gin-examples/multiple-service"
},
{
"name": "unit tests",
"dir": "end-to-end-tests/unit-tests"
}
]}
"tests": [
{
"name": "http web app",
"dir": "end-to-end-tests/http-app"
},
{
"name": "http-mux web app",
"dir": "end-to-end-tests/http-mux-app"
},
{
"name": "grpc app",
"dir": "end-to-end-tests/grpc",
"builds": [
"end-to-end-tests/grpc/server",
"end-to-end-tests/grpc/client"
]
},
{
"name": "gin - basic",
"dir": "end-to-end-tests/gin-examples/basic"
},
{
"name": "gin - multiple services",
"dir": "end-to-end-tests/gin-examples/multiple-service"
},
{
"name": "unit tests",
"dir": "end-to-end-tests/unit-tests"
},
{
"name": "gochi app",
"dir": "end-to-end-tests/gochi"
}
]
}
34 changes: 34 additions & 0 deletions internal/codegen/gochi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package codegen

import "github.com/dave/dst"

const (
NrChiImportPath = "github.com/newrelic/go-agent/v3/integrations/nrgochi"
)

// Inject NR Middleware instrumentation logic to the Chi application via the `Use` directive.
// Ex:
//
// router := chi.NewRouter()
// router.Use(nrgochi.Middleware(app)) <--- Midddleware injection
func NrChiMiddleware(routerName string, agentVariableName dst.Expr) (*dst.ExprStmt, string) {
return &dst.ExprStmt{
X: &dst.CallExpr{
Fun: &dst.SelectorExpr{
X: &dst.Ident{Name: routerName},
Sel: &dst.Ident{Name: "Use"},
},
Args: []dst.Expr{
&dst.CallExpr{
Fun: &dst.Ident{
Name: "Middleware",
Path: NrChiImportPath,
},
Args: []dst.Expr{
agentVariableName,
},
},
},
},
}, NrChiImportPath
}
79 changes: 79 additions & 0 deletions internal/codegen/gochi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package codegen

import (
"reflect"
"testing"

"github.com/dave/dst"
)

const (
ChiImportPath = "github.com/go-chi/chi/v5"
)

func Test_NrChiMiddleware(t *testing.T) {
type args struct {
call *dst.CallExpr
routerName string
agentVariableName dst.Expr
}

type test struct {
name string
args args
want *dst.ExprStmt
}

tests := []test{
{
name: "inject_nrgochi_middleware",
args: args{
call: &dst.CallExpr{
Fun: &dst.SelectorExpr{
X: &dst.Ident{
Name: "NewRouter",
Path: ChiImportPath,
},
},
},
routerName: "router",
agentVariableName: &dst.Ident{Name: "NewRelicApplication"},
},
want: &dst.ExprStmt{
X: &dst.CallExpr{
Fun: &dst.SelectorExpr{
X: &dst.Ident{
Name: "router",
},
Sel: &dst.Ident{
Name: "Use",
},
},
Args: []dst.Expr{
&dst.CallExpr{
Fun: &dst.Ident{
Name: "Middleware",
Path: NrChiImportPath,
},
Args: []dst.Expr{
&dst.Ident{Name: "NewRelicApplication"},
},
},
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, imp := NrChiMiddleware(tt.args.routerName, tt.args.agentVariableName)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NrChiMiddleware() = %v, want %v", got, tt.want)
}
if imp != NrChiImportPath {
t.Errorf("NrChiMiddleware() = %v, want %v", imp, NrChiImportPath)
}
})
}
}
Loading
Loading