Skip to content

Commit

Permalink
Deprecate instrument API
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed May 29, 2019
1 parent 3bfb9f6 commit 19b0b01
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 401 deletions.
29 changes: 8 additions & 21 deletions lib/phoenix/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,14 @@ defmodule Phoenix.Controller do
raise "cannot render template #{inspect template} because conn.params[\"_format\"] is not set. " <>
"Please set `plug :accepts, ~w(html json ...)` in your pipeline."

instrument_render_and_send(conn, format, template, assigns)
render_and_send(conn, format, template, assigns)
end

def render(conn, template, assigns)
when is_binary(template) and (is_map(assigns) or is_list(assigns)) do
case Path.extname(template) do
"." <> format ->
instrument_render_and_send(conn, format, template, assigns)
render_and_send(conn, format, template, assigns)
"" ->
raise "cannot render template #{inspect template} without format. Use an atom if the " <>
"template format is meant to be set dynamically based on the request format"
Expand Down Expand Up @@ -726,32 +726,19 @@ defmodule Phoenix.Controller do
|> render(template, assigns)
end

@doc false
def __put_render__(conn, view, template, format, assigns) do
content_type = MIME.type(format)
conn = prepare_assigns(conn, assigns, template, format)
data = Phoenix.View.render_to_iodata(view, template, Map.put(conn.assigns, :conn, conn))

conn
|> ensure_resp_content_type(content_type)
|> resp(conn.status || 200, data)
end

defp instrument_render_and_send(conn, format, template, assigns) do
defp render_and_send(conn, format, template, assigns) do
template = template_name(template, format)

view =
Map.get(conn.private, :phoenix_view) ||
raise "a view module was not specified, set one with put_view/2"

metadata = %{view: view, template: template, format: format, conn: conn}

conn =
Phoenix.Endpoint.instrument(conn, :phoenix_controller_render, metadata, fn ->
__put_render__(conn, view, template, format, assigns)
end)
conn = prepare_assigns(conn, assigns, template, format)
data = Phoenix.View.render_to_iodata(view, template, Map.put(conn.assigns, :conn, conn))

send_resp(conn)
conn
|> ensure_resp_content_type(MIME.type(format))
|> send_resp(conn.status || 200, data)
end

defp prepare_assigns(conn, assigns, template, format) do
Expand Down
6 changes: 2 additions & 4 deletions lib/phoenix/controller/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ defmodule Phoenix.Controller.Pipeline do
&(&1 |> Map.put(:phoenix_controller, __MODULE__)
|> Map.put(:phoenix_action, action))

Phoenix.Endpoint.instrument conn, :phoenix_controller_call,
%{conn: conn, log_level: @phoenix_log_level}, fn ->
phoenix_controller_pipeline(conn, action)
end
phoenix_controller_pipeline(conn, action)
end

@doc false
Expand Down Expand Up @@ -86,6 +83,7 @@ defmodule Phoenix.Controller.Pipeline do

quote do
defoverridable [action: 2]

