Skip to content

Commit

Permalink
Update the adapter spec to included telemetry events
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 9, 2020
1 parent f747ace commit da3131d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/_build
/cover
/deps
/docs
/doc
Expand Down
6 changes: 5 additions & 1 deletion lib/plug/conn/adapter.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule Plug.Conn.Adapter do
@moduledoc """
Specification of the connection adapter API implemented by webservers
Specification of the connection adapter API implemented by webservers.
All adapters are required to emit the proper `plug_adapter.request.start`,
`plug_adapter.request.stop` and `plug_adapter.request.failure` telemetry
events on every request.
"""
alias Plug.Conn

Expand Down
9 changes: 5 additions & 4 deletions lib/plug/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ defmodule Plug.Telemetry do
In the example above, two events will be emitted:
* `[:my, :plug, :start]` - emitted when the plug is invoked.
The event carries no measurement. The metadata is the whole
`Plug.Conn` under the `:conn` key and any leftover options
given to the plug under `:options`.
The event carries the `system_time` as measurement. The metadata
is the whole `Plug.Conn` under the `:conn` key and any leftover
options given to the plug under `:options`.
* `[:my, :plug, :stop]` - emitted right before the request is sent.
The event carries a single measurement, `:duration`, which is the
Expand Down Expand Up @@ -72,7 +72,8 @@ defmodule Plug.Telemetry do
@impl true
def call(conn, {start_event, stop_event, opts}) do
start_time = System.monotonic_time()
:telemetry.execute(start_event, %{time: start_time}, %{conn: conn, options: opts})
metadata = %{conn: conn, options: opts}
:telemetry.execute(start_event, %{system_time: System.system_time()}, metadata)

Plug.Conn.register_before_send(conn, fn conn ->
duration = System.monotonic_time() - start_time
Expand Down
4 changes: 2 additions & 2 deletions test/plug/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ defmodule Plug.TelemetryTest do

assert_received {:event, [:pipeline, :start], measurements, metadata}
assert map_size(measurements) == 1
assert %{time: time} = measurements
assert is_integer(time)
assert %{system_time: system_time} = measurements
assert is_integer(system_time) and system_time > 0
assert map_size(metadata) == 2
assert %{conn: %Plug.Conn{}, options: [extra_options: :hello]} = metadata

Expand Down

0 comments on commit da3131d

Please sign in to comment.