defmodule VigilWeb.PerformanceLive.Index do
@moduledoc """
A project's transactions (`Vigil.Performance.list_groups/2`) over the
last 24 hours: name, op and method, throughput, average and percentile
durations, error rate and count. `sort` and `q` (a name substring) live
in the URL. New transactions refresh the list live (PubSub), at most
once a second. A project with none yet explains how to turn tracing on.
"""
use VigilWeb, :live_view
import VigilWeb.PerformanceComponents
alias Vigil.Performance
@refresh_ms 1000
@sorts [
{"count", :count, "Most frequent"},
{"p95", :p95, "Slowest p95"},
{"avg", :avg, "Slowest average"},
{"errors", :errors, "Error rate"},
{"recent", :recent, "Latest"}
]
@impl true
def mount(_params, _session, socket) do
project = socket.assigns.project
if connected?(socket),
do: Phoenix.PubSub.subscribe(Vigil.PubSub, "project:#{project.id}:performance")
{:ok,
socket
|> assign(:page_title, "#{project.name} · Performance")
|> assign(:sorts, @sorts)
|> assign(:query, "")
|> assign(:sort, "count")
|> assign(:count, 0)
|> assign(:refresh_scheduled?, false)
|> stream_configure(:groups, dom_id: &"txn-#{&1.id}")
|> stream(:groups, [])}
end
@impl true
def handle_params(params, _uri, socket) do
sort = if params["sort"] in Enum.map(@sorts, &elem(&1, 0)), do: params["sort"], else: "count"
{:noreply,
socket
|> assign(query: params["q"] || "", sort: sort)
|> assign(:search_form, to_form(%{"q" => params["q"] || ""}))
|> load()}
end
defp load(socket) do
%{project: project, query: query, sort: sort} = socket.assigns
{_, sort_key, _} = List.keyfind(@sorts, sort, 0)
groups = Performance.list_groups(project.id, sort: sort_key, query: query)
socket
|> assign(:count, length(groups))
|> assign(:ever?, groups != [] or Performance.any_groups?(project.id))
|> stream(:groups, groups, reset: true)
end
@impl true
def handle_event("search", %{"q" => q}, socket),
do: {:noreply, push_patch(socket, to: list_path(socket.assigns, %{"q" => q}))}
@impl true
def handle_info({:transactions, _}, socket), do: {:noreply, schedule_refresh(socket)}
def handle_info(:refresh, socket),
do: {:noreply, socket |> assign(:refresh_scheduled?, false) |> load()}
defp schedule_refresh(%{assigns: %{refresh_scheduled?: true}} = socket), do: socket
defp schedule_refresh(socket) do
Process.send_after(self(), :refresh, @refresh_ms)
assign(socket, :refresh_scheduled?, true)
end
defp list_path(assigns, changes) do
params =
%{"q" => assigns.query, "sort" => assigns.sort}
|> Map.merge(changes)
|> Enum.reject(fn {k, v} -> v in [nil, ""] or (k == "sort" and v == "count") end)
~p"/orgs/#{assigns.current_scope.organization.slug}/projects/#{assigns.project.slug}/performance?#{params}"
end
defp group_path(scope, project, group),
do: ~p"/orgs/#{scope.organization.slug}/projects/#{project.slug}/performance/#{group.id}"
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} active={:projects}>
<div class="mx-auto max-w-6xl space-y-6">
<.header>
{@project.name} performance
<:subtitle>
How long your transactions take and how often they fail, over the last 24 hours.
</:subtitle>
<:actions>
<.link
navigate={~p"/orgs/#{@current_scope.organization.slug}/projects/#{@project.slug}"}
class="btn btn-ghost btn-sm"
id="performance-back"
>
<.icon name="hero-arrow-left-micro" class="size-4" /> Issues
</.link>
</:actions>
</.header>
<%= if @ever? do %>
<div class="flex flex-wrap items-center gap-3">
<.form
for={@search_form}
id="txn-search"
phx-change="search"
phx-submit="search"
class="min-w-0 flex-1"
>
<label class="input input-sm w-full">
<.icon name="hero-magnifying-glass-micro" class="size-4 opacity-50" />
<input
type="search"
name="q"
id="txn-search-q"
value={@query}
placeholder="Find a transaction"
phx-debounce="250"
autocomplete="off"
/>
</label>
</.form>
<div role="tablist" class="tabs tabs-box tabs-sm" id="txn-sorts">
<.link
:for={{key, _, label} <- @sorts}
patch={list_path(assigns, %{"sort" => key})}
id={"sort-#{key}"}
role="tab"
class={["tab", @sort == key && "tab-active"]}
>
{label}
</.link>
</div>
</div>
<div class="overflow-hidden rounded-box border border-base-300 bg-base-100">
<div class="hidden grid-cols-[minmax(0,1fr)_5rem_5rem_5rem_5rem_5rem_5rem] gap-4 border-b border-base-300 bg-base-200/50 px-4 py-2 text-xs font-medium uppercase tracking-wide text-base-content/60 md:grid">
<span>Transaction</span>
<span class="text-right" title="Transactions a minute">TPM</span>
<span class="text-right">Avg</span>
<span class="text-right">p50</span>
<span class="text-right">p95</span>
<span class="text-right">Errors</span>
<span class="text-right">Count</span>
</div>
<ul id="transactions" phx-update="stream" class="divide-y divide-base-300">
<li
id="transactions-empty"
class="hidden px-4 py-12 text-center text-sm text-base-content/60 only:block"
>
<.icon name="hero-bolt" class="mx-auto mb-2 block size-8 opacity-40" />
<%= if @query != "" do %>
No transaction matches “{@query}” in the last 24 hours.
<% else %>
No transactions in the last 24 hours.
<% end %>
</li>
<li :for={{dom_id, g} <- @streams.groups} id={dom_id}>
<.link
navigate={group_path(@current_scope, @project, g)}
class="group grid grid-cols-3 gap-x-4 gap-y-1 px-4 py-3 transition hover:bg-base-200/60 md:grid-cols-[minmax(0,1fr)_5rem_5rem_5rem_5rem_5rem_5rem] md:items-center"
>
<div class="col-span-3 min-w-0 md:col-span-1">
<p class="flex min-w-0 items-center gap-2">
<span
class="truncate font-mono text-sm font-semibold group-hover:text-primary"
title={g.name}
>
{g.name}
</span>
<.txn_badges group={g} />
</p>
</div>
<.cell label="TPM" class="txn-tpm">{tpm(g.stats.tpm)}</.cell>
<.cell label="Avg" class="txn-avg">{duration(g.stats.avg_ms)}</.cell>
<.cell label="p50" class="txn-p50">{duration(g.stats.p50)}</.cell>
<.cell label="p95" class="txn-p95">{duration(g.stats.p95)}</.cell>
<.cell label="Errors" class="txn-errors">
<span class={[g.stats.error_count > 0 && "font-semibold text-error"]}>
{rate(g.stats.error_rate)}
</span>
</.cell>
<.cell label="Count" class="txn-count">{g.stats.count}</.cell>
</.link>
</li>
</ul>
</div>
<p :if={@count >= 100} class="text-center text-xs text-base-content/60">
Showing 100 transactions. Search to find others.
</p>
<% else %>
<.setup_tracing />
<% end %>
</div>
</Layouts.app>
"""
end
attr :label, :string, required: true
attr :class, :string, required: true
slot :inner_block, required: true
defp cell(assigns) do
~H"""
<span class={["text-sm tabular-nums md:text-right", @class]}>
<span class="text-xs text-base-content/50 md:hidden">{@label}</span>
{render_slot(@inner_block)}
</span>
"""
end
defp setup_tracing(assigns) do
~H"""
<section
id="performance-empty"
class="rounded-box border border-dashed border-base-300 bg-base-100 p-8"
>
<div class="mx-auto max-w-2xl space-y-5">
<div class="text-center">
<.icon name="hero-bolt" class="mx-auto mb-2 block size-10 text-primary/60" />
<h2 class="text-lg font-semibold">No transactions yet</h2>
<p class="mt-1 text-sm text-base-content/70">
Turn on tracing in your SDK and each request or job it measures shows up here,
grouped by name, with its timings and the spans inside it.
</p>
</div>
<div class="space-y-2">
<h3 class="text-sm font-semibold">sentry-elixir</h3>
<p class="text-sm text-base-content/70">
Set a sample rate and add the OpenTelemetry packages
(<code class="font-mono">opentelemetry</code>, <code class="font-mono">opentelemetry_phoenix</code>, <code class="font-mono">opentelemetry_ecto</code>, …), with Sentry's span processor:
</p>
<pre
id="setup-elixir"
class="overflow-x-auto rounded-box bg-base-200 p-3 text-xs"
phx-no-curly-interpolation
><code># config/config.exs
config :sentry,
dsn: "…",
traces_sample_rate: 1.0
config :opentelemetry,
span_processor: {Sentry.OpenTelemetry.SpanProcessor, []},
sampler: {Sentry.OpenTelemetry.Sampler, []}</code></pre>
</div>
<div class="space-y-2">
<h3 class="text-sm font-semibold">sentry-python</h3>
<pre
id="setup-python"
class="overflow-x-auto rounded-box bg-base-200 p-3 text-xs"
phx-no-curly-interpolation
><code>sentry_sdk.init(
dsn="…",
traces_sample_rate=1.0,
)</code></pre>
</div>
<p class="text-xs text-base-content/60">
A rate of <code class="font-mono">1.0</code>
traces everything; lower it in busy production apps.
</p>
</div>
</section>
"""
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
10.4 KiB · text
History
573fef1