Git Product home page Git Product logo

toolbox's Introduction

toolbox

GitHub Workflow Status codecov GoDoc Sourcegraph

Middleware toolbox provides health chcek, pprof, profile and statistic services for Macaron.

API Reference

Installation

go get github.com/go-macaron/toolbox

Usage

// main.go
import (
	"gopkg.in/macaron.v1"
	"github.com/go-macaron/toolbox"
)

func main() {
  	m := macaron.Classic()
  	m.Use(toolbox.Toolboxer(m))
	m.Run()
}

Open your browser and visit http://localhost:4000/debug to see the effects.

Options

toolbox.Toolboxer comes with a variety of configuration options:

type dummyChecker struct {
}

func (dc *dummyChecker) Desc() string {
	return "Dummy checker"
}

func (dc *dummyChecker) Check() error {
	return nil
}

// ...
m.Use(toolbox.Toolboxer(m, toolbox.Options{
	URLPrefix:			"/debug",			// URL prefix for toolbox dashboard
	HealthCheckURL:		"/healthcheck", 	// URL for health check request
	HealthCheckers: []HealthChecker{
		new(dummyChecker),
	},										// Health checkers
	HealthCheckFuncs: []*toolbox.HealthCheckFuncDesc{
		&toolbox.HealthCheckFuncDesc{
			Desc: "Database connection",
			Func: func() error { return "OK" },
		},
	},										// Health check functions
	DisableDebug:		false,				// Turns off all debug functionality when true
	PprofURLPrefix:		"/debug/pprof/", 	// URL prefix of pprof
	ProfileURLPrefix:	"/debug/profile/", 	// URL prefix of profile
	ProfilePath:		"profile",			// Path store profile files
}))
// ...

Route Statistic

Toolbox also comes with a route call statistic functionality:

import (
	"os"
	"time"
	//...
	"github.com/go-macaron/toolbox"
)

func main() {
	//...
	m.Get("/", func(t toolbox.Toolbox) {
		start := time.Now()
		
		// Other operations.
		
		t.AddStatistics("GET", "/", time.Since(start))
	})
	
	m.Get("/dump", func(t toolbox.Toolbox) {
		t.GetMap(os.Stdout)
	})
}

Output take from test:

+---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+
| Request URL                                       | Method     | Times            | Total Used(s)    | Max Used(μs)     | Min Used(μs)     | Avg Used(μs)     |
+---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+
| /api/user                                         | POST       |                2 |         0.000122 |       120.000000 |         2.000000 |        61.000000 |
| /api/user                                         | GET        |                1 |         0.000013 |        13.000000 |        13.000000 |        13.000000 |
| /api/user                                         | DELETE     |                1 |         0.000001 |         1.400000 |         1.400000 |         1.400000 |
| /api/admin                                        | POST       |                1 |         0.000014 |        14.000000 |        14.000000 |        14.000000 |
| /api/user/unknwon                                 | POST       |                1 |         0.000012 |        12.000000 |        12.000000 |        12.000000 |
+---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+

License

This project is under Apache v2 License. See the LICENSE file for the full license text.

toolbox's People

Contributors

ipfans avatar unknwon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

toolbox's Issues

Add option to disable pprof and profile

Can I turn off /debug somehow? I want the healthchecks and statistics functionality, but I really don't want the profiling stuff when macaron.Env = "production".

无法通过编译 t.AddStatistics undefined (type *toolbox.Toolbox is pointer to interface, not interface)

最简单的代码

package main

import (
    "time"

    "github.com/go-macaron/toolbox"
    "gopkg.in/macaron.v1"
)

func main() {
    m := macaron.Classic()
    m.Use(toolbox.Toolboxer(m))
    m.Get("/", func(t *toolbox.Toolbox) {
        start := time.Now()

        // Other operations.

        t.AddStatistics("GET", "/", time.Since(start))
    })
    m.Run()
}

在go1.5rc1提示.\main.go:18: t.AddStatistics undefined (type *toolbox.Toolbox is pointer to interface, not interface)

例子已经不兼容新版代码

首页中的例子:

package main

import (
    "github.com/Unknwon/macaron"
    "github.com/macaron-contrib/toolbox"
)

func main() {
    m := macaron.Classic()
    m.Use(toolbox.Toolboxer(m))
    m.Run()
}

在新的macaron和toolbox下会出现panic,具体log如下:

