Skip to content

Commit

Permalink
添加基本的流量判断
Browse files Browse the repository at this point in the history
  • Loading branch information
6boris committed May 12, 2019
1 parent de742d5 commit e4220cd
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 45 deletions.
63 changes: 61 additions & 2 deletions README.md
@@ -1,2 +1,61 @@
# gin_exporter
Exporter for gin metrics
# The Gin Framework Metrics Middleware


![gin_metrics_v1](https://oss.kyle.link/images/2019/gin_metrics_v1.png)


## Preface
Many small companies don't have such a large architecture for micro-services when they do websites. A simple solution for viewing application traffic is very important. This repository is a middleware that integrates seamlessly with Gin.

## How to use

* install the metrics lib

```bash
go get github.com/kylesliu/gin_metrics
```

* run the server

```go
package main

import (
"github.com/gin-gonic/gin"
"github.com/kylesliu/gin_metrics"
)

func main() {
app := gin.Default()
gin.SetMode(gin.DebugMode)

app.GET("demo1", func(c *gin.Context) {
c.JSON(200, gin.H{
"code": 200,
"msg": "demo1",
})
})

gin_metrics.Default(app)

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


* Config the Prometheus

```yaml
- job_name: 'gin_metrics'
static_configs:
- targets: ['localhost:9000']
```

* Config the Grafana

[Grafana Dashboard](https://snapshot.raintank.io/dashboard/snapshot/YELhgZTaIuynoKd3UPudNJdNBgDy83CC)

## Last
If you have any good suggestions to mention issue or PR, I will check it out in detail.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -4,5 +4,5 @@ go 1.12

require (
github.com/gin-gonic/gin v1.4.0
github.com/prometheus/client_golang v0.9.2 // indirect
github.com/prometheus/client_golang v0.9.2
)
65 changes: 23 additions & 42 deletions metrics.go
@@ -1,20 +1,19 @@
package gin_metrics

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

var (
taskCounter = prometheus.NewCounter(prometheus.CounterOpts{
Subsystem: "workpool",
Name: "complete_task_total",
Help: "Total number of task completed",
})
GinRequestTotalCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gin_reqeust_total",
Help: "Number of hello requests in total",
},
[]string{"method", "path"},
)
)

type GinRoutesInfo struct {
Expand All @@ -26,49 +25,31 @@ type GinRoutesInfo struct {
var routeInfo []GinRoutesInfo

func init() {
prometheus.MustRegister(taskCounter)
//prometheus.MustRegister(taskCounter)
prometheus.MustRegister(GinRequestTotalCount)
//prometheus.MustRegister(respTime)
//prometheus.MustRegister(respSum)
}

func Default(app *gin.Engine) *gin.Engine {
func Default(app *gin.Engine) {
app.Use(metricsMiddleware)

metricsHandler(app)
app.GET("metrics", metricsHandler())
//app.GET("metrics.json", metricsHandlerJson)
generateRouteInfo(app)
return app
}

func metricsMiddleware(c *gin.Context) {
//path := c.Request.URL.Path
start := time.Now()
c.Next()
taskCounter.Add(1)
//fmt.Println(c.ClientIP(), c.Request.URL.Path, start.Sub(time.Now()))
fmt.Println(time.Now().Sub(start))
return
// Gin Metrics api handler
func metricsHandler() gin.HandlerFunc {
h := promhttp.Handler()
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}

// metrics 接口
func metricsHandler(app *gin.Engine) {

app.GET("metrics", func(c *gin.Context) {
c.JSON(200, gin.H{
"code": 200,
"msg": "metrics",
"routes": routeInfo,
})
func metricsHandlerJson(c *gin.Context) {
c.JSON(200, gin.H{
"metrics": routeInfo,
})

//go func() {
// for {
// taskCounter.Add(1)
// time.Sleep(1 * time.Second)
// }
//}()

http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":8888", nil); err != nil {
panic(err)
}
}

// 初始化 Router信息
Expand Down
22 changes: 22 additions & 0 deletions middleware.go
@@ -1 +1,23 @@
package gin_metrics

import "C"
import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
)

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

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

//start := time.Now()
c.Next()
//fmt.Println(c.ClientIP(), c.Request.URL.Path, start.Sub(time.Now()))
//costTime := time.Since(start)
//respTime.WithLabelValues(fmt.Sprintf("%d", costTime)).Observe(costTime.Seconds())

//

//fmt.Println(path)
}

0 comments on commit e4220cd

Please sign in to comment.