From 63205cc118892bc84c1af2acb0517ba884d3d6cd Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Mon, 13 Mar 2023 15:05:53 +0000 Subject: [PATCH] pkg: nump echopprof to include allocs handler --- cmd/service/api.go | 6 +- go.mod | 1 - go.sum | 2 - pkg/echopprof/echopprof.go | 158 +++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 pkg/echopprof/echopprof.go diff --git a/cmd/service/api.go b/cmd/service/api.go index fafa802..b77a287 100644 --- a/cmd/service/api.go +++ b/cmd/service/api.go @@ -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 } diff --git a/go.mod b/go.mod index 0432150..a163155 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 6853260..ab338b2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/echopprof/echopprof.go b/pkg/echopprof/echopprof.go new file mode 100644 index 0000000..d0afbb7 --- /dev/null +++ b/pkg/echopprof/echopprof.go @@ -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 + } +}