3.0 KiB · text History bbb2a48
defmodule Vigil.PerformanceFixtures do
  @moduledoc """
  Transactions and errors sent through the real ingest pipeline
  (`Ingest.accept` then `Drain.drain`), as the performance pages read them.
  """

  alias Vigil.Ingest
  alias Vigil.Ingest.{Drain, Envelope}

  @doc "An ingest target for `project`."
  def target(project) do
    %Ingest.Target{
      project_id: project.id,
      dsn_id: project.dsn_id,
      organization_id: project.organization_id,
      key_id: Ecto.UUID.generate()
    }
  end

  @doc "A random 32-hex trace id."
  def trace_id, do: hex(16)

  @doc "Sends one item and drains the project's queue."
  def send_item(project, type, payload) do
    item = %Envelope.Item{type: type, headers: %{"type" => type}, payload: Jason.encode!(payload)}

    Ingest.accept(target(project), %{headers: %{}, items: [item]}, %{
      client_ip: nil,
      user_agent: nil
    })

    Drain.drain(project.id)
  end

  @doc """
  A sentry-elixir style transaction with a db span and a nested query
  under it. Options: `name`, `ms` (duration, < 1000), `trace`, `status`.
  """
  def transaction_fixture(project, opts \\ []) do
    trace = Keyword.get(opts, :trace, trace_id())
    ms = Keyword.get(opts, :ms, 120)
    root = hex(8)
    db = hex(8)

    send_item(project, "transaction", %{
      "type" => "transaction",
      "event_id" => hex(16),
      "transaction" => Keyword.get(opts, :name, "GET /users/:id"),
      "start_timestamp" => "2026-12-01T10:00:00.000000Z",
      "timestamp" => "2026-12-01T10:00:00.#{String.pad_leading("#{ms}", 3, "0")}000Z",
      "environment" => "prod",
      "release" => "web@1.0",
      "platform" => "elixir",
      "request" => %{"method" => "GET", "url" => "http://localhost/users/1"},
      "contexts" => %{
        "trace" => %{
          "trace_id" => trace,
          "span_id" => root,
          "op" => "http.server",
          "status" => Keyword.get(opts, :status, "ok")
        }
      },
      "spans" => [
        %{
          "span_id" => db,
          "parent_span_id" => root,
          "trace_id" => trace,
          "op" => "db",
          "description" => "SELECT * FROM users WHERE id = $1",
          "start_timestamp" => "2026-12-01T10:00:00.002000Z",
          "timestamp" => "2026-12-01T10:00:00.008000Z",
          "data" => %{"db.system" => "postgresql"}
        },
        %{
          "span_id" => hex(8),
          "parent_span_id" => db,
          "trace_id" => trace,
          "op" => "db.query",
          "description" => "nested",
          "start_timestamp" => "2026-12-01T10:00:00.003000Z",
          "timestamp" => "2026-12-01T10:00:00.004000Z"
        }
      ]
    })

    trace
  end

  @doc "An error event in `trace`."
  def trace_error_fixture(project, trace, message \\ "boom") do
    send_item(project, "event", %{
      "event_id" => hex(16),
      "level" => "error",
      "message" => message,
      "contexts" => %{"trace" => %{"trace_id" => trace, "span_id" => hex(8)}}
    })
  end

  defp hex(bytes), do: bytes |> :crypto.strong_rand_bytes() |> Base.encode16(case: :lower)
end