[Macaron] Started GET /debug for 127.0.0.1
[Macaron] PANIC: runtime error: invalid memory address or nil pointer dereferenc
e
c:/go/src/runtime/panic.go:387 (0x410816)
c:/go/src/runtime/panic.go:42 (0x40fd85)
c:/go/src/runtime/os_windows.go:42 (0x40fb46)
D:/Software/pkg/src/github.com/macaron-contrib/toolbox/toolbox.go:93 (0x44cac8)
        dashboard: </ol>`, opt.PprofURLPrefix, opt.ProfileURLPrefix)))
c:/go/src/runtime/asm_386.s:412 (0x4335c1)
c:/go/src/reflect/value.go:419 (0x4b82d9)
c:/go/src/reflect/value.go:296 (0x4b7465)
D:/Software/pkg/src/github.com/Unknwon/macaron/inject/inject.go:102 (0x4f21aa)
        (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:113 (0x435f90)
        (*Context).run: vals, err := c.Invoke(c.handler())
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:104 (0x435ebf)
        (*Context).Next: c.run()
D:/Software/pkg/src/github.com/Unknwon/macaron/recovery.go:161 (0x442af8)
        func.008: c.Next()
c:/go/src/runtime/asm_386.s:412 (0x4335c1)
c:/go/src/reflect/value.go:419 (0x4b82d9)
c:/go/src/reflect/value.go:296 (0x4b7465)
D:/Software/pkg/src/github.com/Unknwon/macaron/inject/inject.go:102 (0x4f21aa)
        (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:113 (0x435f90)
        (*Context).run: vals, err := c.Invoke(c.handler())
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:104 (0x435ebf)
        (*Context).Next: c.run()
D:/Software/pkg/src/github.com/Unknwon/macaron/logger.go:40 (0x441cf5)
        func.004: ctx.Next()
c:/go/src/runtime/asm_386.s:412 (0x4335c1)
c:/go/src/reflect/value.go:419 (0x4b82d9)
c:/go/src/reflect/value.go:296 (0x4b7465)
D:/Software/pkg/src/github.com/Unknwon/macaron/inject/inject.go:102 (0x4f21aa)
        (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:113 (0x435f90)
        (*Context).run: vals, err := c.Invoke(c.handler())
D:/Software/pkg/src/github.com/Unknwon/macaron/router.go:156 (0x442f74)
        func.015: c.run()
D:/Software/pkg/src/github.com/Unknwon/macaron/router.go:243 (0x43c489)
        (*Router).ServeHTTP: h(rw, req, p)
D:/Software/pkg/src/github.com/Unknwon/macaron/macaron.go:168 (0x439606)
        (*Macaron).ServeHTTP: m.Router.ServeHTTP(rw, req)
c:/go/src/net/http/server.go:1703 (0x4971c5)
c:/go/src/net/http/server.go:1204 (0x495348)
c:/go/src/runtime/asm_386.s:2287 (0x4350f1)
[Macaron] Completed /debug 500 Internal Server Error in 5.0003ms
[Macaron] Started GET /favicon.ico for 127.0.0.1
[Macaron] Completed /favicon.ico 404 Not Found in 0
[Macaron] Started GET /debug for [::1]
[Macaron] PANIC: runtime error: invalid memory address or nil pointer dereferenc
e
c:/go/src/runtime/panic.go:387 (0x410816)
c:/go/src/runtime/panic.go:42 (0x40fd85)
c:/go/src/runtime/os_windows.go:42 (0x40fb46)
D:/Software/pkg/src/github.com/macaron-contrib/toolbox/toolbox.go:93 (0x44cac8)
        dashboard: </ol>`, opt.PprofURLPrefix, opt.ProfileURLPrefix)))
c:/go/src/runtime/asm_386.s:412 (0x4335c1)
c:/go/src/reflect/value.go:419 (0x4b82d9)
c:/go/src/reflect/value.go:296 (0x4b7465)
D:/Software/pkg/src/github.com/Unknwon/macaron/inject/inject.go:102 (0x4f21aa)
        (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:113 (0x435f90)
        (*Context).run: vals, err := c.Invoke(c.handler())
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:104 (0x435ebf)
        (*Context).Next: c.run()
D:/Software/pkg/src/github.com/Unknwon/macaron/recovery.go:161 (0x442af8)
        func.008: c.Next()
c:/go/src/runtime/asm_386.s:412 (0x4335c1)
c:/go/src/reflect/value.go:419 (0x4b82d9)
c:/go/src/reflect/value.go:296 (0x4b7465)
D:/Software/pkg/src/github.com/Unknwon/macaron/inject/inject.go:102 (0x4f21aa)
        (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:113 (0x435f90)
        (*Context).run: vals, err := c.Invoke(c.handler())
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:104 (0x435ebf)
        (*Context).Next: c.run()
D:/Software/pkg/src/github.com/Unknwon/macaron/logger.go:40 (0x441cf5)
        func.004: ctx.Next()
c:/go/src/runtime/asm_386.s:412 (0x4335c1)
c:/go/src/reflect/value.go:419 (0x4b82d9)
c:/go/src/reflect/value.go:296 (0x4b7465)
D:/Software/pkg/src/github.com/Unknwon/macaron/inject/inject.go:102 (0x4f21aa)
        (*injector).Invoke: return reflect.ValueOf(f).Call(in), nil
D:/Software/pkg/src/github.com/Unknwon/macaron/context.go:113 (0x435f90)
        (*Context).run: vals, err := c.Invoke(c.handler())
D:/Software/pkg/src/github.com/Unknwon/macaron/router.go:156 (0x442f74)
        func.015: c.run()
D:/Software/pkg/src/github.com/Unknwon/macaron/router.go:243 (0x43c489)
        (*Router).ServeHTTP: h(rw, req, p)
D:/Software/pkg/src/github.com/Unknwon/macaron/macaron.go:168 (0x439606)
        (*Macaron).ServeHTTP: m.Router.ServeHTTP(rw, req)
c:/go/src/net/http/server.go:1703 (0x4971c5)
c:/go/src/net/http/server.go:1204 (0x495348)
c:/go/src/runtime/asm_386.s:2287 (0x4350f1)
[Macaron] Completed /debug 500 Internal Server Error in 5.0003ms

Toolbox缺少JSON函数

在文档里能看到

 func (m *UrlMap) GetMap(w io.Writer) 
    func (m *UrlMap) JSON(w io.Writer) 

而用toolbox.Toolbox的时候有GetMap却没有JSON,应该是toolbox:40行这里

type Toolbox interface {
    AddHealthCheck(HealthChecker)
    AddHealthCheckFunc(string, HealthCheckFunc)
    AddStatistics(string, string, time.Duration)
    GetMap(io.Writer)
}

这里缺少个
JSON(io.Writer)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.