3.0 KiB · text History 3208487
defmodule VigilWeb.SdkSessionsConformanceTest do
  @moduledoc """
  Slice 10 with the real sentry-python against a running server: an
  application-mode session that crashes (a `session` item, init then
  crashed) and request-mode sessions (a `sessions` aggregate) become the
  release's crash-free rate and adoption. sentry-elixir sends no sessions.
  Skipped without the checkout.
  """
  use VigilWeb.ConnCase, async: false

  import Ecto.Query
  import Vigil.AccountsFixtures
  import Vigil.OrganizationsFixtures

  alias Vigil.{Projects, ReleaseHealth}

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

  @tag :sdk
  test "sentry-python sessions become crash-free and adoption numbers" do
    if File.dir?(Path.join(@python_sdk, "sentry_sdk")) and System.find_executable("python3") 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)

      dsn =
        "http://#{String.replace(key.public_key, "-", "")}@127.0.0.1:#{port}/#{project.dsn_id}"

      script = """
      import sentry_sdk
      from sentry_sdk.sessions import track_session
      sentry_sdk.init(dsn=#{inspect(dsn)}, release="py@1.0", environment="prod")

      # Request mode (what the web integrations do): aggregated.
      for i in range(4):
          with track_session(sentry_sdk.get_isolation_scope(), session_mode="request"):
              pass

      # Application mode: one session for the process, which crashes.
      sentry_sdk.set_user({"id": "ada"})
      scope = sentry_sdk.get_isolation_scope()
      scope.start_session(session_mode="application")
      try:
          1 / 0
      except ZeroDivisionError as e:
          sentry_sdk.capture_exception(e)
          # An unhandled crash: how the excepthook integration reports it.
          scope._session.update(status="crashed")
      scope.end_session()
      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(project.id)

      types =
        Vigil.Repo.all(
          from i in Vigil.Ingest.BufferedItem, where: i.project_id == ^project.id, select: i.type
        )

      health = ReleaseHealth.summary(project.id, "py@1.0")
      assert health.sessions == 5, "#{inspect(health)} items left: #{inspect(types)}\n#{out}"
      assert health.crashed == 1
      assert_in_delta health.crash_free_sessions, 0.8, 0.001
      assert health.users == 1 and health.crashed_users == 1
      assert health.adoption == 1.0
    else
      IO.puts("skipping sentry-python sessions: no checkout at #{@python_sdk}")
    end
  end
end