Skip to content

Commit 9257eae

Browse files
committed
feature: add expose metric path to separate port, and change docs
1 parent 03b7643 commit 9257eae

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ gin-gonic/gin metrics exporter for Prometheus.
33

44
[中文](README_zh.md)
55

6+
- [Introduction](#Introduction)
7+
- [Grafana](#Grafana)
8+
- [Installation](#Installation)
9+
- [Usage](#Usage)
10+
- [Custom Metric](#Custom-Metric)
11+
- [Metric with separate port](#Metric-with-separate-port)
12+
- [Contributing](#Contributing)
13+
614
## Introduction
715

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

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

140+
## Metric with separate port
141+
142+
For some users, they don't want to merge the port of the metric with the port of the application.
143+
144+
So we provide a way to separate the metric port. Here is the example.
145+
146+
```go
147+
func main() {
148+
appRouter := gin.Default()
149+
metricRouter := gin.Default()
150+
151+
m := ginmetrics.GetMonitor()
152+
// use metric middleware without expose metric path
153+
m.UseWithoutExposingEndpoint(appRouter)
154+
// set metric path expose to metric router
155+
m.Expose(metricRouter)
156+
157+
appRouter.GET("/product/:id", func(ctx *gin.Context) {
158+
ctx.JSON(200, map[string]string{
159+
"productId": ctx.Param("id"),
160+
})
161+
})
162+
go func() {
163+
_ = metricRouter.Run(":8081")
164+
}()
165+
_ = appRouter.Run(":8080")
166+
}
167+
```
168+
132169
## Contributing
133170

134171
If someone has a problem or suggestions, you can [new issues](https://github.com/penglongli/gin-metrics/issues/new)

ginmetrics/middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (m *Monitor) UseWithoutExposingEndpoint(r gin.IRoutes) {
4242
}
4343

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

0 commit comments

Comments
 (0)