|
| 1 | +defmodule Timber.PhoenixInstrumenter do |
| 2 | + @moduledoc """ |
| 3 | + Handles instrumentation of `Phoenix.Endpoint` |
| 4 | +
|
| 5 | + This module is designed to log events when Phoenix calls a controller or |
| 6 | + renders a template. It hooks into the instrumentation tools built into |
| 7 | + Phoenix. Because of this, you will have to trigger a Phoenix recompile |
| 8 | + in order for the instrumentation to take effect. |
| 9 | +
|
| 10 | + ## Adding Instrumentation |
| 11 | +
|
| 12 | + Phoenix instrumenetation is controlled through the configuration for your |
| 13 | + Phoenix endpoint module, typically named along the lines of `MyApp.Endpoint`. |
| 14 | + This module will be configured in `config/config.exs` similar to the following: |
| 15 | +
|
| 16 | + ``` |
| 17 | + config :my_app, MyApp.Endpoint, |
| 18 | + http: [port: 4001], |
| 19 | + root: Path.dirname(__DIR__), |
| 20 | + pubsub: [name: MyApp.PubSub, |
| 21 | + adapter: Phoenix.PubSub.PG2] |
| 22 | + ``` |
| 23 | +
|
| 24 | + You will need to add an `:instrumenters` key to this configuration with |
| 25 | + a value of `[Timber.PhoenixInstrumenter]`. This would update the configuration |
| 26 | + to something like the following: |
| 27 | +
|
| 28 | +
|
| 29 | + ``` |
| 30 | + config :my_app, MyApp.Endpoint, |
| 31 | + http: [port: 4001], |
| 32 | + root: Path.dirname(__DIR__), |
| 33 | + instrumenters: [Timber.PhoenixInstrumenter], |
| 34 | + pubsub: [name: MyApp.PubSub, |
| 35 | + adapter: Phoenix.PubSub.PG2] |
| 36 | + ``` |
| 37 | +
|
| 38 | + In order for this to take affect locally, you will need to recompile Phoenix using |
| 39 | + the command `mix deps.compile phoenix`. By default, Timber will log calls to controllers |
| 40 | + and template renders at the `:info` level. You can change this by adding an additional |
| 41 | + configuration line: |
| 42 | +
|
| 43 | + ``` |
| 44 | + config :timber, :instrumentation_level, :debug |
| 45 | + ``` |
| 46 | +
|
| 47 | + If you're currently displaying logs at the `:debug` level, you will also see that |
| 48 | + Phoenix has built-in logging already at this level. The Phoenix logger will not emit |
| 49 | + Timber events, so you can turn it off to stop the duplicate output. The Phoenix logger |
| 50 | + is controlled through the `MyApp.Web` module. Look for a definition block like the |
| 51 | + following: |
| 52 | +
|
| 53 | + ``` |
| 54 | + def controller do |
| 55 | + quote do |
| 56 | + use Phoenix.Controller |
| 57 | + end |
| 58 | + end |
| 59 | + ``` |
| 60 | +
|
| 61 | + You will want to modify this to the following |
| 62 | +
|
| 63 | + ``` |
| 64 | + def controller do |
| 65 | + quote do |
| 66 | + use Phoenix.Controller, log: false |
| 67 | + end |
| 68 | + end |
| 69 | + ``` |
| 70 | + """ |
| 71 | + |
| 72 | + require Logger |
| 73 | + |
| 74 | + alias Timber.Events.ControllerCall |
| 75 | + alias Timber.Events.TemplateRenderEvent |
| 76 | + |
| 77 | + @doc false |
| 78 | + @spec phoenix_controller_call(:start | :stop, map | non_neg_integer, map | :ok) :: :ok |
| 79 | + def phoenix_controller_call(:start, %{module: module}, %{conn: conn}) do |
| 80 | + log_level = get_log_level(:info) |
| 81 | + |
| 82 | + controller = inspect(module) |
| 83 | + action_name = |
| 84 | + conn |
| 85 | + |> Phoenix.Controller.action_name() |
| 86 | + |> Atom.to_string |
| 87 | + |
| 88 | + # Phoenix actions are always 2 arity function |
| 89 | + action = action_name <> "/2" |
| 90 | + |
| 91 | + event = ControllerCall.new( |
| 92 | + action: action, |
| 93 | + controller: controller |
| 94 | + ) |
| 95 | + |
| 96 | + Logger.log(log_level, event.description, timber_event: event) |
| 97 | + |
| 98 | + :ok |
| 99 | + end |
| 100 | + |
| 101 | + def phoenix_controller_call(:stop, _time_diff, :ok) do |
| 102 | + :ok |
| 103 | + end |
| 104 | + |
| 105 | + @doc false |
| 106 | + @spec phoenix_controller_render(:start | :stop, map | non_neg_integer, map | :ok) :: :ok |
| 107 | + def phoenix_controller_render(:start, _compile_metadata, %{template: template_name}) do |
| 108 | + {:ok, template_name} |
| 109 | + end |
| 110 | + |
| 111 | + def phoenix_controller_render(:stop, time_diff, {:ok, template_name}) do |
| 112 | + log_level = get_log_level(:info) |
| 113 | + |
| 114 | + # This comes in as native time but is expected to be a float representing |
| 115 | + # milliseconds |
| 116 | + time_ms = |
| 117 | + time_diff |
| 118 | + |> System.convert_time_unit(:native, :milliseconds) |
| 119 | + |> :erlang.float() |
| 120 | + |
| 121 | + event = TemplateRenderEvent.new( |
| 122 | + name: template_name, |
| 123 | + time_ms: time_ms |
| 124 | + ) |
| 125 | + |
| 126 | + Logger.log(log_level, event.description, timber_event: event) |
| 127 | + |
| 128 | + :ok |
| 129 | + end |
| 130 | + |
| 131 | + @spec get_log_level(atom) :: atom |
| 132 | + defp get_log_level(default) do |
| 133 | + Application.get_env(:timber, :instrumentation_level, default) |
| 134 | + end |
| 135 | +end |
0 commit comments