defmodule Microwaveprop.PromEx do
  @moduledoc """
  Prometheus metrics exporter. Combines PromEx's built-in plugins for
  Application/BEAM/Phoenix/Ecto/Oban with our own custom metrics
  (Microwaveprop.Instrument spans, Oban queue-depth gauges).

  A dedicated Prometheus server outside the Kubernetes cluster scrapes
  `/metrics` over the app's normal HTTP endpoint. The `MicrowavepropWeb.Router`
  is responsible for making that route reachable.
  """
  use PromEx, otp_app: :microwaveprop

  alias PromEx.Plugins

  @impl true
  def plugins do
    [
      # Defaults: VM memory, process counts, scheduler util, app version.
      Plugins.Application,
      Plugins.Beam,

      # Phoenix: request duration by route/status, channel/socket events.
      {Plugins.Phoenix, router: MicrowavepropWeb.Router, endpoint: MicrowavepropWeb.Endpoint},

      # Ecto: query duration, queue time, pool size.
      {Plugins.Ecto, otp_app: :microwaveprop, repos: [Microwaveprop.Repo]},

      # Oban: job duration, queue time, state counts, queue depth.
      Plugins.Oban,

      # Our own spans: Instrument.span/3 emits [:microwaveprop | suffix]
      # events; this plugin converts them to Prometheus histograms.
      Microwaveprop.PromEx.InstrumentPlugin
    ]
  end

  @impl true
  def dashboards do
    [
      {:prom_ex, "application.json"},
      {:prom_ex, "beam.json"},
      {:prom_ex, "phoenix.json"},
      {:prom_ex, "ecto.json"},
      {:prom_ex, "oban.json"}
    ]
  end
end
