zio-http icon indicating copy to clipboard operation
zio-http copied to clipboard

feat: add metrics middleware

Open frekw opened this issue 3 years ago • 15 comments

Since ZIO 2.0 ships with integrated metrics support, I think it makes sense to include middleware for some standard metrics into zio-http.

frekw avatar Aug 18 '22 16:08 frekw

Codecov Report

Merging #1394 (7c99571) into main (3dd64d8) will increase coverage by 0.34%. The diff coverage is 100.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.

codecov-commenter avatar Aug 26 '22 11:08 codecov-commenter

@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.

tusharmath avatar Aug 29 '22 01:08 tusharmath

@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 avatar Aug 30 '22 09:08 frekw

@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.

987Nabil avatar Sep 13 '22 08:09 987Nabil

@frekw If you want this to become #1459, I can help shepherd this through to merging. The functionality is most-welcome!

jdegoes avatar Sep 15 '22 15:09 jdegoes

@jdegoes now that Gauge.increment/decrement is in core I'd love to take another stab at it 👍

frekw avatar Sep 15 '22 15:09 frekw

@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 avatar Sep 16 '22 00:09 frekw

@frekw I think you can do this with tags.

adamgfraser avatar Sep 16 '22 00:09 adamgfraser

@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! 😴

frekw avatar Sep 16 '22 00:09 frekw

Ok, I think this is good to go @adamgfraser @jdegoes

frekw avatar Sep 16 '22 09:09 frekw

Great work here! 👍

swoogles avatar Sep 16 '22 15:09 swoogles

@swoogles Since you also did some work, can you work with @frekw to get this one through?

jdegoes avatar Sep 18 '22 14:09 jdegoes

@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.

swoogles avatar Sep 18 '22 14:09 swoogles

Sorry @swoogles I didn't have notifications turned on for my fork so I had missed it. Merged now ✅

frekw avatar Sep 18 '22 14:09 frekw

Ok, I've rebased based on the latest changes to main and updated the test accordingly. CI should pass now. 👍

frekw avatar Sep 18 '22 20:09 frekw

@jdegoes I noticed master is now using a ZIO snapshot so I've rebased. This should be good to merge now.

frekw avatar Oct 18 '22 20:10 frekw

Whoops, I spoke too early, looks like the Middleware API has changed. I'll adjust.

frekw avatar Oct 18 '22 20:10 frekw

Thank you!

jdegoes avatar Oct 24 '22 13:10 jdegoes