defmodule VigilWeb.TraceLive.Show do
@moduledoc """
One trace across the organization's projects the caller may see
(`Vigil.Performance.get_trace/2`): a waterfall with a row per span,
indented by depth, its bar placed by its offset and duration within the
trace, transactions (segment spans) highlighted; a row expands, client
side, to show the span's status, ids and attributes. Below it, the
errors that happened in the trace, each linking to its event.
At most 1000 spans are drawn; the rest are counted.
"""
use VigilWeb, :live_view
import VigilWeb.PerformanceComponents
alias Phoenix.LiveView.JS
alias Vigil.{Performance, Projects}
@max_rows 1000
@impl true
def mount(%{"trace_id" => trace_id}, _session, socket) do
scope = socket.assigns.current_scope
projects = Projects.list_visible(scope)
trace = Performance.get_trace(Enum.map(projects, & &1.id), trace_id)
socket =
socket
|> assign(:page_title, "Trace #{String.slice(trace_id, 0, 8)}")
|> assign(:trace_id, trace_id)
|> assign(:max_rows, @max_rows)
|> assign(:trace, trace && Map.delete(trace, :spans))
socket =
case trace do
nil ->
socket
trace ->
by_id = Map.new(projects, &{&1.id, &1})
span_projects = trace.spans |> Enum.map(& &1.project_id) |> Enum.uniq()
socket
|> assign(:span_count, length(trace.spans))
|> assign(:projects, by_id)
|> assign(:span_projects, Enum.map(span_projects, &by_id[&1]))
|> assign(:segment_count, Enum.count(trace.spans, & &1.is_segment))
|> stream_configure(:spans, dom_id: &"span-#{&1.id}")
|> stream(:spans, Enum.take(trace.spans, @max_rows))
end
{:ok, socket}
end
defp event_path(scope, event),
do:
~p"/orgs/#{scope.organization.slug}/projects/#{event.issue.project.slug}/issues/#{event.issue.short_id}/events/#{event.id}"
@impl true
def render(%{trace: nil} = assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} active={:projects}>
<div
id="trace-missing"
class="mx-auto max-w-xl rounded-box border border-dashed border-base-300 bg-base-100 p-10 text-center"
>
<.icon name="hero-magnifying-glass" class="mx-auto mb-3 block size-10 opacity-40" />
<h1 class="text-lg font-semibold">Trace not found</h1>
<p class="mt-1 text-sm text-base-content/70">
No spans or errors of trace <code class="break-all font-mono">{@trace_id}</code>
are kept in a project you can see. It may have aged out of retention, or was never sampled.
</p>
<.link
navigate={~p"/orgs/#{@current_scope.organization.slug}"}
class="btn btn-ghost btn-sm mt-4"
id="trace-missing-back"
>
<.icon name="hero-arrow-left-micro" class="size-4" /> {@current_scope.organization.name}
</.link>
</div>
</Layouts.app>
"""
end
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 id="trace-header" class="space-y-3">
<p class="text-xs font-medium uppercase tracking-wide text-base-content/60">Trace</p>
<h1 id="trace-id" class="break-all font-mono text-lg font-semibold sm:text-xl">
{@trace.trace_id}
</h1>
<dl class="flex flex-wrap gap-x-6 gap-y-2 text-sm">
<div>
<dt class="text-xs text-base-content/60">Duration</dt>
<dd id="trace-duration" class="font-semibold tabular-nums">
{duration(@trace.duration_ms)}
</dd>
</div>
<div>
<dt class="text-xs text-base-content/60">Spans</dt>
<dd id="trace-span-count" class="font-semibold tabular-nums">{@span_count}</dd>
</div>
<div>
<dt class="text-xs text-base-content/60">Transactions</dt>
<dd class="font-semibold tabular-nums">{@segment_count}</dd>
</div>
<div>
<dt class="text-xs text-base-content/60">Errors</dt>
<dd class={[
"font-semibold tabular-nums",
@trace.errors != [] && "text-error"
]}>
{length(@trace.errors)}
</dd>
</div>
<div :if={@trace.start_at}>
<dt class="text-xs text-base-content/60">Started</dt>
<dd title={DateTime.to_iso8601(@trace.start_at)}>
{Calendar.strftime(@trace.start_at, "%Y-%m-%d %H:%M:%S UTC")}
</dd>
</div>
<div :if={@span_projects != []}>
<dt class="text-xs text-base-content/60">Projects</dt>
<dd class="flex flex-wrap gap-1">
<.link
:for={p <- @span_projects}
navigate={
~p"/orgs/#{@current_scope.organization.slug}/projects/#{p.slug}/performance"
}
class="badge badge-ghost badge-sm transition hover:badge-primary"
>
{p.slug}
</.link>
</dd>
</div>
</dl>
</header>
<section
:if={@trace.errors != []}
id="trace-errors"
class="space-y-2"
aria-labelledby="trace-errors-title"
>
<h2 id="trace-errors-title" class="flex items-center gap-1.5 text-sm font-semibold">
<.icon name="hero-exclamation-triangle-micro" class="size-4 text-error" />
Errors in this trace
</h2>
<ul class="divide-y divide-base-300 rounded-box border border-base-300 bg-base-100">
<li :for={e <- @trace.errors} id={"trace-error-#{e.id}"}>
<.link
navigate={event_path(@current_scope, e)}
class="group flex items-center gap-3 px-4 py-3 transition hover:bg-base-200/60"
>
<span class="h-8 w-1 shrink-0 rounded-full bg-error" aria-hidden="true" />
<div class="min-w-0 flex-1">
<p class="truncate text-sm font-medium group-hover:text-primary">
{e.title || e.issue.title}
</p>
<p class="truncate text-xs text-base-content/60">
<span class="font-mono">
{String.upcase(e.issue.project.slug)}-{e.issue.short_id}
</span>
<span :if={e.culprit}> · {e.culprit}</span>
</p>
</div>
<.icon
name="hero-chevron-right-micro"
class="size-4 shrink-0 opacity-40 transition-transform group-hover:translate-x-0.5"
/>
</.link>
</li>
</ul>
</section>
<section id="trace-waterfall" class="space-y-2" aria-labelledby="trace-waterfall-title">
<h2 id="trace-waterfall-title" class="text-sm font-semibold">Spans</h2>
<div class="overflow-hidden rounded-box border border-base-300 bg-base-100">
<div class="grid grid-cols-[minmax(0,2fr)_minmax(0,3fr)_4.5rem] gap-3 border-b border-base-300 bg-base-200/50 px-3 py-2 text-xs font-medium uppercase tracking-wide text-base-content/60">
<span>Operation</span>
<span class="flex justify-between normal-case tracking-normal">
<span>0</span><span>{duration(@trace.duration_ms)}</span>
</span>
<span class="text-right">Duration</span>
</div>
<ul id="spans" phx-update="stream" class="divide-y divide-base-200">
<li
id="spans-empty"
class="hidden px-4 py-8 text-center text-sm text-base-content/60 only:block"
>
No spans of this trace are kept, only its errors.
</li>
<li
:for={{dom_id, s} <- @streams.spans}
id={dom_id}
data-depth={s.depth}
class={[s.is_segment && "bg-primary/5"]}
>
<button
type="button"
id={"#{dom_id}-row"}
aria-expanded="false"
aria-controls={"#{dom_id}-detail"}
phx-click={
JS.toggle(to: "##{dom_id}-detail")
|> JS.toggle_class("rotate-90", to: "##{dom_id}-chev")
|> JS.toggle_attribute({"aria-expanded", "true", "false"})
}
class="grid w-full cursor-pointer grid-cols-[minmax(0,2fr)_minmax(0,3fr)_4.5rem] items-center gap-3 px-3 py-1.5 text-left transition hover:bg-base-200/70"
>
<span
class="flex min-w-0 items-center gap-1.5"
style={"padding-left: #{min(s.depth, 24) * 0.75}rem"}
>
<span id={"#{dom_id}-chev"} class="shrink-0 transition-transform">
<.icon name="hero-chevron-right-micro" class="size-3 opacity-50" />
</span>
<.icon
:if={failed?(s.status)}
name="hero-exclamation-triangle-micro"
class="size-3 shrink-0 text-error"
/>
<span class={[
"span-op shrink-0 font-mono text-xs",
if(s.is_segment, do: "font-semibold text-primary", else: "font-medium")
]}>
{s.op || "span"}
</span>
<span
class="span-desc truncate text-xs text-base-content/70"
title={s.description || s.transaction}
>
{if s.is_segment, do: s.transaction || s.description, else: s.description}
</span>
</span>
<span class="relative h-3" aria-hidden="true">
<span
class={[
"absolute inset-y-0 min-w-[2px] rounded-[4px]",
cond do
failed?(s.status) -> "bg-error"
s.is_segment -> "bg-primary"
true -> "bg-primary/50"
end
]}
style={bar_style(s, @trace.duration_ms)}
/>
</span>
<span class="span-duration text-right text-xs tabular-nums">
{duration(s.duration_ms)}
</span>
</button>
<div
id={"#{dom_id}-detail"}
class="hidden border-t border-base-200 bg-base-200/40 px-4 py-3"
>
<.span_detail span={s} project={@projects[s.project_id]} />
</div>
</li>
</ul>
</div>
<p
:if={@span_count > @max_rows}
id="spans-capped"
class="text-center text-xs text-base-content/60"
>
Showing the first {@max_rows} of {@span_count} spans.
</p>
</section>
</div>
</Layouts.app>
"""
end
attr :span, :map, required: true
attr :project, :any, required: true
defp span_detail(assigns) do
~H"""
<dl class="grid gap-x-6 gap-y-1 text-xs sm:grid-cols-[10rem_minmax(0,1fr)]">
<.kv :if={@span.description} key="description">
<span class="whitespace-pre-wrap break-all font-mono">{@span.description}</span>
</.kv>
<.kv key="status">{@span.status || "—"}</.kv>
<.kv key="span id"><span class="font-mono">{@span.span_id}</span></.kv>
<.kv :if={@span.parent_span_id} key="parent span id">
<span class="font-mono">{@span.parent_span_id}</span>
</.kv>
<.kv :if={@span.transaction} key="transaction">
<span class="break-all font-mono">{@span.transaction}</span>
</.kv>
<.kv :if={@project} key="project">{@project.slug}</.kv>
<.kv key="start">
{Calendar.strftime(@span.start_at, "%H:%M:%S.%f UTC")}
<span class="text-base-content/60">(+{duration(@span.offset_ms)})</span>
</.kv>
<.kv :if={@span.environment} key="environment">{@span.environment}</.kv>
<.kv :if={@span.release} key="release"><span class="font-mono">{@span.release}</span></.kv>
<.kv :if={@span.event_id} key="event id">
<span class="font-mono">{@span.event_id}</span>
</.kv>
<.kv :for={{k, v} <- Enum.sort(@span.attributes || %{})} key={k}>
<span class="whitespace-pre-wrap break-all font-mono">{attr_value(v)}</span>
</.kv>
</dl>
"""
end
attr :key, :string, required: true
slot :inner_block, required: true
defp kv(assigns) do
~H"""
<dt class="truncate font-mono text-base-content/60" title={@key}>{@key}</dt>
<dd class="min-w-0 pb-1 sm:pb-0">{render_slot(@inner_block)}</dd>
"""
end
defp attr_value(v) when is_binary(v), do: v
defp attr_value(v) when is_number(v) or is_boolean(v) or is_nil(v), do: inspect(v)
defp attr_value(v), do: Jason.encode!(v)
defp bar_style(_span, total) when total <= 0, do: "left: 0; width: 100%"
defp bar_style(span, total) do
left = min(span.offset_ms / total * 100, 100)
width = min(span.duration_ms / total * 100, 100 - left)
"left: #{Float.round(left, 3)}%; width: #{Float.round(max(width, 0.0), 3)}%"
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
13.4 KiB · text
History
154a76a