Intercepting HTTPoison/hackney requests

Hey,

I am currently building an api gateway. That means it receives a request and makes one or more requests to other sources and returns a response.
Now I want to add metrics about the outgoing requests to my Prometheus metrics. Things like hostname, path, method and response code.
That way, I am very flexible and can derive stats for certain actions instead of creating specialized metrics for each individual action.

Is there an easy way to intercept all HTTPoison calls after they are made so I don’t need to add telemetry calls in every function that uses HTTPoison? I also would like to avoid creating a wrapper module for HTTPoison just for the metrics.

I don’t have an answer about catching the function calls but…To me the wrapper module is the way to go. It helps you to decouple your code from HTTPoison dependency itself, leaving you the freedom to change the HTTP library in the future if you want, without refactoring the whole code. It would also much easier to test your code. And obviously it makes easy to integrate telemetry.

3 Likes

I often see no point in wrapping certain things just because I could eventuell change the underlaying library or framework. Practically this never gets done, at least in my experience. I also kinda dislike redoing apis that already exist because one often ends up with a very limited api that needs to be expanded later on when one wants to use a different functionality from the underlaying library.

I wonder if it wouldn’t make more sense to integrate telemetry into HTTPoison itself. Maybe the maintainer is up to it.

EDIT: Already a topic https://github.com/edgurgel/httpoison/issues/383

2 Likes

to me, in general, it’s more about decoupling from third-party libraries rather than add functionalities. This helps me a lot in testing.

2 Likes

Ok, but do you wrap Ecto, Phoenix, or even Telemetry itself? At some point and for certain things it makes not much sense to wrap them.

1 Like

you don’t need it because you have great testing tools in Ecto and phoenix. Those, in someway, are core libraries. If you haven’t read Mocks and explicit contracts is really interesting and talks about it.

But sorry, I didn’t want to move the thread to the decoupling and testing topic sorry :smile:

UPDATE: I inherited some python microservices (in production) which were using directly AWS S3 calls all over the place. When we had to move some part of the storage locally, it was a nightmare to migrate. If we had a wrapper around it we would have saved days…

Nice, this would be great!

This doesn’t exactly answer your question but you might check out Tesla as an alternative to HTTPoison. It supports telemetry amongst other things and it’s a good model for building http clients.

2 Likes