-
Notifications
You must be signed in to change notification settings - Fork 6
/
gomonitor.go
72 lines (68 loc) · 2.24 KB
/
gomonitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Package gomonitor is a Gin middleware, that exposes metrics to an
// http monitor endpoint. As base it uses
// https://gopkg.in/mcuadros/go-monitor.v1 to expose metrics. It
// supports custom metrics which are not triggered using the
// middleware context. If you want to add a page counter please see
// the example. You can even create your own aspects like defined in
// the https://gopkg.in/mcuadros/go-monitor.v1/aspects package.
//
// Example:
// package main
//
// import (
// "net/http"
//
// "github.com/gin-gonic/gin"
// "github.com/szuecs/gin-gomonitor"
// "github.com/szuecs/gin-gomonitor/aspects"
// "gopkg.in/mcuadros/go-monitor.v1/aspects"
// )
//
// func main() {
// router := gin.New()
// counterAspect := &ginmon.CounterAspect{0}
// asps := []aspects.Aspect{counterAspect}
// // curl http://localhost:9000/Counter
// router.Use(ginmon.CounterHandler(counterAspect))
// // curl http://localhost:9000/
// gomonitor.Start(9000, asps)
// // last middleware
// router.Use(gin.Recovery())
//
// // each request to all handlers like below will increment the Counter
// router.GET("/", func(ctx *gin.Context) {
// ctx.JSON(http.StatusOK, gin.H{"title": "Counter - Hello World"})
// })
//
// //..
// router.Run(":8080")
// }
package gomonitor
import (
"fmt"
mon "gopkg.in/mcuadros/go-monitor.v1"
"gopkg.in/mcuadros/go-monitor.v1/aspects"
)
// Start function exposes metrics of
// https://github.com/mcuadros/go-monitor package of your
// https://github.com/gin-gonic/gin based webapp. Start() get a
// port number as parameter to expose monitoring data to and a slice
// of aspects.Aspect defined by the user.
//
// Example:
// router := gin.New()
// counterAspect := &ginmon.CounterAspect{0}
// asps := []aspects.Aspect{counterAspect}
// // curl http://localhost:9000/Counter
// router.Use(ginmon.CounterHandler(counterAspect))
// // curl http://localhost:9000/
// gomonitor.Start(9000, asps)
// // last middleware
// router.Use(gin.Recovery())
func Start(port int, asps []aspects.Aspect) {
var monitor *mon.Monitor = mon.NewMonitor(fmt.Sprintf(":%d", port))
for _, aspect := range asps {
monitor.AddAspect(aspect)
}
go monitor.Start()
}