go-http-metrics icon indicating copy to clipboard operation
go-http-metrics copied to clipboard

Add IgnoredPaths option

Open angristan opened this issue 3 years ago • 7 comments

Close #100

With this diff:

diff --git a/examples/default/main.go b/examples/default/main.go
index b5bcc33..3c69f1e 100644
--- a/examples/default/main.go
+++ b/examples/default/main.go
@@ -29,7 +29,8 @@ const (
 func main() {
 	// Create our middleware.
 	mdlw := middleware.New(middleware.Config{
-		Recorder: metrics.NewRecorder(metrics.Config{}),
+		Recorder:    metrics.NewRecorder(metrics.Config{}),
+		IgnoredPaths: []string{"/test1"},
 	})
 
 	// Create our server.

➜ curl localhost:8080/test1
➜ curl localhost:8080/test2

Resulting metrics:

➜ curl -s localhost:8081/metrics | grep -v '#' | grep -v 'go_' | grep -v promhttp
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="0.005"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="0.01"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="0.025"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="0.05"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="0.1"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="0.25"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="0.5"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="1"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="2.5"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="5"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="10"} 1
http_request_duration_seconds_bucket{code="204",handler="/test2",method="GET",service="",le="+Inf"} 1
http_request_duration_seconds_sum{code="204",handler="/test2",method="GET",service=""} 3.558e-06
http_request_duration_seconds_count{code="204",handler="/test2",method="GET",service=""} 1
http_requests_inflight{handler="/test1",service=""} 0
http_requests_inflight{handler="/test2",service=""} 0
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="100"} 1
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="1000"} 1
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="10000"} 1
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="100000"} 1
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="1e+06"} 1
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="1e+07"} 1
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="1e+08"} 1
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="1e+09"} 1
http_response_size_bytes_bucket{code="204",handler="/test2",method="GET",service="",le="+Inf"} 1
http_response_size_bytes_sum{code="204",handler="/test2",method="GET",service=""} 0
http_response_size_bytes_count{code="204",handler="/test2",method="GET",service=""} 1

angristan avatar Feb 08 '22 12:02 angristan

Hi @angristan Thanks for the PR!

This proposed feature by #100 is indeed a very nice feature.

Taking your feature and thinking a bit more in the use cases, would make sense to use a list of complied Regexes instead of raw strings?

The only tradeoff that I would see with string comparison VS regex, is that the regexes are less efficient

WDYT?

slok avatar Feb 11 '22 16:02 slok

@slok Sure, why not!

My current need is only a static list of paths, but I think that regexes probably make sense for most of the use cases.

I would be fine with going regex-only, otherwise a tradeoff could be to have two options, or a single option with two fields like so:

IgnoredPaths struct {
	StaticPaths []string
	RegexPaths  []string
}

angristan avatar Feb 11 '22 18:02 angristan

Hello @slok ! Would it be possible to merge this ?

Miaourt avatar Dec 02 '22 21:12 Miaourt

Would be very helpful even only with StaticPaths

kudiyarov avatar Mar 13 '23 12:03 kudiyarov

Hey @slok

is there any plan to merge this?

Thank you!

FedeBev avatar Dec 18 '23 16:12 FedeBev

Lets do this then, however I would suggest using an map instead of a list as the list and the loop would have performance impact on the hot path and using an index would be faster. Also lets add some tests please :)

slok avatar Apr 19 '24 08:04 slok

I would suggest using an map instead of a list as the list and the loop would have performance impact on the hot path and using an index would be faster.

Very good point!

Also lets add some tests please :)

I added a test :)

angristan avatar Apr 19 '24 17:04 angristan