telemetry.ex 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. defmodule ToyWeb.Telemetry do
  2. use Supervisor
  3. import Telemetry.Metrics
  4. def start_link(arg) do
  5. Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
  6. end
  7. @impl true
  8. def init(_arg) do
  9. children = [
  10. # Telemetry poller will execute the given period measurements
  11. # every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics
  12. {:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
  13. # Add reporters as children of your supervision tree.
  14. # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
  15. ]
  16. Supervisor.init(children, strategy: :one_for_one)
  17. end
  18. def metrics do
  19. [
  20. # Phoenix Metrics
  21. summary("phoenix.endpoint.stop.duration",
  22. unit: {:native, :millisecond}
  23. ),
  24. summary("phoenix.router_dispatch.stop.duration",
  25. tags: [:route],
  26. unit: {:native, :millisecond}
  27. ),
  28. # Database Metrics
  29. summary("toy.repo.query.total_time",
  30. unit: {:native, :millisecond},
  31. description: "The sum of the other measurements"
  32. ),
  33. summary("toy.repo.query.decode_time",
  34. unit: {:native, :millisecond},
  35. description: "The time spent decoding the data received from the database"
  36. ),
  37. summary("toy.repo.query.query_time",
  38. unit: {:native, :millisecond},
  39. description: "The time spent executing the query"
  40. ),
  41. summary("toy.repo.query.queue_time",
  42. unit: {:native, :millisecond},
  43. description: "The time spent waiting for a database connection"
  44. ),
  45. summary("toy.repo.query.idle_time",
  46. unit: {:native, :millisecond},
  47. description:
  48. "The time the connection spent waiting before being checked out for the query"
  49. ),
  50. # VM Metrics
  51. summary("vm.memory.total", unit: {:byte, :kilobyte}),
  52. summary("vm.total_run_queue_lengths.total"),
  53. summary("vm.total_run_queue_lengths.cpu"),
  54. summary("vm.total_run_queue_lengths.io")
  55. ]
  56. end
  57. defp periodic_measurements do
  58. [
  59. # A module, function and arguments to be invoked periodically.
  60. # This function must call :telemetry.execute/3 and a metric must be added above.
  61. # {ToyWeb, :count_users, []}
  62. ]
  63. end
  64. end