From 9257eae2e682946fc68fc39f850cd11a1519d3ec Mon Sep 17 00:00:00 2001 From: pelenli Date: Fri, 24 Sep 2021 17:41:45 +0800 Subject: [PATCH] feature: add expose metric path to separate port, and change docs --- README.md | 37 +++++++++++++++++++++++++++++++++++++ ginmetrics/middleware.go | 4 ++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4721bcb..7c44c26 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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) diff --git a/ginmetrics/middleware.go b/ginmetrics/middleware.go index e5c670f..ce3c305 100644 --- a/ginmetrics/middleware.go +++ b/ginmetrics/middleware.go @@ -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)