feat: add metrics middleware
Since ZIO 2.0 ships with integrated metrics support, I think it makes sense to include middleware for some standard metrics into zio-http.
Codecov Report
Merging #1394 (7c99571) into main (3dd64d8) will increase coverage by
0.34%. The diff coverage is100.00%.
@@ Coverage Diff @@
## main #1394 +/- ##
==========================================
+ Coverage 57.54% 57.88% +0.34%
==========================================
Files 97 98 +1
Lines 3196 3222 +26
Branches 89 91 +2
==========================================
+ Hits 1839 1865 +26
Misses 1357 1357
| Impacted Files | Coverage Δ | |
|---|---|---|
| ...-http/src/main/scala/zio/http/middleware/Web.scala | 72.05% <ø> (ø) |
|
| ...p/src/main/scala/zio/http/middleware/Metrics.scala | 100.00% <100.00%> (ø) |
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.
@frekw This would be a great addition to the ZIO Http ecosystem. I think it would better to maintain it as separate project, in the same repo.
@frekw This would be a great addition to the ZIO Http ecosystem. I think it would better to maintain it as separate project, in the same repo.
Definitely! What do you think is a good project structure in this case? Should we add an extras folder or something similar or should we just add metrics to the top level?
@frekw I would highly recommend to make metric names configurable. Orgs might have naming schemas that a team has to stick to. We have this and I know others as well. And I think making names configurable should not be too complicated.
@frekw If you want this to become #1459, I can help shepherd this through to merging. The functionality is most-welcome!
@jdegoes now that Gauge.increment/decrement is in core I'd love to take another stab at it 👍
@jdegoes I've pushed an updated version that uses the new Gauge.increment/decrement. Temporarily changed the dependency to the snapshot version of ZIO to get access to them but should hold off merging until there's a proper release. It gets a bit repetitive to handle all cases (sucess / error / defect) but nothing too bad.
What I'm struggling a bit with is writing tests that aren't dependent on global state (and execution order) – but I guess that's an issue with testing any logic around metrics, or is there a way to create a scoped, fresh metrics registry solely for testing?
@frekw I think you can do this with tags.
@adamgfraser ah, by accepting something like extraTags: Set[MetricLabel] = Set.empty? Clever!
I'll look at that in the morning, need to jump to bed now! 😴
Ok, I think this is good to go @adamgfraser @jdegoes
Great work here! 👍
@swoogles Since you also did some work, can you work with @frekw to get this one through?
@swoogles Since you also did some work, can you work with @frekw to get this one through?
Yep, I've already submitted a PR against his branch with a few small additions. Otherwise I think he nailed it.
Sorry @swoogles I didn't have notifications turned on for my fork so I had missed it. Merged now ✅
Ok, I've rebased based on the latest changes to main and updated the test accordingly. CI should pass now. 👍
@jdegoes I noticed master is now using a ZIO snapshot so I've rebased. This should be good to merge now.
Whoops, I spoke too early, looks like the Middleware API has changed. I'll adjust.
Thank you!