zio / zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
https://zio.dev/zio-http
Apache License 2.0
787 stars 396 forks source link

Metric middleware labels based on response #2536

Open jgranstrom opened 10 months ago

jgranstrom commented 10 months ago

Currently we only allow passing labels when providing the metric middleware (https://github.com/zio/zio-http/blob/b43dbc478e1f387c6de60bdab0b99dfbc1de3e6f/zio-http/src/main/scala/zio/http/Middleware.scala#L260).

What do you think about making this effectful and allow providing these based on request+response? It can be useful when you want to segment metrics on, for example, cache hit or similar only known at response time.

jdegoes commented 9 months ago

Perhaps a new version can be introduced, to avoid negatively impacting the performance of the existing version?

987Nabil commented 9 months ago

I think you don't need to make this effectful, except you want to access the body of the request/response or need state to carry over from the request to the response (response time for example). Accessing things like headers, status code and query params can be done in pure functions. That is the same for the logging middlewares we have.

jgranstrom commented 9 months ago

Yeah, we could initially provide two pure callbacks for labels from request, and labels from response, (we probably would have one that provides both in practice), which would be a big upgrade already. But as you said, with restrictions. I think it would be worth adding the restricted form at least, and that should not be very impactful to performance.