From 2e594507fd99d8b9e50e1f2945d814d91cf91d33 Mon Sep 17 00:00:00 2001 From: Alex Arwine Date: Wed, 17 Nov 2021 20:23:11 -0800 Subject: [PATCH 1/2] Reduce HandlerID cardinality by default in gin middleware Here's the doc for FullPath() ``` FullPath returns a matched route full path. For not found routes returns an empty string. router.GET("/user/:id", func(c *gin.Context) { c.FullPath() == "/user/:id" // true }) ``` --- middleware/gin/gin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/gin/gin.go b/middleware/gin/gin.go index 06ca7a1..b106c7e 100644 --- a/middleware/gin/gin.go +++ b/middleware/gin/gin.go @@ -27,7 +27,7 @@ func (r *reporter) Method() string { return r.c.Request.Method } func (r *reporter) Context() context.Context { return r.c.Request.Context() } -func (r *reporter) URLPath() string { return r.c.Request.URL.Path } +func (r *reporter) URLPath() string { return r.c.FullPath() } func (r *reporter) StatusCode() int { return r.c.Writer.Status() } From 57e61a288f99b5e675d6b08c13cb2b2d2b76ad71 Mon Sep 17 00:00:00 2001 From: Alex Arwine Date: Wed, 17 Nov 2021 20:37:37 -0800 Subject: [PATCH 2/2] Fallback to Request.URL.Path if the route does not exist --- middleware/gin/gin.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/middleware/gin/gin.go b/middleware/gin/gin.go index b106c7e..164d02b 100644 --- a/middleware/gin/gin.go +++ b/middleware/gin/gin.go @@ -27,7 +27,13 @@ func (r *reporter) Method() string { return r.c.Request.Method } func (r *reporter) Context() context.Context { return r.c.Request.Context() } -func (r *reporter) URLPath() string { return r.c.FullPath() } +func (r *reporter) URLPath() string { + urlPath := r.c.FullPath() + if urlPath == "" { + return r.c.Request.URL.Path + } + return urlPath +} func (r *reporter) StatusCode() int { return r.c.Writer.Status() }