Exq.Middleware.Telemetry (exq v0.19.0) View Source

This middleware allows you to subscribe to the telemetry events and collect metrics about your jobs.

Exq telemetry events

The middleware emit three events, same as what :telemetry.span/3 emits.

  • [:exq, :job, :start] - Is invoked whenever a job starts.

    Measurements

    • system_time (integer) - System time when the job started
  • [:exq, :job, :stop] - Is invoked whenever a job completes successfully.

    Measurements

    • duration (integer) - Duration of the job execution in native unit
  • [:exq, :job, :exception] - Is invoked whenever a job fails.

    Measurements

    • duration (integer) - Duration of the job execution in native unit Metadata In addition to the common metadata, exception event will have the following fields.
    • kind (exit | error) - either exit or error
    • reason (term) - could be an Exception.t/0 or term
    • stacktrace (list) - Stacktrace of the error. Will be empty if the kind is exit.

Metadata

Each event has the following common metadata:

  • enqueued_at (DateTime.t/0) - datetime the job was enqueued
  • queue (String.t/0) - the name of the queue the job was executed in
  • class (String.t/0) - the job's class
  • jid (String.t/0) - the job's jid
  • retry_count (integer) - number of times this job has failed so far

Examples

defmodule MyApp.Application do
  def start(_type, _args) do
    children = [
      # .....
      {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end

  defp metrics do
    [
      counter("exq.job.stop.duration"),
      counter("exq.job.exception.duration"),
      distribution("exq.job.stop.duration",
        buckets: [0.1, 0.2, 0.3, 0.5, 0.75, 1, 2, 3, 5, 10],
        unit: {:native, :millisecond}
      ),
      distribution("exq.job.exception.duration",
        buckets: [0.1, 0.2, 0.3, 0.5, 0.75, 1, 2, 3, 5, 10],
        unit: {:native, :millisecond}
      ),
      summary("exq.job.stop.duration", unit: {:native, :millisecond}),
      summary("exq.job.exception.duration", unit: {:native, :millisecond})
    ]
  end
end