Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |