prometheus_phoenix v1.2.1 Prometheus.PhoenixInstrumenter View Source

Phoenix instrumenter generator for Prometheus. Implemented as Phoenix instrumenter.

Usage

  1. Define your instrumenter:
defmodule MyApp.Endpoint.Instrumenter do
  use Prometheus.PhoenixInstrumenter
end
  1. Call MyApp.Endpoint.Instrumenter.setup/0 when application starts (e.g. supervisor setup):
MyApp.Endpoint.Instrumenter.setup()
  1. Add MyApp.Endpoint.Instrumenter to Phoenix endpoint instrumenters list:
config :myapp, MyApp.Endpoint,
  ...
  instrumenters: [MyApp.Endpoint.Instrumenter]
  ...

Metrics

Metrics implemented for the following built-in events:

  • phoenix_controller_call

    • phoenix_controller_call_duration_<duration_unit>;
  • phoenix_controller_render

    • phoenix_controller_render_duration_<duration_unit>;
  • phoenix_channel_join

    • phoenix_channel_join_duration_<duration_unit>;
  • phoenix_channel_receive

    • phoenix_channel_receive_duration_<duration_unit>.

Predefined controller call labels:

  • action - action name (default);
  • controller - controller module name (default).

Predefined controller render labels:

  • format - name of the format of the template (default);
  • template - name of the template (default);
  • view - name of the view (default).

Predefined channel join/receive labels:

  • channel - current channel module (default);
  • endpoint - endpoint module where this socket originated;
  • handler - socket module where this socket originated;
  • pubsub_server - registered name of the socket’s pubsub server;
  • serializer - serializer for socket messages;
  • topic - string topic (default);
  • transport - socket’s transport;
  • transport_name - socket’s transport (default);
  • vsn - protocol version of the client (default).

Predefined channel receive labels:

  • event - event name (default).

Predefined common http request labels:

  • host - request host;
  • method - request method;
  • port - request port;
  • scheme - request scheme.

Predefined compile metadata labels:

  • application - name of OTP application;
  • file - name of file where instrumented function resides;
  • function - name of the instrumented function;
  • line - source line number;
  • module - instrumented function’s module.

Configuration

Instrumenter configured via :prometheus application environment MyApp.Endpoint.Instrumenter key (i.e. app env key is the name of the instrumenter).

Default configuration:

config :prometheus, MyApp.Endpoint.Instrumenter,
  controller_call_labels: [:controller, :action],
  duration_buckets: :prometheus_http.microseconds_duration_buckets(),
  registry: :default,
  duration_unit: :microseconds

Available duration units:

  • microseconds;
  • milliseconds;
  • seconds;
  • minutes;
  • hours;
  • days.

Bear in mind that buckets are so if you are not using default unit you also have to override buckets.

Custom Labels

Custom labels can be defined by implementing label_value/2 function in instrumenter directly or by calling exported function from other module.

  controller_call_labels: [:controller,
                           :my_private_label,
                           {:label_from_other_module, Module}, # eqv to {Module, label_value}
                           {:non_default_label_value, {Module, custom_fun}}]

defmodule MyApp.Endpoint.Instrumenter do
  use Prometheus.PhoenixInstrumenter

  label_value(:my_private_label, conn) do
    ...
  end
end