Skip to content

Commit

Permalink
feature: add expose metric path to separate port, and change docs
Browse files Browse the repository at this point in the history
  • Loading branch information
penglongli committed Sep 24, 2021
1 parent 03b7643 commit 9257eae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ gin-gonic/gin metrics exporter for Prometheus.

[中文](README_zh.md)

- [Introduction](#Introduction)
- [Grafana](#Grafana)
- [Installation](#Installation)
- [Usage](#Usage)
- [Custom Metric](#Custom-Metric)
- [Metric with separate port](#Metric-with-separate-port)
- [Contributing](#Contributing)

## Introduction

`gin-metrics` defines some metrics for gin http-server. There have easy way to use it.
Expand Down Expand Up @@ -129,6 +137,35 @@ With `Counter` type metric, you can use `Inc` and `Add` function, don't use `Set

For `Histogram` and `Summary` type metric, should use `Observe` function.

## Metric with separate port

For some users, they don't want to merge the port of the metric with the port of the application.

So we provide a way to separate the metric port. Here is the example.

```go
func main() {
appRouter := gin.Default()
metricRouter := gin.Default()

m := ginmetrics.GetMonitor()
// use metric middleware without expose metric path
m.UseWithoutExposingEndpoint(appRouter)
// set metric path expose to metric router
m.Expose(metricRouter)

appRouter.GET("/product/:id", func(ctx *gin.Context) {
ctx.JSON(200, map[string]string{
"productId": ctx.Param("id"),
})
})
go func() {
_ = metricRouter.Run(":8081")
}()
_ = appRouter.Run(":8080")
}
```

## Contributing

If someone has a problem or suggestions, you can [new issues](https://github.com/penglongli/gin-metrics/issues/new)
Expand Down
4 changes: 2 additions & 2 deletions ginmetrics/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (m *Monitor) UseWithoutExposingEndpoint(r gin.IRoutes) {
}

// Expose adds metric path to a given router.
// The router can be different than the one passed to UseWithoutExposingEndpoint.
// This alows to expose metrics on different port.
// The router can be different with the one passed to UseWithoutExposingEndpoint.
// This allows to expose metrics on different port.
func (m *Monitor) Expose(r gin.IRoutes) {
r.GET(m.metricPath, func(ctx *gin.Context) {
promhttp.Handler().ServeHTTP(ctx.Writer, ctx.Request)
Expand Down

0 comments on commit 9257eae

Please sign in to comment.