@@ -1537,13 +1537,9 @@ import (
15371537)
15381538
15391539func main() {
1540- http.HandleFunc("/literal", func(w http.ResponseWriter, r *http.Request) {
1541- nrTxn := newrelic.FromContext(r.Context())
1542-
1543- defer nrTxn.StartSegment("http.HandleFunc:/literal").End()
1544-
1540+ http.HandleFunc(newrelic.WrapHandleFunc(txn.Application(), "/literal", func(w http.ResponseWriter, r *http.Request) {
15451541 w.Write([]byte("Hello, literal"))
1546- })
1542+ }))
15471543 http.ListenAndServe(":8080", nil)
15481544}
15491545` ,
@@ -1576,13 +1572,9 @@ import (
15761572)
15771573
15781574func setup() {
1579- http.HandleFunc("/setup", func(w http.ResponseWriter, r *http.Request) {
1580- nrTxn := newrelic.FromContext(r.Context())
1581-
1582- defer nrTxn.StartSegment("http.HandleFunc:/setup").End()
1583-
1575+ http.HandleFunc(newrelic.WrapHandleFunc(txn.Application(), "/setup", func(w http.ResponseWriter, r *http.Request) {
15841576 w.Write([]byte("Hello, setup literal"))
1585- })
1577+ }))
15861578}
15871579
15881580func main() {
@@ -1592,29 +1584,17 @@ func main() {
15921584` ,
15931585 },
15941586 {
1595- name : "HTTP function literal instrumentation mixed case " ,
1587+ name : "HTTP function literal instrumentation request object naming " ,
15961588 code : `package main
15971589
15981590import (
15991591 "net/http"
16001592)
16011593
1602- func setup() {
1603- http.HandleFunc("/setup", func(w http.ResponseWriter, r *http.Request) {
1604- w.Write([]byte("Hello, setup literal"))
1605- })
1606- }
1607-
1608- func declaredHandler(w http.ResponseWriter, r *http.Request) {
1609- w.Write([]byte("Hello, declared"))
1610- }
1611-
16121594func main() {
1613- http.HandleFunc("/index ", func(w http.ResponseWriter, r *http.Request) {
1614- w.Write([]byte("Hello, index "))
1595+ http.HandleFunc("/literal ", func(w http.ResponseWriter, req *http.Request) {
1596+ w.Write([]byte("Hello, literal "))
16151597 })
1616- setup()
1617- http.HandleFunc("/declared", declaredHandler)
16181598 http.ListenAndServe(":8080", nil)
16191599}
16201600` ,
@@ -1626,89 +1606,74 @@ import (
16261606 "github.com/newrelic/go-agent/v3/newrelic"
16271607)
16281608
1629- func setup() {
1630- http.HandleFunc("/setup", func(w http.ResponseWriter, r *http.Request) {
1631- nrTxn := newrelic.FromContext(r.Context())
1632-
1633- defer nrTxn.StartSegment("http.HandleFunc:/setup").End()
1634-
1635- w.Write([]byte("Hello, setup literal"))
1636- })
1637- }
1638-
1639- func declaredHandler(w http.ResponseWriter, r *http.Request) {
1640- w.Write([]byte("Hello, declared"))
1641- }
1642-
16431609func main() {
1644- http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
1645- nrTxn := newrelic.FromContext(r.Context())
1646-
1647- defer nrTxn.StartSegment("http.HandleFunc:/index").End()
1648-
1649- w.Write([]byte("Hello, index"))
1650- })
1651- setup()
1652- http.HandleFunc("/declared", declaredHandler)
1610+ http.HandleFunc(newrelic.WrapHandleFunc(txn.Application(), "/literal", func(w http.ResponseWriter, req *http.Request) {
1611+ w.Write([]byte("Hello, literal"))
1612+ }))
16531613 http.ListenAndServe(":8080", nil)
16541614}
16551615` ,
16561616 },
1617+ }
1618+
1619+ for _ , tt := range tests {
1620+ t .Run (tt .name , func (t * testing.T ) {
1621+ defer panicRecovery (t )
1622+ got := testStatefulTracingFunction (t , tt .code , WrapNestedHandleFunction , true )
1623+ assert .Equal (t , tt .expect , got )
1624+ })
1625+ }
1626+
1627+ }
1628+
1629+ func TestInstrumentRouterFunctionLiteral (t * testing.T ) {
1630+ tests := []struct {
1631+ name string
1632+ code string
1633+ expect string
1634+ }{
16571635 {
1658- name : "HTTP function literal instrumentation request object naming " ,
1636+ name : "Router literal function instrumentation in main " ,
16591637 code : `package main
16601638
16611639import (
16621640 "net/http"
1641+
1642+ chi "github.com/go-chi/chi/v5"
16631643)
16641644
16651645func main() {
1666- http.HandleFunc("/literal", func(w http.ResponseWriter, req *http.Request) {
1667- w.Write([]byte("Hello, literal"))
1646+ router := chi.NewRouter()
1647+ router.Get("/lit-main", func(w http.ResponseWriter, r *http.Request) {
1648+ w.Write([]byte("Hello, main"))
16681649 })
1669- http.ListenAndServe(":8080", nil )
1650+ http.ListenAndServe(":8080", r )
16701651}
16711652` ,
16721653 expect : `package main
16731654
16741655import (
16751656 "net/http"
16761657
1658+ chi "github.com/go-chi/chi/v5"
16771659 "github.com/newrelic/go-agent/v3/newrelic"
16781660)
16791661
16801662func main() {
1681- http.HandleFunc("/literal", func(w http.ResponseWriter, req *http.Request) {
1682- nrTxn := newrelic.FromContext(req.Context())
1663+ router := chi.NewRouter()
1664+ router.Get("/lit-main", func(w http.ResponseWriter, r *http.Request) {
1665+ nrTxn := newrelic.FromContext(r.Context())
16831666
1684- defer nrTxn.StartSegment("http.HandleFunc:/literal ").End()
1667+ defer nrTxn.StartSegment("GET:/lit-main ").End()
16851668
1686- w.Write([]byte("Hello, literal "))
1669+ w.Write([]byte("Hello, main "))
16871670 })
1688- http.ListenAndServe(":8080", nil )
1671+ http.ListenAndServe(":8080", r )
16891672}
16901673` ,
16911674 },
1692- }
1693-
1694- for _ , tt := range tests {
1695- t .Run (tt .name , func (t * testing.T ) {
1696- defer panicRecovery (t )
1697- got := testStatelessTracingFunction (t , tt .code , InstrumentHTTPHandleFuncLit )
1698- assert .Equal (t , tt .expect , got )
1699- })
1700- }
1701-
1702- }
1703-
1704- func TestInstrumentRouterFunctionLiteral (t * testing.T ) {
1705- tests := []struct {
1706- name string
1707- code string
1708- expect string
1709- }{
17101675 {
1711- name : " Router literal function instrumentation in main" ,
1676+ name : ` Router function instrumentation mixed case` ,
17121677 code : `package main
17131678
17141679import (
@@ -1717,11 +1682,16 @@ import (
17171682 chi "github.com/go-chi/chi/v5"
17181683)
17191684
1685+ func declared(w http.ResponseWriter, r *http.Request) {
1686+ w.Write([]byte("declared"))
1687+ }
1688+
17201689func main() {
17211690 router := chi.NewRouter()
1722- router.GET ("/lit-main", func(w http.ResponseWriter, r *http.Request) {
1691+ router.Get ("/lit-main", func(w http.ResponseWriter, r *http.Request) {
17231692 w.Write([]byte("Hello, main"))
17241693 })
1694+ router.Get("/declared", declared)
17251695 http.ListenAndServe(":8080", r)
17261696}
17271697` ,
@@ -1734,15 +1704,20 @@ import (
17341704 "github.com/newrelic/go-agent/v3/newrelic"
17351705)
17361706
1707+ func declared(w http.ResponseWriter, r *http.Request) {
1708+ w.Write([]byte("declared"))
1709+ }
1710+
17371711func main() {
17381712 router := chi.NewRouter()
1739- router.GET ("/lit-main", func(w http.ResponseWriter, r *http.Request) {
1713+ router.Get ("/lit-main", func(w http.ResponseWriter, r *http.Request) {
17401714 nrTxn := newrelic.FromContext(r.Context())
17411715
17421716 defer nrTxn.StartSegment("GET:/lit-main").End()
17431717
17441718 w.Write([]byte("Hello, main"))
17451719 })
1720+ router.Get("/declared", declared)
17461721 http.ListenAndServe(":8080", r)
17471722}
17481723` ,
0 commit comments