From 08ae3289eea4c5c424254dc86a4203aa018f2f95 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 12 Oct 2018 16:15:47 +0800 Subject: [PATCH] update 1. change pprof module 2. remove memory log 3. remove line hook log 4. send line log to es --- Gopkg.lock | 18 +-- main.go | 15 +-- router/line/line.go | 11 +- router/routes/routes.go | 4 +- .../ginpprof}/.gitignore | 1 - .../DeanThompson/ginpprof/.travis.yml | 11 ++ .../pprof => DeanThompson/ginpprof}/LICENSE | 5 +- .../DeanThompson/ginpprof/README.md | 68 ++++++++++ .../github.com/DeanThompson/ginpprof/pprof.go | 123 ++++++++++++++++++ .../github.com/gin-contrib/pprof/.travis.yml | 22 ---- vendor/github.com/gin-contrib/pprof/README.md | 83 ------------ vendor/github.com/gin-contrib/pprof/pprof.go | 42 ------ 12 files changed, 232 insertions(+), 171 deletions(-) rename vendor/github.com/{gin-contrib/pprof => DeanThompson/ginpprof}/.gitignore (96%) create mode 100644 vendor/github.com/DeanThompson/ginpprof/.travis.yml rename vendor/github.com/{gin-contrib/pprof => DeanThompson/ginpprof}/LICENSE (94%) create mode 100644 vendor/github.com/DeanThompson/ginpprof/README.md create mode 100644 vendor/github.com/DeanThompson/ginpprof/pprof.go delete mode 100644 vendor/github.com/gin-contrib/pprof/.travis.yml delete mode 100644 vendor/github.com/gin-contrib/pprof/README.md delete mode 100644 vendor/github.com/gin-contrib/pprof/pprof.go diff --git a/Gopkg.lock b/Gopkg.lock index b260b65..6cd0f6e 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1,6 +1,14 @@ # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. +[[projects]] + branch = "master" + digest = "1:b763ac7907021d7c87885dd299ae8b57015243f179493b274b3b9af808ecbdcc" + name = "github.com/DeanThompson/ginpprof" + packages = ["."] + pruneopts = "UT" + revision = "8c0e31bfeaa87bd40412ee8a8ba383f5f700ff72" + [[projects]] digest = "1:a62f6ed230a8cd138a9efbe718e7d0b0294f139266f5f55cd942769a9aac8de2" name = "github.com/PuerkitoBio/goquery" @@ -44,14 +52,6 @@ revision = "cf4846e6a636a76237a28d9286f163c132e841bc" version = "v1.2" -[[projects]] - digest = "1:fd66f3d5f4398879b0051e46e8421abdc93b5a6a64b645dc80986157fb99308d" - name = "github.com/gin-contrib/pprof" - packages = ["."] - pruneopts = "UT" - revision = "cea200e02ddddca4b843d75d32aa13c413549de9" - version = "v1.1" - [[projects]] branch = "master" digest = "1:36fe9527deed01d2a317617e59304eb2c4ce9f8a24115bcc5c2e37b3aee5bae4" @@ -257,9 +257,9 @@ analyzer-name = "dep" analyzer-version = 1 input-imports = [ + "github.com/DeanThompson/ginpprof", "github.com/PuerkitoBio/goquery", "github.com/gin-contrib/cors", - "github.com/gin-contrib/pprof", "github.com/gin-gonic/contrib/sessions", "github.com/gin-gonic/gin", "github.com/gin-gonic/gin/binding", diff --git a/main.go b/main.go index 308a733..f68353f 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "encoding/gob" "errors" "flag" - "fmt" "log" "os" "path" @@ -102,13 +101,13 @@ func PrintMemUsage() { var m runtime.MemStats runtime.ReadMemStats(&m) // For info on each, see: https://golang.org/pkg/runtime/#MemStats - fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc)) - fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc)) - fmt.Printf("\tSys = %v MiB", bToMb(m.Sys)) - fmt.Printf("\tNumGC = %v\n", m.NumGC) - fmt.Printf("HeapAlloc = %v MiB", bToMb(m.HeapAlloc)) - fmt.Printf("\t HeapSys = %v MiB", bToMb(m.HeapSys)) - fmt.Printf("\t NextGC = %v MiB\n", bToMb(m.NextGC)) + // fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc)) + // fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc)) + // fmt.Printf("\tSys = %v MiB", bToMb(m.Sys)) + // fmt.Printf("\tNumGC = %v\n", m.NumGC) + // fmt.Printf("HeapAlloc = %v MiB", bToMb(m.HeapAlloc)) + // fmt.Printf("\t HeapSys = %v MiB", bToMb(m.HeapSys)) + // fmt.Printf("\t NextGC = %v MiB\n", bToMb(m.NextGC)) obj := map[string]interface{}{ "Alloc": m.Alloc, diff --git a/router/line/line.go b/router/line/line.go index 3507843..085e948 100644 --- a/router/line/line.go +++ b/router/line/line.go @@ -5,9 +5,10 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" - "fmt" "io/ioutil" + "git.trj.tw/golang/mtfosbot/module/es" + "git.trj.tw/golang/mtfosbot/module/config" "git.trj.tw/golang/mtfosbot/module/context" "git.trj.tw/golang/mtfosbot/module/line-message" @@ -72,7 +73,13 @@ func GetLineMessage(c *context.Context) { c.DataFormat("body type error") } - fmt.Println("Line Hook ::: ", string(raw)) + jsonMap := make(map[string]interface{}) + if err := json.Unmarshal(raw, &jsonMap); err == nil { + es.PutLog("line", jsonMap) + } + defer func() { + jsonMap = nil + }() events := struct { Events []*lineobj.EventObject `json:"events"` diff --git a/router/routes/routes.go b/router/routes/routes.go index ec004f3..906a6e8 100644 --- a/router/routes/routes.go +++ b/router/routes/routes.go @@ -12,8 +12,8 @@ import ( "git.trj.tw/golang/mtfosbot/router/private" "git.trj.tw/golang/mtfosbot/router/rimg" "git.trj.tw/golang/mtfosbot/router/twitch" + "github.com/DeanThompson/ginpprof" "github.com/gin-contrib/cors" - "github.com/gin-contrib/pprof" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" ) @@ -105,5 +105,5 @@ func SetRoutes(r *gin.Engine) { } // set pprof router - pprof.Register(r, nil) + ginpprof.Wrap(r) } diff --git a/vendor/github.com/gin-contrib/pprof/.gitignore b/vendor/github.com/DeanThompson/ginpprof/.gitignore similarity index 96% rename from vendor/github.com/gin-contrib/pprof/.gitignore rename to vendor/github.com/DeanThompson/ginpprof/.gitignore index b56be2d..daf913b 100644 --- a/vendor/github.com/gin-contrib/pprof/.gitignore +++ b/vendor/github.com/DeanThompson/ginpprof/.gitignore @@ -22,4 +22,3 @@ _testmain.go *.exe *.test *.prof -coverage* diff --git a/vendor/github.com/DeanThompson/ginpprof/.travis.yml b/vendor/github.com/DeanThompson/ginpprof/.travis.yml new file mode 100644 index 0000000..4be76b9 --- /dev/null +++ b/vendor/github.com/DeanThompson/ginpprof/.travis.yml @@ -0,0 +1,11 @@ +language: go + +go: + - 1.5 + - 1.6 + - 1.7 + - 1.8 + - tip + +script: + - go test diff --git a/vendor/github.com/gin-contrib/pprof/LICENSE b/vendor/github.com/DeanThompson/ginpprof/LICENSE similarity index 94% rename from vendor/github.com/gin-contrib/pprof/LICENSE rename to vendor/github.com/DeanThompson/ginpprof/LICENSE index 4e2cfb0..a25bc61 100644 --- a/vendor/github.com/gin-contrib/pprof/LICENSE +++ b/vendor/github.com/DeanThompson/ginpprof/LICENSE @@ -1,6 +1,6 @@ -MIT License +The MIT License (MIT) -Copyright (c) 2016 Gin-Gonic +Copyright (c) 2015 Yangliang Li Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -19,3 +19,4 @@ 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. + diff --git a/vendor/github.com/DeanThompson/ginpprof/README.md b/vendor/github.com/DeanThompson/ginpprof/README.md new file mode 100644 index 0000000..f5d658b --- /dev/null +++ b/vendor/github.com/DeanThompson/ginpprof/README.md @@ -0,0 +1,68 @@ +ginpprof +======== + +[![GoDoc](https://godoc.org/github.com/DeanThompson/ginpprof?status.svg)](https://godoc.org/github.com/DeanThompson/ginpprof) +[![Build +Status](https://travis-ci.org/DeanThompson/ginpprof.svg?branch=master)](https://travis-ci.org/DeanThompson/ginpprof) + +A wrapper for [golang web framework gin](https://github.com/gin-gonic/gin) to use `net/http/pprof` easily. + +## Install + +First install ginpprof to your GOPATH using `go get`: + +```sh +go get github.com/DeanThompson/ginpprof +``` + +## Usage + +```go +package main + +import ( + "github.com/gin-gonic/gin" + + "github.com/DeanThompson/ginpprof" +) + +func main() { + router := gin.Default() + + router.GET("/ping", func(c *gin.Context) { + c.String(200, "pong") + }) + + // automatically add routers for net/http/pprof + // e.g. /debug/pprof, /debug/pprof/heap, etc. + ginpprof.Wrap(router) + + // ginpprof also plays well with *gin.RouterGroup + // group := router.Group("/debug/pprof") + // ginpprof.WrapGroup(group) + + router.Run(":8080") +} +``` + +Start this server, and you will see such outputs: + +```text +[GIN-debug] GET /ping --> main.main.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/ --> github.com/DeanThompson/ginpprof.IndexHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/heap --> github.com/DeanThompson/ginpprof.HeapHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/goroutine --> github.com/DeanThompson/ginpprof.GoroutineHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/block --> github.com/DeanThompson/ginpprof.BlockHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/threadcreate --> github.com/DeanThompson/ginpprof.ThreadCreateHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/cmdline --> github.com/DeanThompson/ginpprof.CmdlineHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/profile --> github.com/DeanThompson/ginpprof.ProfileHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.SymbolHandler.func1 (3 handlers) +[GIN-debug] POST /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.SymbolHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/trace --> github.com/DeanThompson/ginpprof.TraceHandler.func1 (3 handlers) +[GIN-debug] GET /debug/pprof/mutex --> github.com/DeanThompson/ginpprof.MutexHandler.func1 (3 handlers) +[GIN-debug] Listening and serving HTTP on :8080 +``` + +Now visit [http://127.0.0.1:8080/debug/pprof/](http://127.0.0.1:8080/debug/pprof/) and you'll see what you want. + +Have Fun. diff --git a/vendor/github.com/DeanThompson/ginpprof/pprof.go b/vendor/github.com/DeanThompson/ginpprof/pprof.go new file mode 100644 index 0000000..a76da61 --- /dev/null +++ b/vendor/github.com/DeanThompson/ginpprof/pprof.go @@ -0,0 +1,123 @@ +package ginpprof + +import ( + "net/http/pprof" + "strings" + + "github.com/gin-gonic/gin" +) + +// Wrap adds several routes from package `net/http/pprof` to *gin.Engine object +func Wrap(router *gin.Engine) { + WrapGroup(&router.RouterGroup) +} + +// Wrapper make sure we are backward compatible +var Wrapper = Wrap + +// WrapGroup adds several routes from package `net/http/pprof` to *gin.RouterGroup object +func WrapGroup(router *gin.RouterGroup) { + routers := []struct { + Method string + Path string + Handler gin.HandlerFunc + }{ + {"GET", "/debug/pprof/", IndexHandler()}, + {"GET", "/debug/pprof/heap", HeapHandler()}, + {"GET", "/debug/pprof/goroutine", GoroutineHandler()}, + {"GET", "/debug/pprof/block", BlockHandler()}, + {"GET", "/debug/pprof/threadcreate", ThreadCreateHandler()}, + {"GET", "/debug/pprof/cmdline", CmdlineHandler()}, + {"GET", "/debug/pprof/profile", ProfileHandler()}, + {"GET", "/debug/pprof/symbol", SymbolHandler()}, + {"POST", "/debug/pprof/symbol", SymbolHandler()}, + {"GET", "/debug/pprof/trace", TraceHandler()}, + {"GET", "/debug/pprof/mutex", MutexHandler()}, + } + + basePath := strings.TrimSuffix(router.BasePath(), "/") + var prefix string + + switch { + case basePath == "": + prefix = "" + case strings.HasSuffix(basePath, "/debug"): + prefix = "/debug" + case strings.HasSuffix(basePath, "/debug/pprof"): + prefix = "/debug/pprof" + } + + for _, r := range routers { + router.Handle(r.Method, strings.TrimPrefix(r.Path, prefix), r.Handler) + } +} + +// IndexHandler will pass the call from /debug/pprof to pprof +func IndexHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Index(ctx.Writer, ctx.Request) + } +} + +// HeapHandler will pass the call from /debug/pprof/heap to pprof +func HeapHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Handler("heap").ServeHTTP(ctx.Writer, ctx.Request) + } +} + +// GoroutineHandler will pass the call from /debug/pprof/goroutine to pprof +func GoroutineHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Handler("goroutine").ServeHTTP(ctx.Writer, ctx.Request) + } +} + +// BlockHandler will pass the call from /debug/pprof/block to pprof +func BlockHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Handler("block").ServeHTTP(ctx.Writer, ctx.Request) + } +} + +// ThreadCreateHandler will pass the call from /debug/pprof/threadcreate to pprof +func ThreadCreateHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Handler("threadcreate").ServeHTTP(ctx.Writer, ctx.Request) + } +} + +// CmdlineHandler will pass the call from /debug/pprof/cmdline to pprof +func CmdlineHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Cmdline(ctx.Writer, ctx.Request) + } +} + +// ProfileHandler will pass the call from /debug/pprof/profile to pprof +func ProfileHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Profile(ctx.Writer, ctx.Request) + } +} + +// SymbolHandler will pass the call from /debug/pprof/symbol to pprof +func SymbolHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Symbol(ctx.Writer, ctx.Request) + } +} + +// TraceHandler will pass the call from /debug/pprof/trace to pprof +func TraceHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Trace(ctx.Writer, ctx.Request) + } +} + +// MutexHandler will pass the call from /debug/pprof/mutex to pprof +func MutexHandler() gin.HandlerFunc { + return func(ctx *gin.Context) { + pprof.Handler("mutex").ServeHTTP(ctx.Writer, ctx.Request) + } +} diff --git a/vendor/github.com/gin-contrib/pprof/.travis.yml b/vendor/github.com/gin-contrib/pprof/.travis.yml deleted file mode 100644 index c39908c..0000000 --- a/vendor/github.com/gin-contrib/pprof/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -language: go -sudo: false - -go: - - 1.6.x - - 1.7.x - - 1.8.x - - tip - -script: - - go test -v -covermode=atomic -coverprofile=coverage.out - -after_success: - - bash <(curl -s https://codecov.io/bash) - -notifications: - webhooks: - urls: - - https://webhooks.gitter.im/e/acc2c57482e94b44f557 - on_success: change - on_failure: always - on_start: false diff --git a/vendor/github.com/gin-contrib/pprof/README.md b/vendor/github.com/gin-contrib/pprof/README.md deleted file mode 100644 index e82a4dd..0000000 --- a/vendor/github.com/gin-contrib/pprof/README.md +++ /dev/null @@ -1,83 +0,0 @@ -# pprof - -[![Build Status](https://travis-ci.org/gin-contrib/pprof.svg)](https://travis-ci.org/gin-contrib/pprof) -[![codecov](https://codecov.io/gh/gin-contrib/pprof/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/pprof) -[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/pprof)](https://goreportcard.com/report/github.com/gin-contrib/pprof) -[![GoDoc](https://godoc.org/github.com/gin-contrib/pprof?status.svg)](https://godoc.org/github.com/gin-contrib/pprof) -[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin) - -gin pprof middleware - -> Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool. - -## Usage - -### Start using it - -Download and install it: - -```bash -$ go get gopkg.in/gin-contrib/pprof.v1 -``` - -Import it in your code: - -```go -import "gopkg.in/gin-contrib/pprof.v1" -``` - -### Example: - -```go -package main - -import ( - "github.com/gin-contrib/pprof" - "github.com/gin-gonic/gin" -) - -func main() { - router := gin.Default() - pprof.Register(router, nil) - router.Run(":8080") -} -``` - -### change default path prefix: - -```go -func main() { - router := gin.Default() - pprof.Register(router, &pprof.Options{ - // default is "debug/pprof" - RoutePrefix: "debug/pprof", - }) - router.Run(":8080") -} -``` - -### Use the pprof tool - -Then use the pprof tool to look at the heap profile: - -```bash -go tool pprof http://localhost:8080/debug/pprof/heap -``` - -Or to look at a 30-second CPU profile: - -```bash -go tool pprof http://localhost:8080/debug/pprof/profile -``` - -Or to look at the goroutine blocking profile, after calling runtime.SetBlockProfileRate in your program: - -```bash -go tool pprof http://localhost:8080/debug/pprof/block -``` - -Or to collect a 5-second execution trace: - -```bash -wget http://localhost:8080/debug/pprof/trace?seconds=5 -``` diff --git a/vendor/github.com/gin-contrib/pprof/pprof.go b/vendor/github.com/gin-contrib/pprof/pprof.go deleted file mode 100644 index c04e631..0000000 --- a/vendor/github.com/gin-contrib/pprof/pprof.go +++ /dev/null @@ -1,42 +0,0 @@ -package pprof - -import ( - "net/http" - "net/http/pprof" - - "github.com/gin-gonic/gin" -) - -// Options provides potential route registration configuration options -type Options struct { - // RoutePrefix is an optional path prefix. If left unspecified, `/debug/pprof` - // is used as the default path prefix. - RoutePrefix string -} - -// Register the standard HandlerFuncs from the net/http/pprof package with -// the provided gin.Engine. opts is a optional. If a `nil` value is passed, -// the default path prefix is used. -func Register(r *gin.Engine, opts *Options) { - prefix := routePrefix(opts) - r.GET(prefix+"/block", pprofHandler(pprof.Index)) - r.GET(prefix+"/heap", pprofHandler(pprof.Index)) - r.GET(prefix+"/profile", pprofHandler(pprof.Profile)) - r.POST(prefix+"/symbol", pprofHandler(pprof.Symbol)) - r.GET(prefix+"/symbol", pprofHandler(pprof.Symbol)) - r.GET(prefix+"/trace", pprofHandler(pprof.Trace)) -} - -func pprofHandler(h http.HandlerFunc) gin.HandlerFunc { - handler := http.HandlerFunc(h) - return func(c *gin.Context) { - handler.ServeHTTP(c.Writer, c.Request) - } -} - -func routePrefix(opts *Options) string { - if opts == nil { - return "/debug/pprof" - } - return opts.RoutePrefix -}