diff --git a/docs/modules/fxhttpserver.md b/docs/modules/fxhttpserver.md index ea0b5cd..2a6bd40 100644 --- a/docs/modules/fxhttpserver.md +++ b/docs/modules/fxhttpserver.md @@ -322,7 +322,9 @@ modules: namespace: app # http server metrics namespace (default app.name value) subsystem: httpserver # http server metrics subsystem (default httpserver) buckets: 0.1, 1, 10 # to override default request duration buckets - normalize: true # to normalize http status code (2xx, 3xx, ...) + normalize: + request_path: true # to normalize http request path, disabled by default + response_status: true # to normalize http response status code (2xx, 3xx, ...), disabled by default templates: enabled: true # disabled by default path: templates/*.html # templates path lookup pattern @@ -387,7 +389,7 @@ func NewTemplateHandler(cfg *config.Config) *TemplateHandler { func (h *TemplateHandler) Handle() echo.HandlerFunc { return func(c echo.Context) error { - // will render (HTML output): "App name is app" + // will render: "

App name is app

" return c.Render(http.StatusOK, "app.html", map[string]interface{}{ "name": h.config.AppName(), }) @@ -492,7 +494,9 @@ modules: namespace: app # http server metrics namespace (default app.name value) subsystem: httpserver # http server metrics subsystem (default httpserver) buckets: 0.1, 1, 10 # to override default request duration buckets - normalize: true # to normalize http status code (2xx, 3xx, ...) + normalize: + request_path: true # to normalize http request path, disabled by default + response_status: true # to normalize http response status code (2xx, 3xx, ...), disabled by default ``` 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 # ... # HELP app_httpserver_request_duration_seconds Time spent processing HTTP requests # TYPE app_httpserver_request_duration_seconds histogram -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.005"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.01"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.025"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.05"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.1"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.25"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="0.5"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="1"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="2.5"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="5"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="10"} 1 -app_httpserver_request_duration_seconds_bucket{handler="/example",method="GET",le="+Inf"} 1 -app_httpserver_request_duration_seconds_sum{handler="/",method="GET"} 0.0014433150000000001 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.005"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.01"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.025"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.05"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.1"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.25"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="0.5"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="1"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="2.5"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="5"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="10"} 1 +app_httpserver_request_duration_seconds_bucket{path="/example",method="GET",le="+Inf"} 1 +app_httpserver_request_duration_seconds_sum{path="/",method="GET"} 0.0014433150000000001 # HELP app_httpserver_requests_total Number of processed HTTP requests # TYPE app_httpserver_requests_total counter -app_httpserver_requests_total{handler="/example",method="GET",status="200"} 1 +app_httpserver_requests_total{path="/example",method="GET",status="2xx"} 1 ``` +Regarding metrics normalization, if you register for example a handler: + +- with `fxhttpserver.AsHandler("GET", "/foo/bar/:id", handler.NewExampleHandler)` +- that returns `200` as response code + +And receive requests on `/foo/bar/baz?page=1`: + +- 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` +- if `modules.http.server.metrics.normalize.response_status=true`, the metrics `status` label will be `2xx`, otherwise it'll be `200` + ## Testing This module provides the possibility to perform functional testing, by calling your application endpoints from your tests. diff --git a/fxhttpserver/README.md b/fxhttpserver/README.md index ca0fc00..42909b6 100644 --- a/fxhttpserver/README.md +++ b/fxhttpserver/README.md @@ -125,7 +125,9 @@ modules: namespace: app # http server metrics namespace (default app.name value) subsystem: httpserver # http server metrics subsystem (default httpserver) buckets: 0.1, 1, 10 # to override default request duration buckets - normalize: true # to normalize http status code (2xx, 3xx, ...) + normalize: + request_path: true # to normalize http request path, disabled by default + response_status: true # to normalize http response status code (2xx, 3xx, ...), disabled by default templates: enabled: true # disabled by default path: templates/*.html # templates path lookup pattern @@ -585,7 +587,7 @@ func (h *SomeHandler) Handle() echo.HandlerFunc { } ``` -You can then test it, considering logs, traces and metrics are enabled: +You can then test it, considering `logs`, `traces` and `metrics` are enabled: ```go package handler_test @@ -669,7 +671,7 @@ func TestSomeHandler(t *testing.T) { expectedMetric := ` # HELP app_httpserver_requests_total Number of processed HTTP requests # TYPE app_httpserver_requests_total counter - app_httpserver_requests_total{handler="/test",method="GET",status="2xx"} 1 + app_httpserver_requests_total{path="/test",method="GET",status="2xx"} 1 ` err := testutil.GatherAndCompare( diff --git a/fxhttpserver/go.mod b/fxhttpserver/go.mod index dacf246..b15cd48 100644 --- a/fxhttpserver/go.mod +++ b/fxhttpserver/go.mod @@ -10,11 +10,11 @@ require ( github.com/ankorstore/yokai/fxmetrics v1.0.0 github.com/ankorstore/yokai/fxtrace v1.1.0 github.com/ankorstore/yokai/generate v1.0.0 - github.com/ankorstore/yokai/httpserver v1.0.0 - github.com/ankorstore/yokai/log v1.0.0 + github.com/ankorstore/yokai/httpserver v1.1.0 + github.com/ankorstore/yokai/log v1.1.0 github.com/ankorstore/yokai/trace v1.0.0 github.com/labstack/echo/v4 v4.11.1 - github.com/prometheus/client_golang v1.18.0 + github.com/prometheus/client_golang v1.19.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/otel v1.16.0 go.opentelemetry.io/otel/trace v1.16.0 @@ -39,12 +39,11 @@ require ( github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_model v0.5.0 // indirect - github.com/prometheus/common v0.45.0 // indirect + github.com/prometheus/common v0.48.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/rs/zerolog v1.31.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect @@ -68,9 +67,9 @@ require ( go.uber.org/dig v1.17.1 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.26.0 // indirect - golang.org/x/crypto v0.16.0 // indirect + golang.org/x/crypto v0.18.0 // indirect golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e // indirect - golang.org/x/net v0.19.0 // indirect + golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.5.0 // indirect @@ -78,7 +77,7 @@ require ( google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect google.golang.org/grpc v1.59.0 // indirect - google.golang.org/protobuf v1.31.0 // indirect + google.golang.org/protobuf v1.32.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/fxhttpserver/go.sum b/fxhttpserver/go.sum index c2be765..13eb15f 100644 --- a/fxhttpserver/go.sum +++ b/fxhttpserver/go.sum @@ -48,10 +48,10 @@ github.com/ankorstore/yokai/fxtrace v1.1.0 h1:UBzz5mo0kvfbp2fEaY/2Mamy4lkWoJiWe8 github.com/ankorstore/yokai/fxtrace v1.1.0/go.mod h1:DP/aNn65I+LU1QoBVvCLhFVr2djFUNFnclITmUxjQmc= github.com/ankorstore/yokai/generate v1.0.0 h1:kHpbl8cet9qklUamMqSTJy3h6aiybKMgnAK6dDI42p8= github.com/ankorstore/yokai/generate v1.0.0/go.mod h1:7/gebXdxAOmqeDG54RcguC0a+f3JtqEKVKtSy8f2dlk= -github.com/ankorstore/yokai/httpserver v1.0.0 h1:ROCsM1L/tCSA9zcOpSwrpecQv8twbs3hYtrZ5rFkRF8= -github.com/ankorstore/yokai/httpserver v1.0.0/go.mod h1:W72H3+ok6sUY41Qj5TdhjFqyDlQ9nC4JFwKVQIT6+1A= -github.com/ankorstore/yokai/log v1.0.0 h1:9NsM0J+1O028WuNDW7vr0yeUdWDX1JKYTkuz7hiYCSs= -github.com/ankorstore/yokai/log v1.0.0/go.mod h1:lyBRVA8VkrmlNjaR2jVTH9XjV06ioolWTuDVN6wF0vk= +github.com/ankorstore/yokai/httpserver v1.1.0 h1:0QTwG4cVe1uJYIBDNgsQjs4L/XdB66lGYsDzGswhPR4= +github.com/ankorstore/yokai/httpserver v1.1.0/go.mod h1:DQfiOTqjFWIR4VcTSDNaz1fwuN8i/PAKywnxDv5NsV8= +github.com/ankorstore/yokai/log v1.1.0 h1:7+kkmbGMtpfkEEaWSZ4/4Yp+dieVoolOVG24NpEJDO4= +github.com/ankorstore/yokai/log v1.1.0/go.mod h1:lyBRVA8VkrmlNjaR2jVTH9XjV06ioolWTuDVN6wF0vk= github.com/ankorstore/yokai/trace v1.0.0 h1:EKWXyg2W8v3xszIiB5JfiDwU2OUfSDOo8LXJMDxlSrw= github.com/ankorstore/yokai/trace v1.0.0/go.mod h1:OhCIJouVmBD7je1dIynqR1mhMEFCBzidy16a624lwBw= 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/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= 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/ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= 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 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= -github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= +github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU= +github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= -github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= -github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= +github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= +github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= 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 golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 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 golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= 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 google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/fxhttpserver/module.go b/fxhttpserver/module.go index 8701f4a..c2adff7 100644 --- a/fxhttpserver/module.go +++ b/fxhttpserver/module.go @@ -168,7 +168,7 @@ func withDefaultMiddlewares(httpServer *echo.Echo, p FxHttpServerParam) *echo.Ec var buckets []float64 if bucketsConfig := p.Config.GetString("modules.http.server.metrics.buckets"); bucketsConfig != "" { - for _, s := range strings.Split(strings.ReplaceAll(bucketsConfig, " ", ""), ",") { + for _, s := range Split(bucketsConfig) { f, err := strconv.ParseFloat(s, 64) if err == nil { buckets = append(buckets, f) @@ -177,11 +177,12 @@ func withDefaultMiddlewares(httpServer *echo.Echo, p FxHttpServerParam) *echo.Ec } metricsMiddlewareConfig := httpservermiddleware.RequestMetricsMiddlewareConfig{ - Registry: p.MetricsRegistry, - Namespace: strings.ReplaceAll(namespace, "-", "_"), - Subsystem: strings.ReplaceAll(subsystem, "-", "_"), - Buckets: buckets, - NormalizeHTTPStatus: p.Config.GetBool("modules.http.server.metrics.normalize"), + Registry: p.MetricsRegistry, + Namespace: Sanitize(namespace), + Subsystem: Sanitize(subsystem), + Buckets: buckets, + NormalizeRequestPath: p.Config.GetBool("modules.http.server.metrics.normalize.request_path"), + NormalizeResponseStatus: p.Config.GetBool("modules.http.server.metrics.normalize.response_status"), } httpServer.Use(httpservermiddleware.RequestMetricsMiddlewareWithConfig(metricsMiddlewareConfig)) @@ -207,7 +208,7 @@ func withRegisteredResources(httpServer *echo.Echo, p FxHttpServerParam) *echo.E h.Handler(), h.Middlewares()..., ) - httpServer.Logger.Debugf("registering handler in group for [%s]%s%s", h.Method(), g.Prefix(), h.Path()) + httpServer.Logger.Debugf("registering handler in group for [%s] %s%s", h.Method(), g.Prefix(), h.Path()) } httpServer.Logger.Debugf("registered handlers group for prefix %s", g.Prefix()) @@ -245,7 +246,7 @@ func withRegisteredResources(httpServer *echo.Echo, p FxHttpServerParam) *echo.E h.Middlewares()..., ) - httpServer.Logger.Debugf("registered handler for [%s]%s", h.Method(), h.Path()) + httpServer.Logger.Debugf("registered handler for [%s] %s", h.Method(), h.Path()) } return httpServer diff --git a/fxhttpserver/module_test.go b/fxhttpserver/module_test.go index 0de2128..361615b 100644 --- a/fxhttpserver/module_test.go +++ b/fxhttpserver/module_test.go @@ -764,7 +764,7 @@ func TestModuleWithMetrics(t *testing.T) { # TYPE foo_bar_requests_total counter ` expectedMetric := ` - foo_bar_requests_total{handler="/bar",method="GET",status="2xx"} 1 + foo_bar_requests_total{method="GET",path="/bar",status="2xx"} 1 ` err := testutil.GatherAndCompare( diff --git a/fxhttpserver/testdata/config/config.yaml b/fxhttpserver/testdata/config/config.yaml index 5812d8c..84e361f 100644 --- a/fxhttpserver/testdata/config/config.yaml +++ b/fxhttpserver/testdata/config/config.yaml @@ -32,7 +32,9 @@ modules: namespace: foo subsystem: bar buckets: 0.1, 1, 10 - normalize: true + normalize: + request_path: true + response_status: true templates: enabled: ${TEMPLATES_ENABLED} path: ${TEMPLATES_PATH} diff --git a/fxhttpserver/util.go b/fxhttpserver/util.go new file mode 100644 index 0000000..ec29119 --- /dev/null +++ b/fxhttpserver/util.go @@ -0,0 +1,16 @@ +package fxhttpserver + +import "strings" + +// Sanitize transforms a given string to not contain spaces or dashes, and to be in lower case. +func Sanitize(str string) string { + san := strings.ReplaceAll(str, " ", "_") + san = strings.ReplaceAll(san, "-", "_") + + return strings.ToLower(san) +} + +// Split trims and splits a provided string by comma. +func Split(str string) []string { + return strings.Split(strings.ReplaceAll(str, " ", ""), ",") +} diff --git a/fxhttpserver/util_test.go b/fxhttpserver/util_test.go new file mode 100644 index 0000000..e135345 --- /dev/null +++ b/fxhttpserver/util_test.go @@ -0,0 +1,27 @@ +package fxhttpserver_test + +import ( + "testing" + + "github.com/ankorstore/yokai/fxhttpserver" + "github.com/stretchr/testify/assert" +) + +func TestSanitize(t *testing.T) { + t.Parallel() + + assert.Equal(t, "foo_bar", fxhttpserver.Sanitize("foo-bar")) + assert.Equal(t, "foo_bar", fxhttpserver.Sanitize("foo bar")) + assert.Equal(t, "foo_bar", fxhttpserver.Sanitize("Foo-Bar")) + assert.Equal(t, "foo_bar", fxhttpserver.Sanitize("Foo Bar")) +} + +func TestSplit(t *testing.T) { + t.Parallel() + + assert.Equal(t, []string{"1", "2", "3"}, fxhttpserver.Split("1,2,3")) + assert.Equal(t, []string{"1", "2", "3"}, fxhttpserver.Split(" 1,2,3")) + assert.Equal(t, []string{"1", "2", "3"}, fxhttpserver.Split("1,2,3 ")) + assert.Equal(t, []string{"1", "2", "3"}, fxhttpserver.Split("1, 2, 3")) + assert.Equal(t, []string{"1", "2", "3"}, fxhttpserver.Split(" 1, 2, 3 ")) +}