3.6 KiB · text History 3208487
defmodule VigilWeb.SdkLogsConformanceTest do
  @moduledoc """
  Slice 9's exit with the real SDKs against a running server: sentry-elixir's
  logger handler and sentry-python's logger stream into the live tail
  (`Vigil.Logs.subscribe/1`) and are searchable. Skipped when a checkout is
  missing.
  """
  use VigilWeb.ConnCase, async: false

  import Vigil.AccountsFixtures
  import Vigil.OrganizationsFixtures

  alias Vigil.{Logs, Projects}
  alias Vigil.Logs.LogEvent

  @elixir_sdk System.get_env(
                "SENTRY_ELIXIR_PATH",
                Path.expand("~/Development/local/sentry_oss_2019/sentry-elixir")
              )

  @python_sdk System.get_env(
                "SENTRY_PYTHON_PATH",
                Path.expand("~/Development/local/sentry_oss_2019/sentry-python")
              )

  setup do
    owner = user_fixture()
    org = organization_fixture(owner)
    project = project_fixture(org_scope(owner, org))
    [key] = Projects.list_keys(project)

    server = start_supervised!({Bandit, plug: VigilWeb.Endpoint, port: 0, ip: :loopback})
    {:ok, {_ip, port}} = ThousandIsland.listener_info(server)

    hex = String.replace(key.public_key, "-", "")
    Logs.subscribe(project.id)
    %{project: project, dsn: "http://#{hex}@127.0.0.1:#{port}/#{project.dsn_id}"}
  end

  defp tail_bodies(acc \\ []) do
    receive do
      {:logs, events} -> tail_bodies(acc ++ Enum.map(events, & &1.body))
    after
      0 -> acc
    end
  end

  @tag :sdk
  @tag timeout: 300_000
  test "sentry-elixir's logger handler streams into the tail", ctx do
    if File.exists?(Path.join(@elixir_sdk, "mix.exs")) do
      script = Path.expand("../support/sdk/sentry_elixir_logs.exs", __DIR__)

      {out, status} =
        System.cmd("elixir", [script],
          env: [
            {"SENTRY_DSN", ctx.dsn},
            {"SENTRY_ELIXIR_PATH", @elixir_sdk},
            {"MIX_ENV", "prod"}
          ],
          stderr_to_stdout: true
        )

      assert status == 0, out
      Vigil.Ingest.Drain.drain(ctx.project.id)

      bodies = tail_bodies()
      assert "order placed" in bodies, "tail: #{inspect(bodies)}\n#{out}"
      assert "payment failed" in bodies
      refute "not sent: below the logs level" in bodies

      [placed] = Logs.search(ctx.project.id, Logs.parse_query(~s("order placed")))
      assert %LogEvent{level: "info", environment: "test", release: "vigil-logs@1.0"} = placed

      assert [%{body: "payment retry for order 42"}] =
               Logs.search(ctx.project.id, Logs.parse_query("level:warn"))
    else
      IO.puts("skipping sentry-elixir logs: no checkout at #{@elixir_sdk}")
    end
  end

  @tag :sdk
  test "sentry-python's logger streams into the tail", ctx do
    if File.dir?(Path.join(@python_sdk, "sentry_sdk")) and System.find_executable("python3") do
      script = """
      import sentry_sdk
      sentry_sdk.init(dsn=#{inspect(ctx.dsn)}, environment="test")
      sentry_sdk.logger.info("user {user} signed in", user="ada")
      sentry_sdk.logger.error("queue backed up", attributes={"queue.depth": 9000})
      sentry_sdk.flush(timeout=10)
      """

      {out, status} =
        System.cmd("python3", ["-c", script],
          env: [{"PYTHONPATH", @python_sdk}],
          stderr_to_stdout: true
        )

      assert status == 0, out
      Vigil.Ingest.Drain.drain(ctx.project.id)

      bodies = tail_bodies()
      assert "user ada signed in" in bodies, "tail: #{inspect(bodies)}\n#{out}"

      assert [%LogEvent{level: "error", attributes: %{"queue.depth" => 9000}}] =
               Logs.search(ctx.project.id, Logs.parse_query("queue"))
    else
      IO.puts("skipping sentry-python logs: no checkout at #{@python_sdk}")
    end
  end
end