Skip to content

Commit

Permalink
add some collect
Browse files Browse the repository at this point in the history
  • Loading branch information
6boris committed May 18, 2019
1 parent a25f852 commit 2c0a723
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 15 deletions.
9 changes: 9 additions & 0 deletions glide.yaml
@@ -0,0 +1,9 @@
package: github.com/kylesliu/gin_metrics
import:
- package: github.com/gin-gonic/gin
version: ^1.4.0
- package: github.com/prometheus/client_golang
version: ^0.9.2
subpackages:
- prometheus
- prometheus/promhttp
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -6,3 +6,4 @@ require (
github.com/gin-gonic/gin v1.4.0
github.com/prometheus/client_golang v0.9.2
)

13 changes: 13 additions & 0 deletions go.sum
@@ -1,6 +1,7 @@
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
Expand All @@ -25,17 +26,29 @@ github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7q
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nLJdBg+pBmGgkJlSaKC2KaQmTCk1XDtE=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190514140710-3ec191127204/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190516014833-cab07311ab81/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190516015132-d1a3278ee749/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
Expand Down
75 changes: 69 additions & 6 deletions metrics.go
@@ -1,21 +1,64 @@
package gin_metrics

import (
"context"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
)

var (
GinRequestTotalCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gin_reqeust_total",
Name: "gin_request_total",
Help: "Number of hello requests in total",
},
[]string{"method", "path"},
[]string{"method", "endpoint"},
)

GinRequestGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "gin_request_total2",
Help: "Number of hello requests in total",
},
[]string{"method", "endpoint"},
)

GinRequestHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "gin_request_total3",
Help: "Number of hello requests in total",
Buckets: []float64{},
},
[]string{"method", "endpoint"},
)

GinRequestSummary = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "gin_request_time_millisecond",
Help: "Number of hello requests in total",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"method", "endpoint"},
)
)

type Context struct {
Request *http.Request

index int8

// Keys is a key/value pair exclusively for the context of each request.
Keys map[string]interface{}

// Errors is a list of errors attached to all the handlers/middlewares who used this context.

// Accepted defines a list of manually accepted formats for content negotiation.
Accepted []string
}

type GinRoutesInfo struct {
Method string
Path string
Expand All @@ -25,10 +68,29 @@ type GinRoutesInfo struct {
var routeInfo []GinRoutesInfo

func init() {
//prometheus.MustRegister(taskCounter)
prometheus.MustRegister(GinRequestTotalCount)
//prometheus.MustRegister(respTime)
//prometheus.MustRegister(respSum)
//prometheus.MustRegister(GinRequestTotalCount)
//prometheus.MustRegister(GinRequestGauge)
//prometheus.MustRegister(GinRequestHistogram)
//prometheus.MustRegister(GinRequestSummary)
prometheus.MustRegister(GinRequestTotalCount, GinRequestGauge, GinRequestHistogram, GinRequestSummary)
//registerHandler()
}

func registerHandler() {
if err := prometheus.Register(GinRequestTotalCount); err != nil {
log.Panicln(err.Error())
}

if err := prometheus.Register(GinRequestGauge); err != nil {
log.Panicln(err.Error())
}

if err := prometheus.Register(GinRequestHistogram); err != nil {
log.Panicln(err.Error())
}
if err := prometheus.Register(GinRequestSummary); err != nil {
log.Panicln(err.Error())
}
}

func Default(app *gin.Engine) {
Expand All @@ -54,6 +116,7 @@ func metricsHandlerJson(c *gin.Context) {

// 初始化 Router信息
func generateRouteInfo(app *gin.Engine) {
context.Background()
for _, v := range app.Routes() {
routeInfo = append(routeInfo, GinRoutesInfo{
Method: v.Method,
Expand Down
14 changes: 7 additions & 7 deletions metrics_test.go
Expand Up @@ -36,13 +36,13 @@ func TestDefault(t *testing.T) {
})
}

//if err := app.Run("127.0.0.1:9000"); err != nil {
// panic(err.Error())
//}
if err := app.Run("127.0.0.1:9000"); err != nil {
panic(err.Error())
}
}

func BenchmarkDefault(b *testing.B) {
b.N = 10000
b.N = 1000
var wg sync.WaitGroup
wg.Add(1)

Expand All @@ -55,7 +55,7 @@ func BenchmarkDefault(b *testing.B) {
}
}()
}

//
go func() {
for i := 0; i < b.N; i++ {
Req("http://127.0.0.1:9000/rand_sleep")
Expand All @@ -67,7 +67,7 @@ func BenchmarkDefault(b *testing.B) {
}
}()
time.Sleep(10 * time.Second)
//wg.Wait()
wg.Wait()

}

Expand All @@ -84,7 +84,7 @@ func getRandomString(str_len int) string {
}

func Req(url string) {
time.Sleep(time.Duration(time.Duration(rand.Intn(100)) * time.Millisecond))
time.Sleep(time.Duration(time.Duration(rand.Intn(1000)) * time.Millisecond))

resp, err := http.Get(url)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions middleware.go
Expand Up @@ -4,12 +4,19 @@ import "C"
import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"time"
)

func metricsMiddleware(c *gin.Context) {
//path := c.Request.URL.Path
start := time.Now().UnixNano()

GinRequestTotalCount.With(prometheus.Labels{"method": c.Request.Method, "path": c.Request.URL.Path}).Inc()
// Request time
GinRequestTotalCount.With(prometheus.Labels{"method": c.Request.Method, "endpoint": c.Request.URL.Path}).Inc()

//GinRequestGauge.With(prometheus.Labels{"method": c.Request.Method, "path": c.Request.URL.Path}).
//fmt.Println(time.Now().UnixNano() - start)
//fmt.Printf("时间戳(毫秒):%v;\n", time.Now().Sub(start))
//fmt.Printf("时间戳(毫秒):%v;\n", time.Now().UnixNano()/1e6)

//start := time.Now()
c.Next()
Expand All @@ -18,6 +25,13 @@ func metricsMiddleware(c *gin.Context) {
//respTime.WithLabelValues(fmt.Sprintf("%d", costTime)).Observe(costTime.Seconds())

//
end := time.Now().UnixNano()
use_time := end - start
GinRequestSummary.With(prometheus.Labels{"method": c.Request.Method, "endpoint": c.Request.URL.Path}).Observe(float64(use_time))

//use_time := float64(end / 1e6)
//fmt.Println((end - start) / 1e6)
//fmt.Println()

//fmt.Println(path)
}

0 comments on commit 2c0a723

Please sign in to comment.