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) in [:web, :worker, :combined] 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"
      ),
      distribution("who_need_help.http.request.duration.seconds",
        event_name: [:phoenix, :endpoint, :stop],
        measurement: :duration,
        unit: {:native, :second},
        reporter_options: [
          buckets: [0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0]
        ],
        description: "HTTP request duration distribution"
      ),
      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"
      ),
      distribution("who_need_help.database.query.duration.seconds",
        event_name: [:who_need_help, :repo, :query],
        measurement: :total_time,
        unit: {:native, :second},
        reporter_options: [
          buckets: [
            0.000_05,
            0.000_1,
            0.000_25,
            0.000_5,
            0.001,
            0.0025,
            0.005,
            0.01,
            0.025,
            0.05,
            0.1
          ]
        ],
        description: "Database query duration distribution"
      ),
      distribution("who_need_help.database.query.queue.duration.seconds",
        event_name: [:who_need_help, :repo, :query],
        measurement: fn measurements -> Map.get(measurements, :queue_time, 0) end,
        unit: {:native, :second},
        reporter_options: [
          buckets: [
            0.000_01,
            0.000_025,
            0.000_05,
            0.000_1,
            0.000_25,
            0.000_5,
            0.001,
            0.0025,
            0.005,
            0.01,
            0.025
          ]
        ],
        description: "Time waiting for an Ecto 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"
      ),
      counter("who_need_help.oban.jobs.completed.total",
        event_name: [:oban, :job, :stop],
        measurement: :duration,
        tags: [:queue],
        description: "Completed Oban jobs"
      ),
      counter("who_need_help.oban.jobs.failed.total",
        event_name: [:oban, :job, :exception],
        measurement: :duration,
        tags: [:queue],
        description: "Failed Oban job attempts"
      ),
      sum("who_need_help.oban.job.duration.microseconds.total",
        event_name: [:oban, :job, :stop],
        measurement: fn measurements ->
          System.convert_time_unit(measurements.duration, :native, :microsecond)
        end,
        tags: [:queue],
        description: "Cumulative successful Oban execution duration"
      ),
      sum("who_need_help.oban.job.queue.duration.microseconds.total",
        event_name: [:oban, :job, :stop],
        measurement: fn measurements ->
          System.convert_time_unit(measurements.queue_time, :native, :microsecond)
        end,
        tags: [:queue],
        description: "Cumulative Oban queue wait duration"
      ),
      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
