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

net/http/pprof breaks with Martini #228

Closed
apiguy opened this issue Apr 9, 2014 · 13 comments

Comments

Projects
None yet
6 participants
@apiguy
Copy link

commented Apr 9, 2014

It appears that Martini prevents net/http/pprof from working. This can be reproduced by creating a non-Martini app and an equivalent Martini app and examining the "/debug/pprof/" endpoints.

To reproduce:

If you create the following non-Martini application:

// Non-Martini version
package main

import (
    "fmt"
    "net/http"
    _ "net/http/pprof"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

When running this and then navigating to http://localhost:8080/debug/pprof/ you'll see the following:

screen shot 2014-04-09 at 2 48 32 pm

If you create the equivalent Martini based application:

// Martini Version
package main

import (
    "fmt"
    "github.com/go-martini/martini"
    "net/http"
    _ "net/http/pprof"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    m := martini.New()
    r := martini.NewRouter()
    r.Get("/", handler)
    m.Action(r.Handle)
    http.ListenAndServe(":8080", m)
}

And navigate to the same endpoint, you get a 404.

@tarrsalah

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2014

importing the net/http/pprof package using the blank _ identifier will trigger the init() method :

62       func init() {
63      http.Handle("/debug/pprof/", http.HandlerFunc(Index))
64      http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
65      http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
66      http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
67  }

This code will register a bunch of handlers in the DefaultServeMux, so side-effects of this importing doesn't effect the Martini.Router in any way.

Maybe providing a martini-contrib module for that will be a good solution. The module should declare an init() function to do the same handlers registration stuff.

@codegangsta

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2014

Yup. A martini contrib package would be great to have

On Thu, Apr 10, 2014 at 4:57 AM, صلاح الدين notifications@github.comwrote:

importing the net/http/pprofhttp://golang.org/src/pkg/net/http/pprof/pprof.gopackage using the blank
_ identifier will trigger the init() method :

62 func init() {
63 http.Handle("/debug/pprof/", http.HandlerFunc(Index))
64 http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
65 http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
66 http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
67 }

This code will register a bunch of handlers in the DefaultServeMux, so
side-effects of this importing doesn't effect the Martini.Router in any
way.

Maybe providing a martini-contrib module for that will be a good
solution. The module should declare an init() function to do the same
handlers registration stuff.


Reply to this email directly or view it on GitHubhttps://github.com//issues/228#issuecomment-40069569
.

@tarrsalah

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2014

@codegangsta I can take care of it , how can I add it to martini-contrib repo - N00p question :) - ?

@codegangsta

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2014

Create a repo with the code. I can take a look and make sure it works well
for contrib. After that I make a repo in contrib for ou to manage

On Thu, Apr 10, 2014 at 9:55 AM, ÕáÇÍ ÇáÏíä notifications@github.comwrote:

@codegangsta https://github.com/codegangsta I can take care of it , how
can I add it to martini-contrib repo - N00p question :) - ?


Reply to this email directly or view it on GitHubhttps://github.com//issues/228#issuecomment-40109944
.

@tarrsalah

This comment has been minimized.

Copy link
Contributor

commented Apr 14, 2014

@codegangsta 18 lines of code, just binding the net/http/pprof handlers to martini router, worth a repository ?

@tarrsalah

This comment has been minimized.

Copy link
Contributor

commented Apr 18, 2014

oh .. sorry, I forget to mention the link to the code.

@yinghau76

This comment has been minimized.

Copy link

commented Apr 29, 2014

It is better to use Any because go pprof may POST to these endpoints:

    m.Group("/debug/pprof", func(r martini.Router) {
        r.Any("/", pprof.Index)
        r.Any("/cmdline", pprof.Cmdline)
        r.Any("/profile", pprof.Profile)
        r.Any("/symbol", pprof.Symbol)
        r.Any("/block", pprof.Handler("block").ServeHTTP)
        r.Any("/heap", pprof.Handler("heap").ServeHTTP)
        r.Any("/goroutine", pprof.Handler("goroutine").ServeHTTP)
        r.Any("/threadcreate", pprof.Handler("threadcreate").ServeHTTP)
    })
@codegangsta

This comment has been minimized.

Copy link
Contributor

commented Aug 2, 2014

closing

@codegangsta codegangsta closed this Aug 2, 2014

@rcshubhadeep

This comment has been minimized.

Copy link

commented Jun 27, 2015

@tarrsalah The code link you given is no longer working. I really need this to figure out what is happening with my set up as I can not get it working. I am using martini classic and m.Run()

Also, I was wondering if the results are similar of manual profiling... I really need to know which function is killing the web service. My web service takes too long time.

@tarrsalah

This comment has been minimized.

Copy link
Contributor

commented Jun 27, 2015

@rcshubhadeep It's been so long since I have read or written any martini related code , so honestly I have no idea what's going on in the martini community, sorry!

@rcshubhadeep

This comment has been minimized.

Copy link

commented Jun 27, 2015

Hi,

Sorry, I think it was a mis communication from my side what I waned to mean is that the link gives me 404 error. So it will be really great to see the code once if possible. Rest I can check myself.

Please let me know

@tarrsalah

This comment has been minimized.

Copy link
Contributor

commented Jun 27, 2015

Oh yeah, @rustyrazorblade
Sorry, I deleted the code while ago, I believe that it is totally obsolete, take a look at this code, may help you.

@DavidVaini

This comment has been minimized.

Copy link

commented Aug 13, 2015

The code is deleted, I took at look at that link of code and it doesnt run, anyone have the original code snippet? Otherwise this is still an issue for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.