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
    # Oban plugin's queue-depth metric is a telemetry_poller that runs
    # an Ecto query every 5 s; in the test env that poller has no
    # sandbox connection and crashes with DBConnection.OwnershipError,
    # so it's skipped there.
    oban_plugin =
      if Application.get_env(:microwaveprop, :prom_ex_include_oban_plugin, true),
        do: [Plugins.Oban],
        else: []

    [
      # 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]},

      # Our own spans: Instrument.span/3 emits [:microwaveprop | suffix]
      # events; this plugin converts them to Prometheus histograms.
      Microwaveprop.PromEx.InstrumentPlugin
    ] ++ oban_plugin
  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
