hauleth

hauleth

Server-Timing header in Phoenix/Plug development

Today I have found about Server-Timing header support in browsers. Its purpose is to allow browser developer tools to display some server metrics next to network durations, example:

(Screenshot from Safari, the long blue bar at the bottom)

(Screenshot from Chrome, again, the long blue bar at the bottom)

For now I have created simple plug that exports them:

defmodule Plugs.ServerTimings do
  @behaviour Plug

  import Plug.Conn

  @impl true
  def init(opts) do
    opts
  end

  @impl true
  def call(conn, _) do
    start_time = System.monotonic_time()
    millis = System.convert_time_unit(1, :millisecond, :native)

    register_before_send(conn, fn conn ->
      mono_duration = IO.inspect(System.monotonic_time() - start_time)
      duration = mono_duration / millis

      conn
      |> put_resp_header("server-timing", "plug;dur=#{duration}")
    end)
  end
end

But this is very simple and basic. I tried to use Telemetry together with this, but it worked out rather poorly. Having Telemetry with such would be pretty useful, as it would allow to display other metrics in one timeline:

  • Template rendering time
  • DB queries
  • External services requests
  • Etc.

(Right now the feature set in Server-Timing is rather sparse, as it do not allow for example to state “start” time, which would allow more detailed, tracing-like, information within such view)

What do you think, and what are the possibilities for rendering such informations or even to integrate it into Phoenix/Plug itself.

Most Liked

hauleth

hauleth

I have just published library that provides above plug as a independent library

https://github.com/hauleth/plug_telemetry_server_timing

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18595 126
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New