Skip to content

Commit 0805aed

Browse files
authored
feat(fxhttpserver): Updated http server metrics configurations (#119)
1 parent 6b58341 commit 0805aed

File tree

9 files changed

+114
-55
lines changed

9 files changed

+114
-55
lines changed

docs/modules/fxhttpserver.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,9 @@ modules:
322322
namespace: app # http server metrics namespace (default app.name value)
323323
subsystem: httpserver # http server metrics subsystem (default httpserver)
324324
buckets: 0.1, 1, 10 # to override default request duration buckets
325-
normalize: true # to normalize http status code (2xx, 3xx, ...)
325+
normalize:
326+
request_path: true # to normalize http request path, disabled by default
327+
response_status: true # to normalize http response status code (2xx, 3xx, ...), disabled by default
326328
templates:
327329
enabled: true # disabled by default
328330
path: templates/*.html # templates path lookup pattern
@@ -387,7 +389,7 @@ func NewTemplateHandler(cfg *config.Config) *TemplateHandler {
387389
388390
func (h *TemplateHandler) Handle() echo.HandlerFunc {
389391
return func(c echo.Context) error {
390-
// will render (HTML output): "App name is app"
392+
// will render: "<html><body><h1>App name is app</h1></body></html>"
391393
return c.Render(http.StatusOK, "app.html", map[string]interface{}{
392394
"name": h.config.AppName(),
393395
})
@@ -492,7 +494,9 @@ modules:
492494
namespace: app # http server metrics namespace (default app.name value)
493495
subsystem: httpserver # http server metrics subsystem (default httpserver)
494496
buckets: 0.1, 1, 10 # to override default request duration buckets
495-
normalize: true # to normalize http status code (2xx, 3xx, ...)
497+
normalize:
498+
request_path: true # to normalize http request path, disabled by default
499+
response_status: true # to normalize http response status code (2xx, 3xx, ...), disabled by default
496500
```
497501

498502
For example, after calling `[GET] /example`, the [fxcore](https://github.com/ankorstore/yokai/tree/main/fxcore) HTTP server will expose in the configured metrics endpoint:
@@ -501,24 +505,34 @@ For example, after calling `[GET] /example`, the [fxcore](https://github.com/ank
501505
# ...
502506
# HELP app_httpserver_request_duration_seconds Time spent processing HTTP requests
503507
# TYPE app_httpserver_request_duration_seconds histogram
504-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.005"} 1
505-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.01"} 1
506-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.025"} 1
507-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.05"} 1
508-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.1"} 1
509-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.25"} 1
510-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.5"} 1
511-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="1"} 1
512-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="2.5"} 1
513-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="5"} 1
514-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="10"} 1
515-
app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="+Inf"} 1
516-
app_httpserver_request_duration_seconds_sum{handler="/",method="GET"} 0.0014433150000000001
508+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.005"} 1
509+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.01"} 1
510+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.025"} 1
511+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.05"} 1
512+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.1"} 1
513+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.25"} 1
514+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.5"} 1
515+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="1"} 1
516+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="2.5"} 1
517+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="5"} 1
518+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="10"} 1
519+
app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="+Inf"} 1
520+
app_httpserver_request_duration_seconds_sum{path="/",method="GET"} 0.0014433150000000001
517521
# HELP app_httpserver_requests_total Number of processed HTTP requests
518522
# TYPE app_httpserver_requests_total counter
519-
app_httpserver_requests_total{handler="/example",method="GET",status="200"} 1
523+
app_httpserver_requests_total{path="/example",method="GET",status="2xx"} 1
520524
```
521525

526+
Regarding metrics normalization, if you register for example a handler:
527+
528+
- with `fxhttpserver.AsHandler("GET", "/foo/bar/:id", handler.NewExampleHandler)`
529+
- that returns `200` as response code
530+
531+
And receive requests on `/foo/bar/baz?page=1`:
532+
533+
- if `modules.http.server.metrics.normalize.request_path=true`, the metrics `path` label will be `/foo/bar/:id`, otherwise it'll be `/foo/bar/baz?page=1`
534+
- if `modules.http.server.metrics.normalize.response_status=true`, the metrics `status` label will be `2xx`, otherwise it'll be `200`
535+
522536
## Testing
523537

524538
This module provides the possibility to perform functional testing, by calling your application endpoints from your tests.

fxhttpserver/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ modules:
125125
namespace: app # http server metrics namespace (default app.name value)
126126
subsystem: httpserver # http server metrics subsystem (default httpserver)
127127
buckets: 0.1, 1, 10 # to override default request duration buckets
128-
normalize: true # to normalize http status code (2xx, 3xx, ...)
128+
normalize:
129+
request_path: true # to normalize http request path, disabled by default
130+
response_status: true # to normalize http response status code (2xx, 3xx, ...), disabled by default
129131
templates:
130132
enabled: true # disabled by default
131133
path: templates/*.html # templates path lookup pattern
@@ -585,7 +587,7 @@ func (h *SomeHandler) Handle() echo.HandlerFunc {
585587
}
586588
```
587589

588-
You can then test it, considering logs, traces and metrics are enabled:
590+
You can then test it, considering `logs`, `traces` and `metrics` are enabled:
589591

590592
```go
591593
package handler_test
@@ -669,7 +671,7 @@ func TestSomeHandler(t *testing.T) {
669671
expectedMetric := `
670672
# HELP app_httpserver_requests_total Number of processed HTTP requests
671673
# TYPE app_httpserver_requests_total counter
672-
app_httpserver_requests_total{handler="/test",method="GET",status="2xx"} 1
674+
app_httpserver_requests_total{path="/test",method="GET",status="2xx"} 1
673675
`
674676
675677
err := testutil.GatherAndCompare(

fxhttpserver/go.mod

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ require (
1010
github.com/ankorstore/yokai/fxmetrics v1.0.0
1111
github.com/ankorstore/yokai/fxtrace v1.1.0
1212
github.com/ankorstore/yokai/generate v1.0.0
13-
github.com/ankorstore/yokai/httpserver v1.0.0
14-
github.com/ankorstore/yokai/log v1.0.0
13+
github.com/ankorstore/yokai/httpserver v1.1.0
14+
github.com/ankorstore/yokai/log v1.1.0
1515
github.com/ankorstore/yokai/trace v1.0.0
1616
github.com/labstack/echo/v4 v4.11.1
17-
github.com/prometheus/client_golang v1.18.0
17+
github.com/prometheus/client_golang v1.19.0
1818
github.com/stretchr/testify v1.8.4
1919
go.opentelemetry.io/otel v1.16.0
2020
go.opentelemetry.io/otel/trace v1.16.0
@@ -39,12 +39,11 @@ require (
3939
github.com/magiconair/properties v1.8.7 // indirect
4040
github.com/mattn/go-colorable v0.1.13 // indirect
4141
github.com/mattn/go-isatty v0.0.20 // indirect
42-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
4342
github.com/mitchellh/mapstructure v1.5.0 // indirect
4443
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
4544
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
4645
github.com/prometheus/client_model v0.5.0 // indirect
47-
github.com/prometheus/common v0.45.0 // indirect
46+
github.com/prometheus/common v0.48.0 // indirect
4847
github.com/prometheus/procfs v0.12.0 // indirect
4948
github.com/rs/zerolog v1.31.0 // indirect
5049
github.com/sagikazarmark/locafero v0.4.0 // indirect
@@ -68,17 +67,17 @@ require (
6867
go.uber.org/dig v1.17.1 // indirect
6968
go.uber.org/multierr v1.11.0 // indirect
7069
go.uber.org/zap v1.26.0 // indirect
71-
golang.org/x/crypto v0.16.0 // indirect
70+
golang.org/x/crypto v0.18.0 // indirect
7271
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e // indirect
73-
golang.org/x/net v0.19.0 // indirect
72+
golang.org/x/net v0.20.0 // indirect
7473
golang.org/x/sys v0.16.0 // indirect
7574
golang.org/x/text v0.14.0 // indirect
7675
golang.org/x/time v0.5.0 // indirect
7776
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
7877
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
7978
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
8079
google.golang.org/grpc v1.59.0 // indirect
81-
google.golang.org/protobuf v1.31.0 // indirect
80+
google.golang.org/protobuf v1.32.0 // indirect
8281
gopkg.in/ini.v1 v1.67.0 // indirect
8382
gopkg.in/yaml.v3 v3.0.1 // indirect
8483
)

fxhttpserver/go.sum

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ github.com/ankorstore/yokai/fxtrace v1.1.0 h1:UBzz5mo0kvfbp2fEaY/2Mamy4lkWoJiWe8
4848
github.com/ankorstore/yokai/fxtrace v1.1.0/go.mod h1:DP/aNn65I+LU1QoBVvCLhFVr2djFUNFnclITmUxjQmc=
4949
github.com/ankorstore/yokai/generate v1.0.0 h1:kHpbl8cet9qklUamMqSTJy3h6aiybKMgnAK6dDI42p8=
5050
github.com/ankorstore/yokai/generate v1.0.0/go.mod h1:7/gebXdxAOmqeDG54RcguC0a+f3JtqEKVKtSy8f2dlk=
51-
github.com/ankorstore/yokai/httpserver v1.0.0 h1:ROCsM1L/tCSA9zcOpSwrpecQv8twbs3hYtrZ5rFkRF8=
52-
github.com/ankorstore/yokai/httpserver v1.0.0/go.mod h1:W72H3+ok6sUY41Qj5TdhjFqyDlQ9nC4JFwKVQIT6+1A=
53-
github.com/ankorstore/yokai/log v1.0.0 h1:9NsM0J+1O028WuNDW7vr0yeUdWDX1JKYTkuz7hiYCSs=
54-
github.com/ankorstore/yokai/log v1.0.0/go.mod h1:lyBRVA8VkrmlNjaR2jVTH9XjV06ioolWTuDVN6wF0vk=
51+
github.com/ankorstore/yokai/httpserver v1.1.0 h1:0QTwG4cVe1uJYIBDNgsQjs4L/XdB66lGYsDzGswhPR4=
52+
github.com/ankorstore/yokai/httpserver v1.1.0/go.mod h1:DQfiOTqjFWIR4VcTSDNaz1fwuN8i/PAKywnxDv5NsV8=
53+
github.com/ankorstore/yokai/log v1.1.0 h1:7+kkmbGMtpfkEEaWSZ4/4Yp+dieVoolOVG24NpEJDO4=
54+
github.com/ankorstore/yokai/log v1.1.0/go.mod h1:lyBRVA8VkrmlNjaR2jVTH9XjV06ioolWTuDVN6wF0vk=
5555
github.com/ankorstore/yokai/trace v1.0.0 h1:EKWXyg2W8v3xszIiB5JfiDwU2OUfSDOo8LXJMDxlSrw=
5656
github.com/ankorstore/yokai/trace v1.0.0/go.mod h1:OhCIJouVmBD7je1dIynqR1mhMEFCBzidy16a624lwBw=
5757
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
@@ -147,7 +147,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
147147
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
148148
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
149149
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
150-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
150+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
151151
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
152152
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
153153
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
@@ -191,8 +191,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
191191
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
192192
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
193193
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
194-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
195-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
196194
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
197195
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
198196
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
@@ -201,13 +199,13 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
201199
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
202200
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
203201
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
204-
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
205-
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
202+
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
203+
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
206204
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
207205
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
208206
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
209-
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
210-
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
207+
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
208+
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
211209
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
212210
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
213211
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
@@ -290,8 +288,8 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
290288
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
291289
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
292290
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
293-
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
294-
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
291+
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
292+
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
295293
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
296294
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
297295
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -351,8 +349,8 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R
351349
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
352350
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
353351
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
354-
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
355-
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
352+
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
353+
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
356354
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
357355
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
358356
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -548,8 +546,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
548546
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
549547
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
550548
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
551-
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
552-
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
549+
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
550+
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
553551
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
554552
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
555553
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=

fxhttpserver/module.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func withDefaultMiddlewares(httpServer *echo.Echo, p FxHttpServerParam) *echo.Ec
168168

169169
var buckets []float64
170170
if bucketsConfig := p.Config.GetString("modules.http.server.metrics.buckets"); bucketsConfig != "" {
171-
for _, s := range strings.Split(strings.ReplaceAll(bucketsConfig, " ", ""), ",") {
171+
for _, s := range Split(bucketsConfig) {
172172
f, err := strconv.ParseFloat(s, 64)
173173
if err == nil {
174174
buckets = append(buckets, f)
@@ -177,11 +177,12 @@ func withDefaultMiddlewares(httpServer *echo.Echo, p FxHttpServerParam) *echo.Ec
177177
}
178178

179179
metricsMiddlewareConfig := httpservermiddleware.RequestMetricsMiddlewareConfig{
180-
Registry: p.MetricsRegistry,
181-
Namespace: strings.ReplaceAll(namespace, "-", "_"),
182-
Subsystem: strings.ReplaceAll(subsystem, "-", "_"),
183-
Buckets: buckets,
184-
NormalizeHTTPStatus: p.Config.GetBool("modules.http.server.metrics.normalize"),
180+
Registry: p.MetricsRegistry,
181+
Namespace: Sanitize(namespace),
182+
Subsystem: Sanitize(subsystem),
183+
Buckets: buckets,
184+
NormalizeRequestPath: p.Config.GetBool("modules.http.server.metrics.normalize.request_path"),
185+
NormalizeResponseStatus: p.Config.GetBool("modules.http.server.metrics.normalize.response_status"),
185186
}
186187

187188
httpServer.Use(httpservermiddleware.RequestMetricsMiddlewareWithConfig(metricsMiddlewareConfig))
@@ -207,7 +208,7 @@ func withRegisteredResources(httpServer *echo.Echo, p FxHttpServerParam) *echo.E
207208
h.Handler(),
208209
h.Middlewares()...,
209210
)
210-
httpServer.Logger.Debugf("registering handler in group for [%s]%s%s", h.Method(), g.Prefix(), h.Path())
211+
httpServer.Logger.Debugf("registering handler in group for [%s] %s%s", h.Method(), g.Prefix(), h.Path())
211212
}
212213

213214
httpServer.Logger.Debugf("registered handlers group for prefix %s", g.Prefix())
@@ -245,7 +246,7 @@ func withRegisteredResources(httpServer *echo.Echo, p FxHttpServerParam) *echo.E
245246
h.Middlewares()...,
246247
)
247248

248-
httpServer.Logger.Debugf("registered handler for [%s]%s", h.Method(), h.Path())
249+
httpServer.Logger.Debugf("registered handler for [%s] %s", h.Method(), h.Path())
249250
}
250251

251252
return httpServer

fxhttpserver/module_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ func TestModuleWithMetrics(t *testing.T) {
764764
# TYPE foo_bar_requests_total counter
765765
`
766766
expectedMetric := `
767-
foo_bar_requests_total{handler="/bar",method="GET",status="2xx"} 1
767+
foo_bar_requests_total{method="GET",path="/bar",status="2xx"} 1
768768
`
769769

770770
err := testutil.GatherAndCompare(

fxhttpserver/testdata/config/config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ modules:
3232
namespace: foo
3333
subsystem: bar
3434
buckets: 0.1, 1, 10
35-
normalize: true
35+
normalize:
36+
request_path: true
37+
response_status: true
3638
templates:
3739
enabled: ${TEMPLATES_ENABLED}
3840
path: ${TEMPLATES_PATH}

0 commit comments

Comments
 (0)