who_need_help/lib/who_need_help_web/telemetry.ex

189 lines
6.5 KiB
Elixir

defmodule WhoNeedHelpWeb.Telemetry do
use Supervisor
import Telemetry.Metrics
def start_link(arg) do
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
end
@impl true
def init(_arg) do
reporter_children =
if Application.fetch_env!(:who_need_help, :app_role) == :web do
[
{TelemetryMetricsPrometheus.Core,
name: :prometheus_metrics, metrics: prometheus_metrics(), start_async: false}
]
else
[]
end
children =
reporter_children ++
[
# Telemetry poller will execute the given period measurements
# every 10_000ms. Learn more here: https://telemetry-metrics.hexdocs.pm
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
]
Supervisor.init(children, strategy: :one_for_one)
end
def prometheus_metrics do
[
counter("who_need_help.http.requests.total",
event_name: [:phoenix, :endpoint, :stop],
measurement: :duration,
description: "Completed HTTP requests"
),
sum("who_need_help.http.request.duration.microseconds.total",
event_name: [:phoenix, :endpoint, :stop],
measurement: fn measurements ->
System.convert_time_unit(measurements.duration, :native, :microsecond)
end,
description: "Cumulative HTTP request duration"
),
counter("who_need_help.http.exceptions.total",
event_name: [:phoenix, :router_dispatch, :exception],
measurement: :duration,
description: "Router dispatch exceptions"
),
counter("who_need_help.database.queries.total",
event_name: [:who_need_help, :repo, :query],
measurement: :total_time,
description: "Completed database queries"
),
sum("who_need_help.database.query.duration.microseconds.total",
event_name: [:who_need_help, :repo, :query],
measurement: fn measurements ->
System.convert_time_unit(measurements.total_time, :native, :microsecond)
end,
description: "Cumulative database query duration"
),
sum("who_need_help.database.query.execution.duration.microseconds.total",
event_name: [:who_need_help, :repo, :query],
measurement: fn measurements ->
System.convert_time_unit(measurements.query_time, :native, :microsecond)
end,
description: "Cumulative database execution duration"
),
sum("who_need_help.database.query.queue.duration.microseconds.total",
event_name: [:who_need_help, :repo, :query],
measurement: fn measurements ->
measurements
|> Map.get(:queue_time, 0)
|> System.convert_time_unit(:native, :microsecond)
end,
description: "Cumulative time waiting for a database connection"
),
sum("who_need_help.database.query.decode.duration.microseconds.total",
event_name: [:who_need_help, :repo, :query],
measurement: fn measurements ->
measurements
|> Map.get(:decode_time, 0)
|> System.convert_time_unit(:native, :microsecond)
end,
description: "Cumulative database result decoding duration"
),
counter("who_need_help.websocket.connections.total",
event_name: [:phoenix, :socket_connected],
measurement: :duration,
description: "Completed WebSocket connection attempts"
),
last_value("who_need_help.vm.memory.total.bytes",
event_name: [:vm, :memory],
measurement: :total,
unit: :byte,
description: "Total memory used by the Erlang VM"
),
last_value("who_need_help.vm.run_queue.total",
event_name: [:vm, :total_run_queue_lengths],
measurement: :total,
description: "Total scheduler run queue length"
),
last_value("who_need_help.vm.run_queue.cpu",
event_name: [:vm, :total_run_queue_lengths],
measurement: :cpu,
description: "CPU scheduler run queue length"
),
last_value("who_need_help.vm.run_queue.io",
event_name: [:vm, :total_run_queue_lengths],
measurement: :io,
description: "I/O scheduler run queue length"
)
]
end
def metrics do
[
# Phoenix Metrics
summary("phoenix.endpoint.start.system_time",
unit: {:native, :millisecond}
),
summary("phoenix.endpoint.stop.duration",
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.start.system_time",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.exception.duration",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.stop.duration",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.socket_connected.duration",
unit: {:native, :millisecond}
),
sum("phoenix.socket_drain.count"),
summary("phoenix.channel_joined.duration",
unit: {:native, :millisecond}
),
summary("phoenix.channel_handled_in.duration",
tags: [:event],
unit: {:native, :millisecond}
),
# Database Metrics
summary("who_need_help.repo.query.total_time",
unit: {:native, :millisecond},
description: "The sum of the other measurements"
),
summary("who_need_help.repo.query.decode_time",
unit: {:native, :millisecond},
description: "The time spent decoding the data received from the database"
),
summary("who_need_help.repo.query.query_time",
unit: {:native, :millisecond},
description: "The time spent executing the query"
),
summary("who_need_help.repo.query.queue_time",
unit: {:native, :millisecond},
description: "The time spent waiting for a database connection"
),
summary("who_need_help.repo.query.idle_time",
unit: {:native, :millisecond},
description:
"The time the connection spent waiting before being checked out for the query"
),
# VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"),
summary("vm.total_run_queue_lengths.cpu"),
summary("vm.total_run_queue_lengths.io")
]
end
defp periodic_measurements do
[
# A module, function and arguments to be invoked periodically.
# This function must call :telemetry.execute/3 and a metric must be added above.
# {WhoNeedHelpWeb, :count_users, []}
]
end
end