pkg: nump echopprof to include allocs handler

This commit is contained in:
Mohamed Sohail 2023-03-13 15:05:53 +00:00
parent 122b16451c
commit 63205cc118
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
4 changed files with 160 additions and 7 deletions

View File

@ -2,8 +2,8 @@ package main
import (
"github.com/VictoriaMetrics/metrics"
"github.com/grassrootseconomics/cic-chain-events/pkg/echopprof"
"github.com/labstack/echo/v4"
echopprof "github.com/sevenNt/echo-pprof"
)
func initApiServer() *echo.Echo {
@ -18,9 +18,7 @@ func initApiServer() *echo.Echo {
})
}
if debugFlag {
echopprof.Wrap(server)
}
echopprof.Wrap(server)
return server
}

1
go.mod
View File

@ -15,7 +15,6 @@ require (
github.com/knadh/koanf/v2 v2.0.0
github.com/labstack/echo/v4 v4.10.2
github.com/nats-io/nats.go v1.24.0
github.com/sevenNt/echo-pprof v0.1.1-0.20230131020615-4dd36891e14b
github.com/stretchr/testify v1.8.2
github.com/zerodha/logf v0.5.5
)

2
go.sum
View File

@ -555,8 +555,6 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sevenNt/echo-pprof v0.1.1-0.20230131020615-4dd36891e14b h1:IXGKwQZ6+llGbDFyTJvBXWGTkfrAqsbYwtVVm+Ax4WU=
github.com/sevenNt/echo-pprof v0.1.1-0.20230131020615-4dd36891e14b/go.mod h1:ArUb+H7+Tew7UUjK6x2xiAqFrznLrANIfz9M6m66J0c=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=

158
pkg/echopprof/echopprof.go Normal file
View File

@ -0,0 +1,158 @@
// MIT License
// Copyright (c) 2017 Leslie Zheng
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package echopprof
import (
"net/http/pprof"
"strings"
"github.com/labstack/echo/v4"
)
// Wrap adds several routes from package `net/http/pprof` to *echo.Echo object.
func Wrap(e *echo.Echo) {
WrapGroup("", e.Group("/debug/pprof"))
}
// Wrapper make sure we are backward compatible.
var Wrapper = Wrap
// WrapGroup adds several routes from package `net/http/pprof` to *echo.Group object.
func WrapGroup(prefix string, g *echo.Group) {
routers := []struct {
Method string
Path string
Handler echo.HandlerFunc
}{
{"GET", "", IndexHandler()},
{"GET", "/", IndexHandler()},
{"GET", "/heap", HeapHandler()},
{"GET", "/goroutine", GoroutineHandler()},
{"GET", "/block", BlockHandler()},
{"GET", "/threadcreate", ThreadCreateHandler()},
{"GET", "/cmdline", CmdlineHandler()},
{"GET", "/profile", ProfileHandler()},
{"GET", "/symbol", SymbolHandler()},
{"POST", "/symbol", SymbolHandler()},
{"GET", "/trace", TraceHandler()},
{"GET", "/mutex", MutexHandler()},
{"GET", "/allocs", AllocsHandler()},
}
for _, r := range routers {
switch r.Method {
case "GET":
g.GET(strings.TrimPrefix(r.Path, prefix), r.Handler)
case "POST":
g.POST(strings.TrimPrefix(r.Path, prefix), r.Handler)
}
}
}
// IndexHandler will pass the call from /debug/pprof to pprof.
func IndexHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Index(ctx.Response().Writer, ctx.Request())
return nil
}
}
// HeapHandler will pass the call from /debug/pprof/heap to pprof.
func HeapHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("heap").ServeHTTP(ctx.Response(), ctx.Request())
return nil
}
}
// GoroutineHandler will pass the call from /debug/pprof/goroutine to pprof.
func GoroutineHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("goroutine").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}
// BlockHandler will pass the call from /debug/pprof/block to pprof.
func BlockHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("block").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}
// ThreadCreateHandler will pass the call from /debug/pprof/threadcreate to pprof.
func ThreadCreateHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("threadcreate").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}
// CmdlineHandler will pass the call from /debug/pprof/cmdline to pprof.
func CmdlineHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Cmdline(ctx.Response().Writer, ctx.Request())
return nil
}
}
// ProfileHandler will pass the call from /debug/pprof/profile to pprof.
func ProfileHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Profile(ctx.Response().Writer, ctx.Request())
return nil
}
}
// SymbolHandler will pass the call from /debug/pprof/symbol to pprof.
func SymbolHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Symbol(ctx.Response().Writer, ctx.Request())
return nil
}
}
// TraceHandler will pass the call from /debug/pprof/trace to pprof.
func TraceHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Trace(ctx.Response().Writer, ctx.Request())
return nil
}
}
// MutexHandler will pass the call from /debug/pprof/mutex to pprof.
func MutexHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("mutex").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}
// AllocsHandler will pass the call from /debug/pprof/allocs to pprof.
func AllocsHandler() echo.HandlerFunc {
return func(ctx echo.Context) error {
pprof.Handler("allocs").ServeHTTP(ctx.Response().Writer, ctx.Request())
return nil
}
}