appcues / mojito

An easy-to-use Elixir HTTP client, built on the low-level Mint library.
https://hexdocs.pm/mojito/Mojito.html
MIT License
349 stars 34 forks source link

Timing data in Mojito #37

Closed christhekeele closed 3 years ago

christhekeele commented 5 years ago

Playing around with an HTTP forward-proxy powered by Mojito. One question I keep asking myself: how much overhead am I adding?

I would like to return the time spent outside the proxy to the caller, but I can't do this without knowing how long Mojito itself was blocked, since I want to incorporate time spent in Mojito into the time spent in the proxy, and separate it (as much as possible) from the external time spent waiting for the request to be processed.

The only way I can see this working is having Mojito aggregate the time spent waiting for connection responses from Mint, and return the sum once the request is completed to the caller of Mojito.request.

Any thoughts about how this could work to guide a PR?

christhekeele commented 5 years ago

The main sensitive issue I forsee is sampling of timing data impacting performance of Mojito itself.

I don't think several System.monotonic_time measurements and a few System.convert_time_unit/Integer.to_string calls would make a huge difference. Plug itself makes similar calls in its Logger.

But both Logger and Plug purge such unnecessary calls in production environments via macros, so perhaps a similar Mojito mechanism should do the same?

gamache commented 5 years ago

Hi @christhekeele! Thanks for the feature request. I think the best way to implement this would be to accept a timing: true config option in Mojito.request, and add timing information as a new field in the returned %Mojito.Response{}. My gut feeling is that implementing this in terms of macros won't be necessary; it would certainly be less flexible if we did.

gamache commented 4 years ago

Coming back on this: the Telemetry library is gaining acceptance as a way to publish this sort of timing information. I will be adding this to Mojito.

collegeimprovements commented 4 years ago

Is there any way to config proxy in config.exs for Mojito ? Or is there any hook like init for Ecto to define proxy for Mojito?

christhekeele commented 4 years ago

Hey @collegeimprovements! Just to clarify here, Mojito is not responsible for any sort of proxy functionality.

I was building my own proxy on top of Mojito when I started this discussion, and brought that up as a use-case.

christhekeele commented 4 years ago

@gamache I spent a little time tinkering with a PR for this tonight and realized that most of the telemetry I personally am interested in may more sense in Mint rather than Mojito itself. Specifically, events like:

I think Mint could emit most of those itself. I'll probably poke through it more to think about where these things belong.


Stepping back from my own needs, though, I can also see the value in pooling telemetry, since that is a major value-add of Mojito. Maybe events like:


I'm not sure if these are the sorts of things you have in mind, too. If you're accepting contributions, I'd enjoy discussing what sort of events you think belong in Mojito and submitting a :telemetry-enabling PR along those requirements. Otherwise, I appreciate you entertaining this feature request. Either way, thanks for the work on Mojito!

andyleclair commented 3 years ago

@christhekeele how does https://github.com/appcues/mojito/pull/81 look to you?

christhekeele commented 3 years ago

@andyleclair that looks brilliant, exactly what I needed!

andyleclair commented 3 years ago

Closing this as it is resolved in 0.7.7