logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

metrics_exporter_test.exs (1868B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Endpoint.MetricsExporterTest do
  5. # Modifies AppEnv, has to stay synchronous
  6. use Pleroma.Web.ConnCase
  7. alias Pleroma.Web.Endpoint.MetricsExporter
  8. defp config do
  9. Application.get_env(:prometheus, MetricsExporter)
  10. end
  11. describe "with default config" do
  12. test "does NOT expose app metrics", %{conn: conn} do
  13. conn
  14. |> get(config()[:path])
  15. |> json_response(404)
  16. end
  17. end
  18. describe "when enabled" do
  19. setup do
  20. initial_config = config()
  21. on_exit(fn -> Application.put_env(:prometheus, MetricsExporter, initial_config) end)
  22. Application.put_env(
  23. :prometheus,
  24. MetricsExporter,
  25. Keyword.put(initial_config, :enabled, true)
  26. )
  27. end
  28. test "serves app metrics", %{conn: conn} do
  29. conn = get(conn, config()[:path])
  30. assert response = response(conn, 200)
  31. for metric <- [
  32. "http_requests_total",
  33. "http_request_duration_microseconds",
  34. "phoenix_controller_call_duration",
  35. "telemetry_scrape_duration",
  36. "erlang_vm_memory_atom_bytes_total"
  37. ] do
  38. assert response =~ ~r/#{metric}/
  39. end
  40. end
  41. test "when IP whitelist configured, " <>
  42. "serves app metrics only if client IP is whitelisted",
  43. %{conn: conn} do
  44. Application.put_env(
  45. :prometheus,
  46. MetricsExporter,
  47. Keyword.put(config(), :ip_whitelist, ["127.127.127.127", {1, 1, 1, 1}, '255.255.255.255'])
  48. )
  49. conn
  50. |> get(config()[:path])
  51. |> json_response(404)
  52. conn
  53. |> Map.put(:remote_ip, {127, 127, 127, 127})
  54. |> get(config()[:path])
  55. |> response(200)
  56. end
  57. end
  58. end