Skip to content

Commit

Permalink
Merge pull request #2 from dbubel/feature/trace-endpoints
Browse files Browse the repository at this point in the history
add debug pprof route attachment options
  • Loading branch information
nkane committed Jun 16, 2023
2 parents d2915db + e43f877 commit 2e83cb4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
14 changes: 8 additions & 6 deletions examples/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"fmt"
"github.com/dbubel/intake"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
"net/http"
"runtime"
"strings"
"time"

"github.com/dbubel/intake"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
)

type test struct {
Expand Down Expand Up @@ -44,7 +45,6 @@ func mw2(next intake.Handler) intake.Handler {
}

func main() {

apiLogger := logrus.New()
apiLogger.SetLevel(logrus.DebugLevel)
apiLogger.SetReportCaller(true)
Expand All @@ -60,13 +60,15 @@ func main() {
intake.GET("/test-get", testSimple),
}

//mw := Middleware{logger: apiLogger}
mw := Middleware{logger: apiLogger}

//app.AddGlobalMiddleware(mw.Logging)
app.AddGlobalMiddleware(mw.Logging)
app.AddGlobalMiddleware(mw2)
app.AddGlobalMiddleware(mw1)
app.AddEndpoints(eps)

app.AttachPprofTraceEndpoints()

app.Run(&http.Server{
Addr: ":8000",
Handler: app.Router,
Expand Down
43 changes: 43 additions & 0 deletions trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package intake

import (
"net/http"
"net/http/pprof"

"github.com/julienschmidt/httprouter"
)

// AttachPprofTraceEndpoints attaches with index page for viewing
func (a *Intake) AttachPprofTraceEndpoints() {
a.Logger.Info("attaching debug pprof endpoints")
a.Router.Handler(http.MethodGet, "/debug/pprof/*item", http.DefaultServeMux)
}

// DebugTraceEndpoints follows the same middleware route pattern without the pprof
// index page
func DebugTraceEndpoints(mw ...MiddleWare) Endpoints {
endpoints := Endpoints{
GET("/debug/pprof/cmdline", DEBUGPProfCmdLine),
GET("/debug/pprof/profile", DEBUGPProfProfile),
GET("/debug/pprof/symbol", DEBUGPProfSymbol),
GET("/debug/pprof/trace", DEBUGPProfTrace),
}
endpoints.Use(mw...)
return endpoints
}

func DEBUGPProfCmdLine(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Cmdline(w, r)
}

func DEBUGPProfProfile(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Profile(w, r)
}

func DEBUGPProfSymbol(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Symbol(w, r)
}

func DEBUGPProfTrace(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
pprof.Trace(w, r)
}

0 comments on commit 2e83cb4

Please sign in to comment.