aungkoko

aungkoko

How can I get the response time of a HTTP request in elixir?

I am doing some monitoring for my api endpoints. I’ve already tried out with some http clients like httpoison, and hackney erlang library. But they don’t give me http response time, do they? Did someone experience kind of this? It would be grateful if someone can help me out.

Most Liked

dom

dom

I tend to do something like

{microseconds, result} = :timer.tc(fn -> do_the_request(...) end)
milliseconds = System.convert_time_unit(microseconds, :microseconds, :millisecond)
yurko

yurko

I do this

start_ms = System.monotonic_time(:milliseconds)

# do the request and receive response
    
end_ms = System.monotonic_time(:milliseconds)
diff = end_ms - start_ms
idi527

idi527

I don’t know if that would work for you (I’ve seen it being used in knutin/elli), but if all of your work is done in a single process, you can put timings into the process dictionary:

defp t(key) do
  :erlang.put({:time, key}, :os.timestamp())
end

and call it in different places inside the same process

t(:start_request)
# request something
t(:done_requesting)
t(:start_parsing_body)
# parse body
t(:done_parsing_body)

and then collect the timings at some point later (you would need to be in the same process though).

Enum.flat_map(:erlang.get(), fn
  {{:time, event} = key, time} ->
    :erlang.erase(key)
    [{event, time}]
  
  _ ->
    []
end)

which would return something like

timings = [start_request: {1518, 275996, 334820}, done_requesting: {1518, 276000, 54915}]

and you can calculate diffs with :timer.now_diff/2

start_request = timings[:start_request]
done_requesting = timings[:done_requesting]

:timer.now_diff(done_requesting, start_request)
#=> 3720095 (microseconds)

You might want to read Two frequently used system calls are ~77% slower on AWS EC2 if you are on xen. I think beam uses these calls if it is compiled with --enable-gettimeofday-as-os-system-time and erl_xcomp_clock_gettime_cpu_time set to yes, but I might be wrong.

hristonev

hristonev

I need this time too. I was thinking to measure it by system timer :slight_smile: probably will do the job.

cmkarlsson

cmkarlsson

Agreed. There can be quite a bit of difference here, especially when the HTTP client pool is overloaded. The function execution time can be large, even if the response time is faster.

Hackney though has metrics. (See Metrics on https://github.com/benoitc/hackney). It seems like it is tracking request and response times among other things. Perhaps it is closer to what you want.

It supports two erlang metrics libraries, but you can write your own as long as you implement the correct contract for your module if folsom or exometer is not your cup of tea or hard to integrate into elixir (I have no idea here).

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement