spandex_phoenix v0.3.2 SpandexPhoenix

A Plug wrapper for use in a Plug.Router or Phoenix.Endpoint to trace the entire request with Spandex.

NOTE: If you want to use this in combination with Plug.ErrorHandler or similar “wrapper” plugs, this one should be last so that it traces the effects of the other wrappers.

Phoenix integration:

defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app
  use SpandexPhoenix

  # ...
end

Plug integration:

defmodule MyApp.Router do
  use Plug.Router
  use SpandexPhoenix

  # ...
end

Options for use Macro

  • :filter_traces (arity-1 function reference)

    A function that takes a Plug.Conn and returns true for requests to be traced. For example, to only trace certain HTTP methods, you could do something like:

    defmodule MyAppWeb.Endpoint do
      use Phoenix.Endpoint, otp_app: :my_app
      use SpandexPhoenix, filter_traces: &filter_traces/1
    
      def filter_traces(conn) do
        conn.method in ~w(DELETE GET POST PUT)
      end
    end

    NOTE: Local references to functions in the module being defined (e.g. &function/1) will not work because the module will not be compiled yet when the function is being referenced, so the function does not exist. Referencing the local function using &__MODULE__.function/1 will work, however.

    Default: (a private function that always returns true)

  • :span_name (String)

    The name to be used for the top level span.

    Default: “request”

  • :tracer (Atom)

    The tracing module to be used for the trace.

    Default: Application.get_env(:spandex_phoenix, :tracer)

  • :customize_metadata (arity-1 function reference)

    A function that takes the Plug.Conn for the current request and returns the desired span options to apply to the top-level span in the trace (as a Keyword). The Plug.Conn is normally evaluated just before the response is sent to the client, to ensure that the most-accurate metadata can be collected. In cases where there is an unhandled error, it may only represent the initial request without any response information.

    For example, if you want a particular path parameter to show its value in the resource instead of its name, you should do something like:

    defmodule MyApp.Tracer do
      use Spandex.Tracer, otp_app: :my_app
    
      def customize_metadata(conn) do
        name = conn.path_params["name"] || ""
    
        conn
        |> SpandexPhoenix.default_metadata()
        |> Keyword.update(:resource, "", &String.replace(&1, ":name", name))
      end
    end
    
    defmodule MyAppWeb.Endpoint do
      use Phoenix.Endpoint, otp_app: :my_app
      use SpandexPhoenix, customize_metadata: &MyApp.Tracer.customize_metadata/1
      plug Router
    
    end

    NOTE: Local references to functions in the module being defined (e.g. &function/1) will not work because the module will not be compiled yet when the function is being referenced, so the function does not exist. Referencing the local function using &__MODULE__.function/1 will work, however.

    Default: &SpandexPhoenix.default_metadata/1

Link to this section Summary

Link to this section Functions

Link to this function default_metadata(conn)
default_metadata(Plug.Conn.t()) :: Keyword.t()