def action(var!(conn_before), opts) do
try do
var!(conn_after) = super(var!(conn_before), opts)
Expand Down
35 changes: 5 additions & 30 deletions lib/phoenix/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Phoenix.Endpoint do
Endpoint configuration is split into two categories. Compile-time
configuration means the configuration is read during compilation
and changing it at runtime has no effect. The compile-time
configuration is mostly related to error handling and instrumentation.
configuration is mostly related to error handling.
Runtime configuration, instead, is accessed during or
after your application is started and can be read through the
Expand Down Expand Up @@ -92,10 +92,6 @@ defmodule Phoenix.Endpoint do
The default format is used when none is set in the connection
* `:instrumenters` - a list of instrumenter modules whose callbacks will
be fired on instrumentation events. Read more on instrumentation in the
"Instrumentation" section below
### Runtime configuration
* `:cache_static_manifest` - a path to a json manifest file that contains
Expand Down Expand Up @@ -232,7 +228,6 @@ defmodule Phoenix.Endpoint do
* for broadcasting to channels: `c:broadcast/3`, `c:broadcast!/3`,
`c:broadcast_from/4`, and `c:broadcast_from!/4`
* for configuration: `c:start_link/0`, `c:config/2`, and `c:config_change/2`
* for instrumentation: `c:instrument/3`
* as required by the `Plug` behaviour: `c:Plug.init/1` and `c:Plug.call/2`
## Instrumentation
Expand Down Expand Up @@ -387,17 +382,6 @@ defmodule Phoenix.Endpoint do
"""
@callback broadcast_from!(from :: pid, topic, event, msg) :: :ok | no_return

# Instrumentation

@doc """
Allows instrumenting operation defined by `function`.
`runtime_metadata` may be omitted and defaults to `nil`.
Read more about instrumentation in the "Instrumentation" section.
"""
@macrocallback instrument(instrument_event :: Macro.t, runtime_metadata :: Macro.t, function :: Macro.t) :: Macro.t

@doc false
defmacro __using__(opts) do
quote do
Expand Down Expand Up @@ -652,8 +636,6 @@ defmodule Phoenix.Endpoint do
@doc false
defmacro __before_compile__(%{module: module}) do
sockets = Module.get_attribute(module, :phoenix_sockets)
otp_app = Module.get_attribute(module, :otp_app)
instrumentation = Phoenix.Endpoint.Instrument.definstrument(otp_app, module)

dispatches =
for {path, socket, socket_opts} <- sockets,
Expand Down Expand Up @@ -692,9 +674,6 @@ defmodule Phoenix.Endpoint do

@doc false
def __handler__(%{path_info: path} = conn, opts), do: do_handler(path, conn, opts)

unquote(instrumentation)

unquote(dispatches)
defp do_handler(_path, conn, opts), do: {:plug, conn, __MODULE__, opts}
end
Expand Down Expand Up @@ -894,15 +873,11 @@ defmodule Phoenix.Endpoint do
end

@doc false
defmacro instrument(endpoint_or_conn_or_socket, event, runtime \\ Macro.escape(%{}), fun) do
compile = Phoenix.Endpoint.Instrument.strip_caller(__CALLER__) |> Macro.escape()
defmacro instrument(_endpoint_or_conn_or_socket, _event, _runtime, _fun) do
IO.warn "Phoenix.Endpoint.instrument/4 is deprecated and has no effect. Use :telemetry instead",
Macro.Env.stacktrace(__CALLER__)

quote do
case Phoenix.Endpoint.Instrument.extract_endpoint(unquote(endpoint_or_conn_or_socket)) do
nil -> unquote(fun).()
endpoint -> endpoint.instrument(unquote(event), unquote(compile), unquote(runtime), unquote(fun))
end
end
:ok
end

@doc """
Expand Down
198 changes: 0 additions & 198 deletions lib/phoenix/endpoint/instrument.ex

This file was deleted.

8 changes: 4 additions & 4 deletions lib/phoenix/endpoint/render_errors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ defmodule Phoenix.Endpoint.RenderErrors do
metadata = %{status: status, kind: kind, reason: reason, stacktrace: stack, log: level}

try do
conn
|> render(status, kind, reason, stack, opts)
|> send_resp()
render(conn, status, kind, reason, stack, opts)
after
duration = System.monotonic_time() - start
:telemetry.execute([:phoenix, :error_rendered], %{duration: duration}, metadata)
Expand Down Expand Up @@ -111,7 +109,9 @@ defmodule Phoenix.Endpoint.RenderErrors do
template = "#{conn.status}.#{format}"
assigns = %{kind: kind, reason: reason, stack: stack}

Controller.__put_render__(conn, view, template, format, assigns)
conn
|> Controller.put_view(view)
|> Controller.render(template, assigns)
end

defp maybe_fetch_query_params(conn) do
Expand Down
Loading

0 comments on commit 19b0b01

Please sign in to comment.