1. change pprof module
2. remove memory log
3. remove line hook log
4. send line log to es
This commit is contained in:
Jay 2018-10-12 16:15:47 +08:00
parent a6b8c9ef8a
commit 08ae3289ee
12 changed files with 232 additions and 171 deletions

18
Gopkg.lock generated
View File

@ -1,6 +1,14 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. # 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]] [[projects]]
digest = "1:a62f6ed230a8cd138a9efbe718e7d0b0294f139266f5f55cd942769a9aac8de2" digest = "1:a62f6ed230a8cd138a9efbe718e7d0b0294f139266f5f55cd942769a9aac8de2"
name = "github.com/PuerkitoBio/goquery" name = "github.com/PuerkitoBio/goquery"
@ -44,14 +52,6 @@
revision = "cf4846e6a636a76237a28d9286f163c132e841bc" revision = "cf4846e6a636a76237a28d9286f163c132e841bc"
version = "v1.2" version = "v1.2"
[[projects]]
digest = "1:fd66f3d5f4398879b0051e46e8421abdc93b5a6a64b645dc80986157fb99308d"
name = "github.com/gin-contrib/pprof"
packages = ["."]
pruneopts = "UT"
revision = "cea200e02ddddca4b843d75d32aa13c413549de9"
version = "v1.1"
[[projects]] [[projects]]
branch = "master" branch = "master"
digest = "1:36fe9527deed01d2a317617e59304eb2c4ce9f8a24115bcc5c2e37b3aee5bae4" digest = "1:36fe9527deed01d2a317617e59304eb2c4ce9f8a24115bcc5c2e37b3aee5bae4"
@ -257,9 +257,9 @@
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
input-imports = [ input-imports = [
"github.com/DeanThompson/ginpprof",
"github.com/PuerkitoBio/goquery", "github.com/PuerkitoBio/goquery",
"github.com/gin-contrib/cors", "github.com/gin-contrib/cors",
"github.com/gin-contrib/pprof",
"github.com/gin-gonic/contrib/sessions", "github.com/gin-gonic/contrib/sessions",
"github.com/gin-gonic/gin", "github.com/gin-gonic/gin",
"github.com/gin-gonic/gin/binding", "github.com/gin-gonic/gin/binding",

15
main.go
View File

@ -4,7 +4,6 @@ import (
"encoding/gob" "encoding/gob"
"errors" "errors"
"flag" "flag"
"fmt"
"log" "log"
"os" "os"
"path" "path"
@ -102,13 +101,13 @@ func PrintMemUsage() {
var m runtime.MemStats var m runtime.MemStats
runtime.ReadMemStats(&m) runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats // For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc)) // fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc)) // fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys)) // fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC) // fmt.Printf("\tNumGC = %v\n", m.NumGC)
fmt.Printf("HeapAlloc = %v MiB", bToMb(m.HeapAlloc)) // fmt.Printf("HeapAlloc = %v MiB", bToMb(m.HeapAlloc))
fmt.Printf("\t HeapSys = %v MiB", bToMb(m.HeapSys)) // fmt.Printf("\t HeapSys = %v MiB", bToMb(m.HeapSys))
fmt.Printf("\t NextGC = %v MiB\n", bToMb(m.NextGC)) // fmt.Printf("\t NextGC = %v MiB\n", bToMb(m.NextGC))
obj := map[string]interface{}{ obj := map[string]interface{}{
"Alloc": m.Alloc, "Alloc": m.Alloc,

View File

@ -5,9 +5,10 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"git.trj.tw/golang/mtfosbot/module/es"
"git.trj.tw/golang/mtfosbot/module/config" "git.trj.tw/golang/mtfosbot/module/config"
"git.trj.tw/golang/mtfosbot/module/context" "git.trj.tw/golang/mtfosbot/module/context"
"git.trj.tw/golang/mtfosbot/module/line-message" "git.trj.tw/golang/mtfosbot/module/line-message"
@ -72,7 +73,13 @@ func GetLineMessage(c *context.Context) {
c.DataFormat("body type error") 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 := struct {
Events []*lineobj.EventObject `json:"events"` Events []*lineobj.EventObject `json:"events"`

View File

@ -12,8 +12,8 @@ import (
"git.trj.tw/golang/mtfosbot/router/private" "git.trj.tw/golang/mtfosbot/router/private"
"git.trj.tw/golang/mtfosbot/router/rimg" "git.trj.tw/golang/mtfosbot/router/rimg"
"git.trj.tw/golang/mtfosbot/router/twitch" "git.trj.tw/golang/mtfosbot/router/twitch"
"github.com/DeanThompson/ginpprof"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -105,5 +105,5 @@ func SetRoutes(r *gin.Engine) {
} }
// set pprof router // set pprof router
pprof.Register(r, nil) ginpprof.Wrap(r)
} }

View File

@ -22,4 +22,3 @@ _testmain.go
*.exe *.exe
*.test *.test
*.prof *.prof
coverage*

11
vendor/github.com/DeanThompson/ginpprof/.travis.yml generated vendored Normal file
View File

@ -0,0 +1,11 @@
language: go
go:
- 1.5
- 1.6
- 1.7
- 1.8
- tip
script:
- go test

View File

@ -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 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal 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, 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.

68
vendor/github.com/DeanThompson/ginpprof/README.md generated vendored Normal file
View File

@ -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.

123
vendor/github.com/DeanThompson/ginpprof/pprof.go generated vendored Normal file
View File

@ -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)
}
}

View File

@ -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

View File

@ -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
```

View File

@ -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
}