Skip to content

Commit 48084db

Browse files
committed
do not set url if DoNotUseRequestPathFor404 is true
1 parent 1606e32 commit 48084db

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

echoprometheus/prometheus.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ type MiddlewareConfig struct {
7474

7575
timeNow func() time.Time
7676

77-
// If DoNotUseURLFor404 is true, all 404 responses (due to non-matching route) will have the same `url` label and
77+
// If DoNotUseRequestPathFor404 is true, all 404 responses (due to non-matching route) will have the same `url` label and
7878
// thus won't generate new metrics.
79-
DoNotUseURLFor404 bool
79+
DoNotUseRequestPathFor404 bool
8080
}
8181

8282
type LabelValueFunc func(c echo.Context, err error) string
@@ -250,7 +250,7 @@ func (conf MiddlewareConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
250250
}
251251

252252
url := c.Path() // contains route path ala `/users/:id`
253-
if url == "" {
253+
if url == "" && !conf.DoNotUseRequestPathFor404 {
254254
// as of Echo v4.10.1 path is empty for 404 cases (when router did not find any matching routes)
255255
// in this case we use actual path from request to have some distinction in Prometheus
256256
url = c.Request().URL.Path
@@ -271,10 +271,7 @@ func (conf MiddlewareConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
271271
values[0] = strconv.Itoa(status)
272272
values[1] = c.Request().Method
273273
values[2] = c.Request().Host
274-
// as of Echo v4.10.1 an empty c.Path() means the router did not find any matching routes (404)
275-
if c.Path() != "" || !conf.DoNotUseURLFor404 {
276-
values[3] = url
277-
}
274+
values[3] = url
278275
for _, cv := range customValuers {
279276
values[cv.index] = cv.valueFunc(c, err)
280277
}

echoprometheus/prometheus_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func TestRunPushGatewayGatherer(t *testing.T) {
283283
func TestSetPathFor404NoMatchingRoute(t *testing.T) {
284284
e := echo.New()
285285

286-
e.Use(NewMiddlewareWithConfig(MiddlewareConfig{DoNotUseURLFor404: true, Subsystem: defaultSubsystem}))
286+
e.Use(NewMiddlewareWithConfig(MiddlewareConfig{DoNotUseRequestPathFor404: true, Subsystem: defaultSubsystem}))
287287
e.GET("/metrics", NewHandler())
288288

289289
assert.Equal(t, http.StatusNotFound, request(e, "/nonExistentPath"))
@@ -301,7 +301,7 @@ func TestSetPathFor404Logic(t *testing.T) {
301301
unregisterDefaults("myapp")
302302
e := echo.New()
303303

304-
e.Use(NewMiddlewareWithConfig(MiddlewareConfig{DoNotUseURLFor404: true, Subsystem: defaultSubsystem}))
304+
e.Use(NewMiddlewareWithConfig(MiddlewareConfig{DoNotUseRequestPathFor404: true, Subsystem: defaultSubsystem}))
305305
e.GET("/metrics", NewHandler())
306306

307307
e.GET("/sample", echo.NotFoundHandler)

0 commit comments

Comments
 (0)