neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
Logs UI: project log explorer with live tail, trace logs section
21e8fe5 · gmorell · 2026-12-02 03:29
Message
{commit_body(@commit)}
Files changed
modified
lib/vigil/logs.ex
+54
−1
@@ -12,7 +12,9 @@ defmodule Vigil.Logs do
| 12 | 12 | spaces: `service:"billing api"`. A leading `-` negates a word or a |
| 13 | 13 | filter. |
| 14 | 14 | * `facets/2`: levels, environments and services seen recently, with |
| 15 | − counts, for filter menus; | |
| 15 | + counts, for filter menus; `volume/3`, lines a minute for a chart; | |
| 16 | + * `trace_issues/2`: the issue a trace's errors went to, for linking a | |
| 17 | + line to its errors; | |
| 16 | 18 | * the live tail: new lines are broadcast on `"logs:<project_id>"` as |
| 17 | 19 | `{:logs, [%LogEvent{}]}` (`subscribe/1`). |
| 18 | 20 | """ |
@@ -192,6 +194,57 @@ defmodule Vigil.Logs do
| 192 | 194 | end |
| 193 | 195 | end |
| 194 | 196 | |
| 197 | + @doc """ | |
| 198 | + Lines a minute matching `filters` over the last `minutes` (60), as | |
| 199 | + `{minute, count}` pairs, oldest first: a pair for every minute (0 when | |
| 200 | + none), the last one the current minute. | |
| 201 | + """ | |
| 202 | + def volume(project_id, filters \\ [], minutes \\ 60) do | |
| 203 | + last = %{DateTime.truncate(DateTime.utc_now(), :second) | second: 0} | |
| 204 | + first = DateTime.add(last, -(minutes - 1) * 60) | |
| 205 | + | |
| 206 | + counts = | |
| 207 | + from(e in LogEvent, | |
| 208 | + where: | |
| 209 | + e.project_id == ^project_id and e.id >= ^Vigil.Partitions.uuid7_floor(first) and | |
| 210 | + e.timestamp >= ^first, | |
| 211 | + group_by: fragment("date_trunc('minute', ?)", e.timestamp), | |
| 212 | + select: {fragment("date_trunc('minute', ?)", e.timestamp), count(e.id)} | |
| 213 | + ) | |
| 214 | + |> apply_filters(filters) | |
| 215 | + |> Repo.all() | |
| 216 | + |> Map.new(fn {minute, n} -> | |
| 217 | + {minute |> DateTime.from_naive!("Etc/UTC") |> DateTime.truncate(:second), n} | |
| 218 | + end) | |
| 219 | + | |
| 220 | + for i <- 0..(minutes - 1) do | |
| 221 | + minute = DateTime.add(first, i * 60) | |
| 222 | + {minute, Map.get(counts, minute, 0)} | |
| 223 | + end | |
| 224 | + end | |
| 225 | + | |
| 226 | + @doc """ | |
| 227 | + For each of `trace_ids`, the project's first issue (not deleted) with an | |
| 228 | + event in that trace: `%{trace_id => %Issue{}}`, leaving out traces with | |
| 229 | + none. | |
| 230 | + """ | |
| 231 | + def trace_issues(_project_id, []), do: %{} | |
| 232 | + | |
| 233 | + def trace_issues(project_id, trace_ids) do | |
| 234 | + ids = trace_ids |> Enum.map(&String.downcase/1) |> Enum.uniq() | |
| 235 | + | |
| 236 | + Repo.all( | |
| 237 | + from e in Vigil.Issues.IssueEvent, | |
| 238 | + join: i in Vigil.Issues.Issue, | |
| 239 | + on: i.id == e.issue_id, | |
| 240 | + where: e.project_id == ^project_id and e.trace_id in ^ids and is_nil(i.deleted_at), | |
| 241 | + distinct: e.trace_id, | |
| 242 | + order_by: [asc: e.trace_id, asc: e.id], | |
| 243 | + select: {e.trace_id, i} | |
| 244 | + ) | |
| 245 | + |> Map.new() | |
| 246 | + end | |
| 247 | + | |
| 195 | 248 | @doc "The logs of a trace across the given (visible) projects, oldest first." |
| 196 | 249 | def for_trace(project_ids, trace_id, limit \\ 500) do |
| 197 | 250 | Repo.all( |
added
lib/vigil_web/components/log_components.ex
+125
−0
@@ -0,0 +1,125 @@
| 1 | +defmodule VigilWeb.LogComponents do | |
| 2 | + @moduledoc """ | |
| 3 | + Pieces of the log pages (`VigilWeb.LogLive.Index`, the trace page's | |
| 4 | + "Logs in this trace"): a level badge, a log line's time (shown in the | |
| 5 | + viewer's local time once connected, the full ISO time on hover), and | |
| 6 | + the search box's syntax help. | |
| 7 | + """ | |
| 8 | + use Phoenix.Component | |
| 9 | + | |
| 10 | + import VigilWeb.CoreComponents, only: [icon: 1] | |
| 11 | + | |
| 12 | + @doc """ | |
| 13 | + A log level, always labelled: trace and debug muted, info blue, warn | |
| 14 | + amber, error and fatal in the error color (fatal solid, with an icon). | |
| 15 | + """ | |
| 16 | + attr :level, :string, required: true | |
| 17 | + attr :class, :any, default: "badge-sm w-14" | |
| 18 | + | |
| 19 | + def level_badge(assigns) do | |
| 20 | + ~H""" | |
| 21 | + <span | |
| 22 | + data-level={@level} | |
| 23 | + class={[ | |
| 24 | + "log-level badge shrink-0 justify-center gap-0.5 font-mono text-[0.65rem] font-semibold uppercase", | |
| 25 | + level_class(@level), | |
| 26 | + @class | |
| 27 | + ]} | |
| 28 | + > | |
| 29 | + <.icon :if={@level == "fatal"} name="hero-fire-micro" class="size-3" />{@level || "—"} | |
| 30 | + </span> | |
| 31 | + """ | |
| 32 | + end | |
| 33 | + | |
| 34 | + defp level_class("fatal"), do: "badge-error" | |
| 35 | + defp level_class("error"), do: "badge-error badge-soft" | |
| 36 | + defp level_class("warn"), do: "badge-warning badge-soft" | |
| 37 | + defp level_class("info"), do: "badge-info badge-soft" | |
| 38 | + defp level_class("debug"), do: "badge-ghost" | |
| 39 | + defp level_class(_), do: "badge-ghost text-base-content/60" | |
| 40 | + | |
| 41 | + @doc """ | |
| 42 | + When a line was logged: `HH:MM:SS.mmm` (with the date when it isn't | |
| 43 | + today), UTC until the browser rewrites it in local time; the full ISO | |
| 44 | + time on hover. | |
| 45 | + """ | |
| 46 | + attr :at, DateTime, required: true | |
| 47 | + attr :id, :string, required: true | |
| 48 | + attr :class, :any, default: nil | |
| 49 | + | |
| 50 | + def log_time(assigns) do | |
| 51 | + ~H""" | |
| 52 | + <time | |
| 53 | + id={@id} | |
| 54 | + datetime={DateTime.to_iso8601(@at)} | |
| 55 | + title={DateTime.to_iso8601(@at)} | |
| 56 | + phx-hook=".LocalTime" | |
| 57 | + phx-update="ignore" | |
| 58 | + class={["log-time shrink-0 font-mono text-xs tabular-nums text-base-content/60", @class]} | |
| 59 | + > | |
| 60 | + {short_time(@at)} | |
| 61 | + </time> | |
| 62 | + <script :type={Phoenix.LiveView.ColocatedHook} name=".LocalTime"> | |
| 63 | + export default { | |
| 64 | + mounted() { | |
| 65 | + const d = new Date(this.el.dateTime) | |
| 66 | + if (isNaN(d)) return | |
| 67 | + const opts = {hour: "2-digit", minute: "2-digit", second: "2-digit", hourCycle: "h23"} | |
| 68 | + if (d.toDateString() !== new Date().toDateString()) { | |
| 69 | + opts.month = "short" | |
| 70 | + opts.day = "numeric" | |
| 71 | + } | |
| 72 | + const ms = String(d.getMilliseconds()).padStart(3, "0") | |
| 73 | + this.el.textContent = `${d.toLocaleString(undefined, opts)}.${ms}` | |
| 74 | + } | |
| 75 | + } | |
| 76 | + </script> | |
| 77 | + """ | |
| 78 | + end | |
| 79 | + | |
| 80 | + @doc "A time as the server shows it before the browser localizes it." | |
| 81 | + def short_time(%DateTime{} = at, today \\ Date.utc_today()) do | |
| 82 | + ms = | |
| 83 | + at.microsecond |> elem(0) |> div(1000) |> Integer.to_string() |> String.pad_leading(3, "0") | |
| 84 | + | |
| 85 | + if DateTime.to_date(at) == today, | |
| 86 | + do: Calendar.strftime(at, "%H:%M:%S.") <> ms, | |
| 87 | + else: Calendar.strftime(at, "%b %-d %H:%M:%S.") <> ms | |
| 88 | + end | |
| 89 | + | |
| 90 | + @doc "The search syntax, briefly, in a disclosure." | |
| 91 | + attr :id, :string, required: true | |
| 92 | + | |
| 93 | + def syntax_help(assigns) do | |
| 94 | + ~H""" | |
| 95 | + <details id={@id} class="group relative"> | |
| 96 | + <summary class="btn btn-ghost btn-sm list-none gap-1 text-base-content/70"> | |
| 97 | + <.icon name="hero-question-mark-circle-micro" class="size-4" /> Syntax | |
| 98 | + </summary> | |
| 99 | + <div class="absolute right-0 z-20 mt-1 w-80 max-w-[calc(100vw-2rem)] rounded-box border border-base-300 bg-base-100 p-4 text-xs shadow-lg"> | |
| 100 | + <p class="mb-2 text-base-content/70"> | |
| 101 | + Words match the message, all of them, ignoring case. Narrow with <code class="font-mono">key:value</code>: | |
| 102 | + </p> | |
| 103 | + <dl class="grid grid-cols-[auto_minmax(0,1fr)] gap-x-3 gap-y-1"> | |
| 104 | + <dt><code class="font-mono">level:warn,error</code></dt> | |
| 105 | + <dd class="text-base-content/70">either level</dd> | |
| 106 | + <dt><code class="font-mono">env:prod</code></dt> | |
| 107 | + <dd class="text-base-content/70">environment</dd> | |
| 108 | + <dt><code class="font-mono">service:billing</code></dt> | |
| 109 | + <dd class="text-base-content/70"> | |
| 110 | + also <code class="font-mono">host:</code>, <code class="font-mono">release:</code> | |
| 111 | + </dd> | |
| 112 | + <dt><code class="font-mono">trace:0af7…</code></dt> | |
| 113 | + <dd class="text-base-content/70">one trace's lines</dd> | |
| 114 | + <dt><code class="font-mono">user.id:42</code></dt> | |
| 115 | + <dd class="text-base-content/70">any attribute</dd> | |
| 116 | + <dt><code class="font-mono">service:"billing api"</code></dt> | |
| 117 | + <dd class="text-base-content/70">quote spaces</dd> | |
| 118 | + <dt><code class="font-mono">-env:dev -timeout</code></dt> | |
| 119 | + <dd class="text-base-content/70">a leading <code class="font-mono">-</code> negates</dd> | |
| 120 | + </dl> | |
| 121 | + </div> | |
| 122 | + </details> | |
| 123 | + """ | |
| 124 | + end | |
| 125 | +end |
modified
lib/vigil_web/live/issue_live/index.ex
+7
−0
@@ -251,6 +251,13 @@ defmodule VigilWeb.IssueLive.Index do
| 251 | 251 | > |
| 252 | 252 | <.icon name="hero-clock-micro" class="size-4" /> Crons |
| 253 | 253 | </.link> |
| 254 | + <.link | |
| 255 | + navigate={~p"/orgs/#{@current_scope.organization.slug}/projects/#{@project.slug}/logs"} | |
| 256 | + class="btn btn-ghost btn-sm" | |
| 257 | + id="project-logs" | |
| 258 | + > | |
| 259 | + <.icon name="hero-document-text-micro" class="size-4" /> Logs | |
| 260 | + </.link> | |
| 254 | 261 | <.link |
| 255 | 262 | :if={Scope.can?(@current_scope, "project:write")} |
| 256 | 263 | navigate={ |
added
lib/vigil_web/live/log_live/index.ex
+697
−0
@@ -0,0 +1,697 @@
| 1 | +defmodule VigilWeb.LogLive.Index do | |
| 2 | + @moduledoc """ | |
| 3 | + A project's log explorer: newest lines first (`Vigil.Logs.search/3`), | |
| 4 | + searched with `Vigil.Logs.parse_query/1`'s syntax. Everything is in the | |
| 5 | + URL's `q`: the level chips, environment and service menus edit its | |
| 6 | + `level:`, `env:` and `service:` terms, and a line's "add as filter" | |
| 7 | + appends `key:value`, so any view can be shared. | |
| 8 | + | |
| 9 | + A row expands, client side, to its attributes, ids and links to its | |
| 10 | + trace and to the issue its trace's errors went to. | |
| 11 | + | |
| 12 | + The live tail: new lines (`Vigil.Logs.subscribe/1`) that match the | |
| 13 | + search are put at the top (a burst at most every half second), keeping | |
| 14 | + at most 500 rows. Paused, they are counted instead; resuming refetches. | |
| 15 | + A per-minute volume chart of the search over the last hour counts them | |
| 16 | + too. | |
| 17 | + "Load older" pages back from the oldest row shown. | |
| 18 | + """ | |
| 19 | + use VigilWeb, :live_view | |
| 20 | + | |
| 21 | + import VigilWeb.LogComponents | |
| 22 | + | |
| 23 | + alias Phoenix.LiveView.JS | |
| 24 | + alias Vigil.Logs | |
| 25 | + alias VigilWeb.ChartComponents | |
| 26 | + | |
| 27 | + @page 100 | |
| 28 | + @cap 500 | |
| 29 | + @flush_ms 500 | |
| 30 | + | |
| 31 | + @impl true | |
| 32 | + def mount(_params, _session, socket) do | |
| 33 | + project = socket.assigns.project | |
| 34 | + if connected?(socket), do: Logs.subscribe(project.id) | |
| 35 | + | |
| 36 | + {:ok, | |
| 37 | + socket | |
| 38 | + |> assign(:page_title, "#{project.name} · Logs") | |
| 39 | + |> assign(:levels, Logs.levels()) | |
| 40 | + |> assign(:facets, Logs.facets(project.id)) | |
| 41 | + |> assign(live?: true, pending: 0, buffer: [], last_flush: nil, flush_scheduled?: false) | |
| 42 | + |> assign(query: "", filters: [], ids: [], more?: false, ever?: false) | |
| 43 | + |> stream_configure(:logs, dom_id: &"log-#{&1.id}") | |
| 44 | + |> stream(:logs, [])} | |
| 45 | + end | |
| 46 | + | |
| 47 | + @impl true | |
| 48 | + def handle_params(params, _uri, socket) do | |
| 49 | + query = String.trim(params["q"] || "") | |
| 50 | + filters = Logs.parse_query(query) | |
| 51 | + | |
| 52 | + {:noreply, | |
| 53 | + socket | |
| 54 | + |> assign(query: query, filters: filters) | |
| 55 | + |> assign(:search_form, to_form(%{"q" => query})) | |
| 56 | + |> assign(:facet_form, facet_form(filters)) | |
| 57 | + |> load()} | |
| 58 | + end | |
| 59 | + | |
| 60 | + defp load(socket) do | |
| 61 | + %{project: project, filters: filters} = socket.assigns | |
| 62 | + logs = Logs.search(project.id, filters, limit: @page) | |
| 63 | + ever? = logs != [] or (filters != [] and Logs.search(project.id, [], limit: 1) != []) | |
| 64 | + | |
| 65 | + socket | |
| 66 | + |> assign(ids: Enum.map(logs, & &1.id), more?: length(logs) == @page, ever?: ever?) | |
| 67 | + |> assign(pending: 0, buffer: []) | |
| 68 | + |> assign(:volume, Logs.volume(project.id, filters)) | |
| 69 | + |> stream(:logs, decorate(project.id, logs), reset: true) | |
| 70 | + end | |
| 71 | + | |
| 72 | + # Each line gets the issue its trace's errors went to, if any. | |
| 73 | + defp decorate(project_id, logs) do | |
| 74 | + issues = | |
| 75 | + Logs.trace_issues(project_id, logs |> Enum.map(& &1.trace_id) |> Enum.reject(&is_nil/1)) | |
| 76 | + | |
| 77 | + Enum.map(logs, &Map.put(&1, :trace_issue, issues[&1.trace_id])) | |
| 78 | + end | |
| 79 | + | |
| 80 | + ## Events | |
| 81 | + | |
| 82 | + @impl true | |
| 83 | + def handle_event("search", %{"q" => q}, socket), do: {:noreply, patch_query(socket, q)} | |
| 84 | + | |
| 85 | + def handle_event("toggle_level", %{"level" => level}, socket) do | |
| 86 | + current = selected(socket.assigns.filters, :level) | |
| 87 | + | |
| 88 | + levels = | |
| 89 | + if level in current, | |
| 90 | + do: List.delete(current, level), | |
| 91 | + else: Enum.filter(Logs.levels(), &(&1 in [level | current])) | |
| 92 | + | |
| 93 | + {:noreply, patch_query(socket, set_term(socket.assigns.query, "level", levels))} | |
| 94 | + end | |
| 95 | + | |
| 96 | + def handle_event("facet", %{"facets" => facets}, socket) do | |
| 97 | + query = | |
| 98 | + socket.assigns.query | |
| 99 | + |> set_term("env", List.wrap(blank_nil(facets["env"])), ~w(env environment)) | |
| 100 | + |> set_term("service", List.wrap(blank_nil(facets["service"]))) | |
| 101 | + | |
| 102 | + {:noreply, patch_query(socket, query)} | |
| 103 | + end | |
| 104 | + | |
| 105 | + def handle_event("add_filter", %{"key" => key, "value" => value}, socket) do | |
| 106 | + term = "#{quote_term(key)}:#{quote_term(value)}" | |
| 107 | + query = socket.assigns.query | |
| 108 | + | |
| 109 | + query = | |
| 110 | + if term in String.split(query), do: query, else: String.trim("#{query} #{term}") | |
| 111 | + | |
| 112 | + {:noreply, patch_query(socket, query)} | |
| 113 | + end | |
| 114 | + | |
| 115 | + def handle_event("toggle_tail", _, %{assigns: %{live?: true}} = socket), | |
| 116 | + do: | |
| 117 | + {:noreply, assign(socket, live?: false, pending: length(socket.assigns.buffer), buffer: [])} | |
| 118 | + | |
| 119 | + def handle_event("toggle_tail", _, socket), do: {:noreply, resume(socket)} | |
| 120 | + | |
| 121 | + def handle_event("resume", _, socket), do: {:noreply, resume(socket)} | |
| 122 | + | |
| 123 | + def handle_event("load_older", _, socket) do | |
| 124 | + %{project: project, filters: filters, ids: ids} = socket.assigns | |
| 125 | + | |
| 126 | + case List.last(ids) do | |
| 127 | + nil -> | |
| 128 | + {:noreply, assign(socket, :more?, false)} | |
| 129 | + | |
| 130 | + oldest -> | |
| 131 | + logs = Logs.search(project.id, filters, limit: @page, before: oldest) | |
| 132 | + | |
| 133 | + {:noreply, | |
| 134 | + socket | |
| 135 | + |> assign(ids: ids ++ Enum.map(logs, & &1.id), more?: length(logs) == @page) | |
| 136 | + |> stream(:logs, decorate(project.id, logs), at: -1)} | |
| 137 | + end | |
| 138 | + end | |
| 139 | + | |
| 140 | + defp resume(socket) do | |
| 141 | + socket | |
| 142 | + |> assign(:live?, true) | |
| 143 | + |> assign(:facets, Logs.facets(socket.assigns.project.id)) | |
| 144 | + |> load() | |
| 145 | + end | |
| 146 | + | |
| 147 | + defp patch_query(socket, q) do | |
| 148 | + %{current_scope: scope, project: project} = socket.assigns | |
| 149 | + push_patch(socket, to: logs_path(scope.organization.slug, project.slug, q)) | |
| 150 | + end | |
| 151 | + | |
| 152 | + @doc false | |
| 153 | + def logs_path(org_slug, project_slug, q) do | |
| 154 | + params = if String.trim(q || "") == "", do: [], else: [q: String.trim(q)] | |
| 155 | + ~p"/orgs/#{org_slug}/projects/#{project_slug}/logs?#{params}" | |
| 156 | + end | |
| 157 | + | |
| 158 | + ## The live tail | |
| 159 | + | |
| 160 | + @impl true | |
| 161 | + def handle_info({:logs, events}, socket) do | |
| 162 | + %{filters: filters, project: project} = socket.assigns | |
| 163 | + matched = Enum.filter(events, &Logs.matches?(&1, filters)) | |
| 164 | + | |
| 165 | + cond do | |
| 166 | + matched == [] -> | |
| 167 | + {:noreply, socket} | |
| 168 | + | |
| 169 | + not socket.assigns.ever? -> | |
| 170 | + {:noreply, socket |> assign(:facets, Logs.facets(project.id)) |> load()} | |
| 171 | + | |
| 172 | + not socket.assigns.live? -> | |
| 173 | + {:noreply, | |
| 174 | + socket | |
| 175 | + |> update(:pending, &(&1 + length(matched))) | |
| 176 | + |> update(:volume, &bump(&1, matched))} | |
| 177 | + | |
| 178 | + true -> | |
| 179 | + {:noreply, socket |> update(:volume, &bump(&1, matched)) |> enqueue(matched)} | |
| 180 | + end | |
| 181 | + end | |
| 182 | + | |
| 183 | + def handle_info(:flush, %{assigns: %{live?: true}} = socket), do: {:noreply, flush(socket)} | |
| 184 | + def handle_info(:flush, socket), do: {:noreply, assign(socket, :flush_scheduled?, false)} | |
| 185 | + | |
| 186 | + # Leading-edge throttle: a batch shows at once unless one did in the | |
| 187 | + # last @flush_ms; then it waits for the rest of that window. | |
| 188 | + defp enqueue(socket, events) do | |
| 189 | + socket = update(socket, :buffer, &Enum.take(&1 ++ events, -@cap)) | |
| 190 | + now = System.monotonic_time(:millisecond) | |
| 191 | + last = socket.assigns.last_flush | |
| 192 | + | |
| 193 | + cond do | |
| 194 | + socket.assigns.flush_scheduled? -> | |
| 195 | + socket | |
| 196 | + | |
| 197 | + is_nil(last) or now - last >= @flush_ms -> | |
| 198 | + flush(socket) | |
| 199 | + | |
| 200 | + true -> | |
| 201 | + Process.send_after(self(), :flush, @flush_ms - (now - last)) | |
| 202 | + assign(socket, :flush_scheduled?, true) | |
| 203 | + end | |
| 204 | + end | |
| 205 | + | |
| 206 | + defp flush(socket) do | |
| 207 | + %{buffer: buffer, ids: ids, project: project} = socket.assigns | |
| 208 | + # Inserted one by one at the top, so oldest first leaves the newest on top. | |
| 209 | + items = Enum.sort_by(buffer, & &1.id) | |
| 210 | + ids = Enum.map(Enum.reverse(items), & &1.id) ++ ids | |
| 211 | + | |
| 212 | + socket | |
| 213 | + |> assign(:ids, Enum.take(ids, @cap)) | |
| 214 | + |> assign(:more?, socket.assigns.more? or length(ids) > @cap) | |
| 215 | + |> assign(buffer: [], flush_scheduled?: false) | |
| 216 | + |> assign(:last_flush, System.monotonic_time(:millisecond)) | |
| 217 | + |> stream(:logs, decorate(project.id, items), at: 0, limit: @cap) | |
| 218 | + end | |
| 219 | + | |
| 220 | + # Counts tailed lines into the chart, sliding it on to a new minute. | |
| 221 | + defp bump(volume, events) do | |
| 222 | + {last, _} = List.last(volume) | |
| 223 | + n = length(volume) | |
| 224 | + counts = Enum.frequencies_by(events, &minute_of(&1.timestamp)) | |
| 225 | + latest = counts |> Map.keys() |> Enum.max(DateTime, fn -> last end) | |
| 226 | + | |
| 227 | + volume = | |
| 228 | + case DateTime.diff(latest, last, :minute) do | |
| 229 | + ahead when ahead > 0 -> | |
| 230 | + Enum.drop(volume, min(ahead, n)) ++ | |
| 231 | + for i <- max(ahead - n + 1, 1)..ahead, do: {DateTime.add(last, i * 60), 0} | |
| 232 | + | |
| 233 | + _ -> | |
| 234 | + volume | |
| 235 | + end | |
| 236 | + | |
| 237 | + Enum.map(volume, fn {minute, c} -> {minute, c + Map.get(counts, minute, 0)} end) | |
| 238 | + end | |
| 239 | + | |
| 240 | + defp minute_of(at), do: %{DateTime.truncate(at, :second) | second: 0} | |
| 241 | + | |
| 242 | + ## The query's terms | |
| 243 | + | |
| 244 | + # The values of the query's (not negated) `key:` term. | |
| 245 | + defp selected(filters, column) do | |
| 246 | + Enum.find_value(filters, [], fn | |
| 247 | + {:column, ^column, values, false} -> values | |
| 248 | + _ -> nil | |
| 249 | + end) | |
| 250 | + end | |
| 251 | + | |
| 252 | + defp facet_form(filters) do | |
| 253 | + one = fn col -> | |
| 254 | + case selected(filters, col) do | |
| 255 | + [value] -> value | |
| 256 | + _ -> "" | |
| 257 | + end | |
| 258 | + end | |
| 259 | + | |
| 260 | + to_form(%{"env" => one.(:environment), "service" => one.(:service)}, as: :facets) | |
| 261 | + end | |
| 262 | + | |
| 263 | + # Replaces the query's (not negated) terms for `aliases` with | |
| 264 | + # `key:values`, or drops them when `values` is empty. | |
| 265 | + @doc false | |
| 266 | + def set_term(query, key, values, aliases \\ nil) do | |
| 267 | + names = (aliases || [key]) |> Enum.map(&Regex.escape/1) |> Enum.join("|") | |
| 268 | + term = ~r/(^|\s+)(?:#{names}):(?:"(?:[^"\\]|\\.)*"|\S+)/i | |
| 269 | + rest = query |> String.replace(term, "") |> String.trim() | |
| 270 | + | |
| 271 | + case values do | |
| 272 | + [] -> | |
| 273 | + rest | |
| 274 | + | |
| 275 | + values -> | |
| 276 | + String.trim("#{rest} #{key}:#{values |> Enum.map("e_term/1) |> Enum.join(",")}") | |
| 277 | + end | |
| 278 | + end | |
| 279 | + | |
| 280 | + @doc false | |
| 281 | + def quote_term(value) do | |
| 282 | + value = to_string(value) | |
| 283 | + | |
| 284 | + if value == "" or Regex.match?(~r/[\s":]/u, value), | |
| 285 | + do: ~s(") <> String.replace(value, ~s("), ~S(\")) <> ~s("), | |
| 286 | + else: value | |
| 287 | + end | |
| 288 | + | |
| 289 | + defp blank_nil(""), do: nil | |
| 290 | + defp blank_nil(v), do: v | |
| 291 | + | |
| 292 | + defp facet_options(values, current) do | |
| 293 | + values = Enum.map(values, fn {v, n} -> {"#{v} (#{n})", v} end) | |
| 294 | + | |
| 295 | + if current in ["", nil] or Enum.any?(values, &(elem(&1, 1) == current)), | |
| 296 | + do: values, | |
| 297 | + else: [{current, current} | values] | |
| 298 | + end | |
| 299 | + | |
| 300 | + defp level_count(facets, level) do | |
| 301 | + case List.keyfind(facets.level, level, 0) do | |
| 302 | + {_, n} -> n | |
| 303 | + nil -> 0 | |
| 304 | + end | |
| 305 | + end | |
| 306 | + | |
| 307 | + ## A line's details | |
| 308 | + | |
| 309 | + @columns [ | |
| 310 | + {:environment, "environment", "env"}, | |
| 311 | + {:release, "release", "release"}, | |
| 312 | + {:service, "service", "service"}, | |
| 313 | + {:host, "host", "host"}, | |
| 314 | + {:trace_id, "trace_id", "trace"}, | |
| 315 | + {:span_id, "span_id", nil} | |
| 316 | + ] | |
| 317 | + | |
| 318 | + # `{label, value, filter key or nil}` for the detail table. | |
| 319 | + defp detail_rows(log) do | |
| 320 | + columns = | |
| 321 | + for {field, label, key} <- @columns, | |
| 322 | + value = Map.get(log, field), | |
| 323 | + value not in [nil, ""], | |
| 324 | + do: {label, value, key} | |
| 325 | + | |
| 326 | + attrs = | |
| 327 | + for {k, v} <- Enum.sort(log.attributes || %{}), | |
| 328 | + do: {k, attr_value(v), if(scalar?(v), do: k)} | |
| 329 | + | |
| 330 | + columns ++ attrs | |
| 331 | + end | |
| 332 | + | |
| 333 | + defp scalar?(v), do: is_binary(v) or is_number(v) or is_boolean(v) | |
| 334 | + | |
| 335 | + defp attr_value(v) when is_binary(v), do: v | |
| 336 | + defp attr_value(v) when is_number(v) or is_boolean(v) or is_nil(v), do: inspect(v) | |
| 337 | + defp attr_value(v), do: Jason.encode!(v) | |
| 338 | + | |
| 339 | + defp volume_points(volume) do | |
| 340 | + for {minute, n} <- volume do | |
| 341 | + %{ | |
| 342 | + value: n, | |
| 343 | + tip: "lines at #{Calendar.strftime(minute, "%H:%M")} UTC", | |
| 344 | + axis: Calendar.strftime(minute, "%H:%M"), | |
| 345 | + row: Calendar.strftime(minute, "%Y-%m-%d %H:%M") | |
| 346 | + } | |
| 347 | + end | |
| 348 | + end | |
| 349 | + | |
| 350 | + defp toggle_row(dom_id) do | |
| 351 | + JS.toggle(to: "##{dom_id}-detail") | |
| 352 | + |> JS.toggle_class("rotate-90", to: "##{dom_id}-chev") | |
| 353 | + |> JS.toggle_attribute({"aria-expanded", "true", "false"}, to: "##{dom_id}-row") | |
| 354 | + end | |
| 355 | + | |
| 356 | + @impl true | |
| 357 | + def render(assigns) do | |
| 358 | + ~H""" | |
| 359 | + <Layouts.app flash={@flash} current_scope={@current_scope} active={:projects}> | |
| 360 | + <div class="mx-auto max-w-6xl space-y-5"> | |
| 361 | + <.header> | |
| 362 | + {@project.name} logs | |
| 363 | + <:subtitle>Structured log lines from your SDKs, newest first, as they arrive.</:subtitle> | |
| 364 | + <:actions> | |
| 365 | + <div class="flex items-center gap-2"> | |
| 366 | + <button | |
| 367 | + :if={@ever?} | |
| 368 | + type="button" | |
| 369 | + id="tail-toggle" | |
| 370 | + phx-click="toggle_tail" | |
| 371 | + aria-pressed={to_string(@live?)} | |
| 372 | + data-live={to_string(@live?)} | |
| 373 | + class={[ | |
| 374 | + "btn btn-sm gap-2 transition", | |
| 375 | + if(@live?, do: "btn-soft btn-success", else: "btn-ghost border-base-300") | |
| 376 | + ]} | |
| 377 | + > | |
| 378 | + <span class="relative flex size-2" aria-hidden="true"> | |
| 379 | + <span | |
| 380 | + :if={@live?} | |
| 381 | + class="absolute inline-flex size-full animate-ping rounded-full bg-success opacity-60" | |
| 382 | + /> | |
| 383 | + <span class={[ | |
| 384 | + "relative inline-flex size-2 rounded-full", | |
| 385 | + if(@live?, do: "bg-success", else: "bg-base-content/40") | |
| 386 | + ]} /> | |
| 387 | + </span> | |
| 388 | + {if @live?, do: "Live", else: "Paused"} | |
| 389 | + </button> | |
| 390 | + <.link | |
| 391 | + navigate={~p"/orgs/#{@current_scope.organization.slug}/projects/#{@project.slug}"} | |
| 392 | + class="btn btn-ghost btn-sm" | |
| 393 | + id="logs-back" | |
| 394 | + > | |
| 395 | + <.icon name="hero-arrow-left-micro" class="size-4" /> Issues | |
| 396 | + </.link> | |
| 397 | + </div> | |
| 398 | + </:actions> | |
| 399 | + </.header> | |
| 400 | + | |
| 401 | + <%= if @ever? do %> | |
| 402 | + <div class="space-y-3"> | |
| 403 | + <div class="flex flex-wrap items-center gap-2"> | |
| 404 | + <.form | |
| 405 | + for={@search_form} | |
| 406 | + id="log-search" | |
| 407 | + phx-submit="search" | |
| 408 | + class="min-w-0 flex-1 basis-72" | |
| 409 | + > | |
| 410 | + <label class="input input-sm w-full font-mono"> | |
| 411 | + <.icon name="hero-magnifying-glass-micro" class="size-4 opacity-50" /> | |
| 412 | + <input | |
| 413 | + type="search" | |
| 414 | + name="q" | |
| 415 | + id="log-search-q" | |
| 416 | + value={@query} | |
| 417 | + placeholder="timeout level:error env:prod user.id:42" | |
| 418 | + autocomplete="off" | |
| 419 | + spellcheck="false" | |
| 420 | + /> | |
| 421 | + <kbd class="kbd kbd-xs hidden sm:inline-flex">⏎</kbd> | |
| 422 | + </label> | |
| 423 | + </.form> | |
| 424 | + <.syntax_help id="log-syntax" /> | |
| 425 | + </div> | |
| 426 | + | |
| 427 | + <div class="flex flex-wrap items-center gap-x-4 gap-y-2"> | |
| 428 | + <div | |
| 429 | + id="level-chips" | |
| 430 | + class="flex flex-wrap items-center gap-1" | |
| 431 | + role="group" | |
| 432 | + aria-label="Levels" | |
| 433 | + > | |
| 434 | + <button | |
| 435 | + :for={level <- @levels} | |
| 436 | + type="button" | |
| 437 | + id={"level-#{level}"} | |
| 438 | + phx-click="toggle_level" | |
| 439 | + phx-value-level={level} | |
| 440 | + aria-pressed={to_string(level in selected(@filters, :level))} | |
| 441 | + class={[ | |
| 442 | + "flex items-center gap-1 rounded-full border px-1 py-0.5 text-xs transition hover:border-base-content/30", | |
| 443 | + if(level in selected(@filters, :level), | |
| 444 | + do: "border-primary bg-primary/10 ring-1 ring-primary/40", | |
| 445 | + else: "border-base-300 bg-base-100" | |
| 446 | + ) | |
| 447 | + ]} | |
| 448 | + > | |
| 449 | + <.level_badge level={level} class="badge-xs px-1.5" /> | |
| 450 | + <span class="pr-1 tabular-nums text-base-content/60"> | |
| 451 | + {level_count(@facets, level)} | |
| 452 | + </span> | |
| 453 | + </button> | |
| 454 | + </div> | |
| 455 | + | |
| 456 | + <.form | |
| 457 | + for={@facet_form} | |
| 458 | + id="log-facets" | |
| 459 | + phx-change="facet" | |
| 460 | + class="flex flex-wrap items-center gap-2 [&_.fieldset]:mb-0" | |
| 461 | + > | |
| 462 | + <.input | |
| 463 | + type="select" | |
| 464 | + field={@facet_form[:env]} | |
| 465 | + prompt="All environments" | |
| 466 | + options={facet_options(@facets.environment, @facet_form[:env].value)} | |
| 467 | + class="select select-sm w-44" | |
| 468 | + aria-label="Environment" | |
| 469 | + /> | |
| 470 | + <.input | |
| 471 | + type="select" | |
| 472 | + field={@facet_form[:service]} | |
| 473 | + prompt="All services" | |
| 474 | + options={facet_options(@facets.service, @facet_form[:service].value)} | |
| 475 | + class="select select-sm w-44" | |
| 476 | + aria-label="Service" | |
| 477 | + /> | |
| 478 | + </.form> | |
| 479 | + </div> | |
| 480 | + </div> | |
| 481 | + | |
| 482 | + <section | |
| 483 | + id="log-volume-section" | |
| 484 | + class="rounded-box border border-base-300 bg-base-100 px-4 pt-3 pb-2" | |
| 485 | + aria-labelledby="log-volume-title" | |
| 486 | + > | |
| 487 | + <h2 id="log-volume-title" class="text-xs font-medium text-base-content/60"> | |
| 488 | + Lines a minute, last hour{if @query != "", do: ", matching the search"} | |
| 489 | + </h2> | |
| 490 | + <ChartComponents.bars | |
| 491 | + id="log-volume" | |
| 492 | + points={volume_points(@volume)} | |
| 493 | + label="Log lines a minute over the last hour" | |
| 494 | + key_header="Minute (UTC)" | |
| 495 | + value_header="Lines" | |
| 496 | + class="h-14" | |
| 497 | + /> | |
| 498 | + </section> | |
| 499 | + | |
| 500 | + <button | |
| 501 | + :if={not @live? and @pending > 0} | |
| 502 | + type="button" | |
| 503 | + id="tail-pending" | |
| 504 | + phx-click="resume" | |
| 505 | + class="flex w-full items-center justify-center gap-2 rounded-box border border-primary/30 bg-primary/5 py-2 text-sm text-primary transition hover:bg-primary/10" | |
| 506 | + > | |
| 507 | + <.icon name="hero-arrow-up-micro" class="size-4" /> | |
| 508 | + {@pending} new {if @pending == 1, do: "line", else: "lines"} — show | |
| 509 | + </button> | |
| 510 | + | |
| 511 | + <div class="overflow-hidden rounded-box border border-base-300 bg-base-100"> | |
| 512 | + <ul id="logs" phx-update="stream" class="divide-y divide-base-200 font-mono text-xs"> | |
| 513 | + <li | |
| 514 | + id="logs-empty" | |
| 515 | + class="hidden px-4 py-12 text-center font-sans text-sm text-base-content/60 only:block" | |
| 516 | + > | |
| 517 | + <.icon name="hero-funnel" class="mx-auto mb-2 block size-8 opacity-40" /> | |
| 518 | + No line matches <code class="font-mono">{@query}</code>. | |
| 519 | + <.link | |
| 520 | + patch={logs_path(@current_scope.organization.slug, @project.slug, "")} | |
| 521 | + class="link link-primary" | |
| 522 | + id="logs-clear" | |
| 523 | + > | |
| 524 | + Clear the search | |
| 525 | + </.link> | |
| 526 | + </li> | |
| 527 | + <li :for={{dom_id, log} <- @streams.logs} id={dom_id} data-level={log.level}> | |
| 528 | + <div | |
| 529 | + id={"#{dom_id}-row"} | |
| 530 | + role="button" | |
| 531 | + tabindex="0" | |
| 532 | + aria-expanded="false" | |
| 533 | + aria-controls={"#{dom_id}-detail"} | |
| 534 | + phx-click={toggle_row(dom_id)} | |
| 535 | + class="group flex cursor-pointer items-center gap-3 px-3 py-1.5 transition hover:bg-base-200/60" | |
| 536 | + > | |
| 537 | + <span id={"#{dom_id}-chev"} class="shrink-0 transition-transform"> | |
| 538 | + <.icon name="hero-chevron-right-micro" class="size-3 opacity-40" /> | |
| 539 | + </span> | |
| 540 | + <.log_time id={"#{dom_id}-time"} at={log.timestamp} class="w-28" /> | |
| 541 | + <.level_badge level={log.level} /> | |
| 542 | + <span | |
| 543 | + :if={log.service || log.host} | |
| 544 | + class="log-source hidden w-32 shrink-0 truncate text-base-content/60 sm:inline" | |
| 545 | + title={Enum.join(Enum.reject([log.service, log.host], &is_nil/1), " · ")} | |
| 546 | + > | |
| 547 | + {log.service || log.host} | |
| 548 | + </span> | |
| 549 | + <span class={[ | |
| 550 | + "log-body min-w-0 flex-1 truncate", | |
| 551 | + log.level in ["error", "fatal"] && "text-error" | |
| 552 | + ]}> | |
| 553 | + {log.body} | |
| 554 | + </span> | |
| 555 | + <.icon | |
| 556 | + :if={log.trace_id} | |
| 557 | + name="hero-link-micro" | |
| 558 | + class="size-3 shrink-0 opacity-30" | |
| 559 | + /> | |
| 560 | + </div> | |
| 561 | + <div | |
| 562 | + id={"#{dom_id}-detail"} | |
| 563 | + class="hidden space-y-3 border-t border-base-200 bg-base-200/40 px-4 py-3 font-sans" | |
| 564 | + > | |
| 565 | + <pre class="whitespace-pre-wrap break-words font-mono text-xs">{log.body}</pre> | |
| 566 | + <div :if={log.trace_id} class="flex flex-wrap gap-2"> | |
| 567 | + <.link | |
| 568 | + navigate={~p"/orgs/#{@current_scope.organization.slug}/traces/#{log.trace_id}"} | |
| 569 | + id={"#{dom_id}-trace"} | |
| 570 | + class="btn btn-xs btn-soft btn-primary" | |
| 571 | + > | |
| 572 | + <.icon name="hero-queue-list-micro" class="size-3.5" /> View trace | |
| 573 | + </.link> | |
| 574 | + <.link | |
| 575 | + :if={log.trace_issue} | |
| 576 | + navigate={ | |
| 577 | + ~p"/orgs/#{@current_scope.organization.slug}/projects/#{@project.slug}/issues/#{log.trace_issue.short_id}" | |
| 578 | + } | |
| 579 | + id={"#{dom_id}-issue"} | |
| 580 | + class="btn btn-xs btn-soft btn-error" | |
| 581 | + title={log.trace_issue.title} | |
| 582 | + > | |
| 583 | + <.icon name="hero-exclamation-triangle-micro" class="size-3.5" /> | |
| 584 | + Errors in this trace | |
| 585 | + </.link> | |
| 586 | + </div> | |
| 587 | + <table class="w-full text-xs"> | |
| 588 | + <tbody> | |
| 589 | + <tr | |
| 590 | + :for={{{label, value, key}, i} <- Enum.with_index(detail_rows(log))} | |
| 591 | + class="group/attr align-top" | |
| 592 | + > | |
| 593 | + <th | |
| 594 | + scope="row" | |
| 595 | + class="w-1/4 max-w-48 truncate py-0.5 pr-4 text-left font-mono font-normal text-base-content/60" | |
| 596 | + title={label} | |
| 597 | + > | |
| 598 | + {label} | |
| 599 | + </th> | |
| 600 | + <td class="py-0.5 font-mono break-all whitespace-pre-wrap">{value}</td> | |
| 601 | + <td class="w-8 py-0.5 text-right"> | |
| 602 | + <button | |
| 603 | + :if={key} | |
| 604 | + type="button" | |
| 605 | + id={"#{dom_id}-filter-#{i}"} | |
| 606 | + phx-click="add_filter" | |
| 607 | + phx-value-key={key} | |
| 608 | + phx-value-value={value} | |
| 609 | + title={"Filter by #{key}:#{value}"} | |
| 610 | + aria-label={"Add #{label} as a filter"} | |
| 611 | + class="btn btn-ghost btn-xs btn-square opacity-40 transition group-hover/attr:opacity-100 focus:opacity-100" | |
| 612 | + > | |
| 613 | + <.icon name="hero-funnel-micro" class="size-3.5" /> | |
| 614 | + </button> | |
| 615 | + </td> | |
| 616 | + </tr> | |
| 617 | + </tbody> | |
| 618 | + </table> | |
| 619 | + </div> | |
| 620 | + </li> | |
| 621 | + </ul> | |
| 622 | + </div> | |
| 623 | + | |
| 624 | + <div :if={@more?} class="flex justify-center"> | |
| 625 | + <button | |
| 626 | + type="button" | |
| 627 | + id="load-older" | |
| 628 | + phx-click="load_older" | |
| 629 | + phx-disable-with="Loading…" | |
| 630 | + class="btn btn-ghost btn-sm" | |
| 631 | + > | |
| 632 | + <.icon name="hero-arrow-down-micro" class="size-4" /> Load older | |
| 633 | + </button> | |
| 634 | + </div> | |
| 635 | + <% else %> | |
| 636 | + <.setup_logs /> | |
| 637 | + <% end %> | |
| 638 | + </div> | |
| 639 | + </Layouts.app> | |
| 640 | + """ | |
| 641 | + end | |
| 642 | + | |
| 643 | + defp setup_logs(assigns) do | |
| 644 | + ~H""" | |
| 645 | + <section id="logs-setup" class="rounded-box border border-dashed border-base-300 bg-base-100 p-8"> | |
| 646 | + <div class="mx-auto max-w-2xl space-y-5"> | |
| 647 | + <div class="text-center"> | |
| 648 | + <.icon name="hero-document-text" class="mx-auto mb-2 block size-10 text-primary/60" /> | |
| 649 | + <h2 class="text-lg font-semibold">No logs yet</h2> | |
| 650 | + <p class="mt-1 text-sm text-base-content/70"> | |
| 651 | + Turn on logs in your SDK and its log lines show up here as they happen, searchable, | |
| 652 | + with their attributes and a link to the trace they were logged in. | |
| 653 | + </p> | |
| 654 | + </div> | |
| 655 | + | |
| 656 | + <div class="space-y-2"> | |
| 657 | + <h3 class="text-sm font-semibold">sentry-elixir</h3> | |
| 658 | + <p class="text-sm text-base-content/70"> | |
| 659 | + The SDK attaches its logger handler and sends lines at or above the level: | |
| 660 | + </p> | |
| 661 | + <pre | |
| 662 | + id="logs-setup-elixir" | |
| 663 | + class="overflow-x-auto rounded-box bg-base-200 p-3 text-xs" | |
| 664 | + phx-no-curly-interpolation | |
| 665 | + ><code># config/config.exs | |
| 666 | + config :sentry, | |
| 667 | + dsn: "…", | |
| 668 | + logs: [level: :info]</code></pre> | |
| 669 | + </div> | |
| 670 | + | |
| 671 | + <div class="space-y-2"> | |
| 672 | + <h3 class="text-sm font-semibold">sentry-python</h3> | |
| 673 | + <p class="text-sm text-base-content/70"> | |
| 674 | + Then log with <code class="font-mono">sentry_sdk.logger</code>, or through the standard | |
| 675 | + <code class="font-mono">logging</code> | |
| 676 | + module (the logging integration forwards it). | |
| 677 | + </p> | |
| 678 | + <pre | |
| 679 | + id="logs-setup-python" | |
| 680 | + class="overflow-x-auto rounded-box bg-base-200 p-3 text-xs" | |
| 681 | + phx-no-curly-interpolation | |
| 682 | + ><code>sentry_sdk.init( | |
| 683 | + dsn="…", | |
| 684 | + enable_logs=True, | |
| 685 | + ) | |
| 686 | + | |
| 687 | + sentry_sdk.logger.info("order {order_id} placed", order_id=42)</code></pre> | |
| 688 | + </div> | |
| 689 | + | |
| 690 | + <p class="text-xs text-base-content/60"> | |
| 691 | + This page is live: the first line that arrives replaces this guide. | |
| 692 | + </p> | |
| 693 | + </div> | |
| 694 | + </section> | |
| 695 | + """ | |
| 696 | + end | |
| 697 | +end |
modified
lib/vigil_web/live/trace_live/show.ex
+81
−6
@@ -5,16 +5,19 @@ defmodule VigilWeb.TraceLive.Show do
| 5 | 5 | indented by depth, its bar placed by its offset and duration within the |
| 6 | 6 | trace, transactions (segment spans) highlighted; a row expands, client |
| 7 | 7 | side, to show the span's status, ids and attributes. Below it, the |
| 8 | − errors that happened in the trace, each linking to its event. | |
| 8 | + errors that happened in the trace, each linking to its event. Last, its | |
| 9 | + log lines (`Vigil.Logs.for_trace/2`), each linking to its project's logs | |
| 10 | + searched for the trace; shown even when no spans or errors are kept. | |
| 9 | 11 | |
| 10 | 12 | At most 1000 spans are drawn; the rest are counted. |
| 11 | 13 | """ |
| 12 | 14 | use VigilWeb, :live_view |
| 13 | 15 | |
| 14 | − import VigilWeb.PerformanceComponents | |
| 16 | + import VigilWeb.{LogComponents, PerformanceComponents} | |
| 15 | 17 | |
| 16 | 18 | alias Phoenix.LiveView.JS |
| 17 | − alias Vigil.{Performance, Projects} | |
| 19 | + alias Vigil.{Logs, Performance, Projects} | |
| 20 | + alias VigilWeb.LogLive | |
| 18 | 21 | |
| 19 | 22 | @max_rows 1000 |
| 20 | 23 |
@@ -22,7 +25,10 @@ defmodule VigilWeb.TraceLive.Show do
| 22 | 25 | def mount(%{"trace_id" => trace_id}, _session, socket) do |
| 23 | 26 | scope = socket.assigns.current_scope |
| 24 | 27 | projects = Projects.list_visible(scope) |
| 25 | − trace = Performance.get_trace(Enum.map(projects, & &1.id), trace_id) | |
| 28 | + project_ids = Enum.map(projects, & &1.id) | |
| 29 | + trace = Performance.get_trace(project_ids, trace_id) | |
| 30 | + logs = Logs.for_trace(project_ids, trace_id) | |
| 31 | + by_id = Map.new(projects, &{&1.id, &1}) | |
| 26 | 32 | |
| 27 | 33 | socket = |
| 28 | 34 | socket |
@@ -30,6 +36,10 @@ defmodule VigilWeb.TraceLive.Show do
| 30 | 36 | |> assign(:trace_id, trace_id) |
| 31 | 37 | |> assign(:max_rows, @max_rows) |
| 32 | 38 | |> assign(:trace, trace && Map.delete(trace, :spans)) |
| 39 | + |> assign(:projects, by_id) | |
| 40 | + |> assign(:log_count, length(logs)) | |
| 41 | + |> stream_configure(:trace_logs, dom_id: &"trace-log-#{&1.id}") | |
| 42 | + |> stream(:trace_logs, logs) | |
| 33 | 43 | |
| 34 | 44 | socket = |
| 35 | 45 | case trace do |
@@ -37,12 +47,10 @@ defmodule VigilWeb.TraceLive.Show do
| 37 | 47 | socket |
| 38 | 48 | |
| 39 | 49 | trace -> |
| 40 | − by_id = Map.new(projects, &{&1.id, &1}) | |
| 41 | 50 | span_projects = trace.spans |> Enum.map(& &1.project_id) |> Enum.uniq() |
| 42 | 51 | |
| 43 | 52 | socket |
| 44 | 53 | |> assign(:span_count, length(trace.spans)) |
| 45 | − |> assign(:projects, by_id) | |
| 46 | 54 | |> assign(:span_projects, Enum.map(span_projects, &by_id[&1])) |
| 47 | 55 | |> assign(:segment_count, Enum.count(trace.spans, & &1.is_segment)) |
| 48 | 56 | |> stream_configure(:spans, dom_id: &"span-#{&1.id}") |
@@ -69,6 +77,7 @@ defmodule VigilWeb.TraceLive.Show do
| 69 | 77 | <p class="mt-1 text-sm text-base-content/70"> |
| 70 | 78 | No spans or errors of trace <code class="break-all font-mono">{@trace_id}</code> |
| 71 | 79 | are kept in a project you can see. It may have aged out of retention, or was never sampled. |
| 80 | + <span :if={@log_count > 0}>Its log lines are below.</span> | |
| 72 | 81 | </p> |
| 73 | 82 | <.link |
| 74 | 83 | navigate={~p"/orgs/#{@current_scope.organization.slug}"} |
@@ -78,6 +87,15 @@ defmodule VigilWeb.TraceLive.Show do
| 78 | 87 | <.icon name="hero-arrow-left-micro" class="size-4" /> {@current_scope.organization.name} |
| 79 | 88 | </.link> |
| 80 | 89 | </div> |
| 90 | + <div :if={@log_count > 0} class="mx-auto mt-6 max-w-6xl"> | |
| 91 | + <.trace_logs | |
| 92 | + logs={@streams.trace_logs} | |
| 93 | + count={@log_count} | |
| 94 | + projects={@projects} | |
| 95 | + trace_id={@trace_id} | |
| 96 | + org_slug={@current_scope.organization.slug} | |
| 97 | + /> | |
| 98 | + </div> | |
| 81 | 99 | </Layouts.app> |
| 82 | 100 | """ |
| 83 | 101 | end |
@@ -269,11 +287,68 @@ defmodule VigilWeb.TraceLive.Show do
| 269 | 287 | Showing the first {@max_rows} of {@span_count} spans. |
| 270 | 288 | </p> |
| 271 | 289 | </section> |
| 290 | + | |
| 291 | + <.trace_logs | |
| 292 | + :if={@log_count > 0} | |
| 293 | + logs={@streams.trace_logs} | |
| 294 | + count={@log_count} | |
| 295 | + projects={@projects} | |
| 296 | + trace_id={@trace.trace_id} | |
| 297 | + org_slug={@current_scope.organization.slug} | |
| 298 | + /> | |
| 272 | 299 | </div> |
| 273 | 300 | </Layouts.app> |
| 274 | 301 | """ |
| 275 | 302 | end |
| 276 | 303 | |
| 304 | + attr :logs, :any, required: true | |
| 305 | + attr :count, :integer, required: true | |
| 306 | + attr :projects, :map, required: true | |
| 307 | + attr :trace_id, :string, required: true | |
| 308 | + attr :org_slug, :string, required: true | |
| 309 | + | |
| 310 | + defp trace_logs(assigns) do | |
| 311 | + ~H""" | |
| 312 | + <section id="trace-logs-section" class="space-y-2" aria-labelledby="trace-logs-title"> | |
| 313 | + <h2 id="trace-logs-title" class="flex items-center gap-1.5 text-sm font-semibold"> | |
| 314 | + <.icon name="hero-document-text-micro" class="size-4 text-base-content/60" /> | |
| 315 | + Logs in this trace <span class="badge badge-ghost badge-sm tabular-nums">{@count}</span> | |
| 316 | + </h2> | |
| 317 | + <ul | |
| 318 | + id="trace-logs" | |
| 319 | + phx-update="stream" | |
| 320 | + class="divide-y divide-base-200 overflow-hidden rounded-box border border-base-300 bg-base-100 font-mono text-xs" | |
| 321 | + > | |
| 322 | + <li :for={{dom_id, log} <- @logs} id={dom_id} data-level={log.level}> | |
| 323 | + <.link | |
| 324 | + :if={@projects[log.project_id]} | |
| 325 | + navigate={ | |
| 326 | + LogLive.Index.logs_path(@org_slug, @projects[log.project_id].slug, "trace:#{@trace_id}") | |
| 327 | + } | |
| 328 | + class="group flex items-center gap-3 px-3 py-1.5 transition hover:bg-base-200/60" | |
| 329 | + > | |
| 330 | + <.log_time id={"#{dom_id}-time"} at={log.timestamp} class="w-28" /> | |
| 331 | + <.level_badge level={log.level} /> | |
| 332 | + <span class="hidden w-24 shrink-0 truncate text-base-content/60 sm:inline"> | |
| 333 | + {@projects[log.project_id].slug} | |
| 334 | + </span> | |
| 335 | + <span class={[ | |
| 336 | + "min-w-0 flex-1 truncate group-hover:text-primary", | |
| 337 | + log.level in ["error", "fatal"] && "text-error" | |
| 338 | + ]}> | |
| 339 | + {log.body} | |
| 340 | + </span> | |
| 341 | + <.icon | |
| 342 | + name="hero-chevron-right-micro" | |
| 343 | + class="size-4 shrink-0 opacity-40 transition-transform group-hover:translate-x-0.5" | |
| 344 | + /> | |
| 345 | + </.link> | |
| 346 | + </li> | |
| 347 | + </ul> | |
| 348 | + </section> | |
| 349 | + """ | |
| 350 | + end | |
| 351 | + | |
| 277 | 352 | attr :span, :map, required: true |
| 278 | 353 | attr :project, :any, required: true |
| 279 | 354 |
modified
lib/vigil_web/router.ex
+1
−0
@@ -148,6 +148,7 @@ defmodule VigilWeb.Router do
| 148 | 148 | live "/orgs/:org/projects/:project/performance/:group_id", PerformanceLive.Show, :show |
| 149 | 149 | live "/orgs/:org/projects/:project/monitors", MonitorLive.Index, :index |
| 150 | 150 | live "/orgs/:org/projects/:project/monitors/:slug", MonitorLive.Show, :show |
| 151 | + live "/orgs/:org/projects/:project/logs", LogLive.Index, :index | |
| 151 | 152 | live "/orgs/:org/projects/:project/issues/:short_id", IssueLive.Show, :show |
| 152 | 153 | |
| 153 | 154 | live "/orgs/:org/projects/:project/issues/:short_id/events/:event_id", |
added
test/support/fixtures/logs_fixtures.ex
+42
−0
@@ -0,0 +1,42 @@
| 1 | +defmodule Vigil.LogsFixtures do | |
| 2 | + @moduledoc """ | |
| 3 | + Log lines sent through the real ingest pipeline (`Ingest.accept` then | |
| 4 | + `Drain.drain`), so the processor stores and broadcasts them. | |
| 5 | + """ | |
| 6 | + | |
| 7 | + import Vigil.PerformanceFixtures, only: [send_item: 3] | |
| 8 | + | |
| 9 | + @doc """ | |
| 10 | + A sentry-elixir style line, for `send_logs/2`. Options: `level` | |
| 11 | + ("info"), `env` ("prod"), `service`, `host` ("web-1"), `trace`, `attrs` | |
| 12 | + (extra attributes, plain values), `at` (a DateTime, now). | |
| 13 | + """ | |
| 14 | + def log_line(body, opts \\ []) do | |
| 15 | + at = Keyword.get(opts, :at, DateTime.utc_now()) | |
| 16 | + | |
| 17 | + attrs = | |
| 18 | + %{ | |
| 19 | + "sentry.environment" => opts |> Keyword.get(:env, "prod"), | |
| 20 | + "sentry.release" => "shop@1.2", | |
| 21 | + "sentry.address" => Keyword.get(opts, :host, "web-1") | |
| 22 | + } | |
| 23 | + |> Map.merge(if s = opts[:service], do: %{"service.name" => s}, else: %{}) | |
| 24 | + |> Map.merge(Keyword.get(opts, :attrs, %{})) | |
| 25 | + |> Map.new(fn {k, v} -> {k, typed(v)} end) | |
| 26 | + | |
| 27 | + %{ | |
| 28 | + "timestamp" => DateTime.to_unix(at, :microsecond) / 1_000_000, | |
| 29 | + "level" => Keyword.get(opts, :level, "info"), | |
| 30 | + "body" => body, | |
| 31 | + "trace_id" => opts[:trace], | |
| 32 | + "span_id" => nil, | |
| 33 | + "attributes" => attrs | |
| 34 | + } | |
| 35 | + end | |
| 36 | + | |
| 37 | + @doc "Sends lines (`log_line/2` maps) in one `log` item and drains." | |
| 38 | + def send_logs(project, lines), do: send_item(project, "log", %{"items" => lines}) | |
| 39 | + | |
| 40 | + defp typed(v) when is_integer(v), do: %{"value" => v, "type" => "integer"} | |
| 41 | + defp typed(v), do: %{"value" => v, "type" => "string"} | |
| 42 | +end |
modified
test/vigil/logs_test.exs
+38
−0
@@ -150,4 +150,42 @@ defmodule Vigil.LogsTest do
| 150 | 150 | ~s(timeout level:warning,error service:"billing api" -http.route:/orders "two words") |
| 151 | 151 | ) |
| 152 | 152 | end |
| 153 | + | |
| 154 | + test "volume: lines a minute over the last hour, filtered", ctx do | |
| 155 | + ten_ago = now() - 600 | |
| 156 | + | |
| 157 | + send_logs(ctx.target, %{ | |
| 158 | + "items" => [ | |
| 159 | + elixir_log("now a", "info"), | |
| 160 | + elixir_log("now b", "error"), | |
| 161 | + elixir_log("earlier", "error") |> Map.put("timestamp", ten_ago), | |
| 162 | + elixir_log("too old", "error") |> Map.put("timestamp", now() - 7200) | |
| 163 | + ] | |
| 164 | + }) | |
| 165 | + | |
| 166 | + volume = Logs.volume(ctx.project.id) | |
| 167 | + assert length(volume) == 60 | |
| 168 | + {last, _} = List.last(volume) | |
| 169 | + # (now a and b may straddle a minute) | |
| 170 | + assert volume |> Enum.take(-2) |> Enum.map(&elem(&1, 1)) |> Enum.sum() == 2 | |
| 171 | + assert last.second == 0 and DateTime.diff(DateTime.utc_now(), last) < 60 | |
| 172 | + assert Enum.sum(Enum.map(volume, &elem(&1, 1))) == 3 | |
| 173 | + | |
| 174 | + errors = Logs.volume(ctx.project.id, Logs.parse_query("level:error")) | |
| 175 | + assert errors |> Enum.take(-2) |> Enum.map(&elem(&1, 1)) |> Enum.sum() == 1 | |
| 176 | + assert Enum.sum(Enum.map(errors, &elem(&1, 1))) == 2 | |
| 177 | + assert length(Logs.volume(ctx.project.id, [], 15)) == 15 | |
| 178 | + end | |
| 179 | + | |
| 180 | + test "trace_issues: the issue a trace's errors went to", ctx do | |
| 181 | + Vigil.PerformanceFixtures.trace_error_fixture(ctx.project, @trace, "kaboom") | |
| 182 | + other = Vigil.PerformanceFixtures.trace_id() | |
| 183 | + | |
| 184 | + assert %{@trace => %Vigil.Issues.Issue{title: title}} = | |
| 185 | + issues = Logs.trace_issues(ctx.project.id, [String.upcase(@trace), other]) | |
| 186 | + | |
| 187 | + assert title =~ "kaboom" | |
| 188 | + refute Map.has_key?(issues, other) | |
| 189 | + assert Logs.trace_issues(ctx.project.id, []) == %{} | |
| 190 | + end | |
| 153 | 191 | end |
added
test/vigil_web/live/log_live_test.exs
+282
−0
@@ -0,0 +1,282 @@
| 1 | +defmodule VigilWeb.LogLiveTest do | |
| 2 | + use VigilWeb.ConnCase, async: true | |
| 3 | + | |
| 4 | + import Phoenix.LiveViewTest | |
| 5 | + import Vigil.AccountsFixtures | |
| 6 | + import Vigil.LogsFixtures | |
| 7 | + import Vigil.OrganizationsFixtures | |
| 8 | + import Vigil.PerformanceFixtures | |
| 9 | + | |
| 10 | + alias Vigil.Logs | |
| 11 | + alias VigilWeb.LogLive | |
| 12 | + | |
| 13 | + setup :register_and_log_in_user | |
| 14 | + | |
| 15 | + setup %{user: user} do | |
| 16 | + org = organization_fixture(user) | |
| 17 | + project = project_fixture(org_scope(user, org), %{"name" => "web"}) | |
| 18 | + base = ~p"/orgs/#{org.slug}/projects/web/logs" | |
| 19 | + %{org: org, project: project, base: base} | |
| 20 | + end | |
| 21 | + | |
| 22 | + defp logs_q(org, q), do: LogLive.Index.logs_path(org.slug, "web", q) | |
| 23 | + | |
| 24 | + defp log_by_body(project, body), | |
| 25 | + do: Enum.find(Logs.search(project.id, [], limit: 500), &(&1.body == body)) | |
| 26 | + | |
| 27 | + test "is linked from the project's issues page", %{conn: conn, org: org} do | |
| 28 | + {:ok, view, _} = live(conn, ~p"/orgs/#{org.slug}/projects/web") | |
| 29 | + assert has_element?(view, "#project-logs[href='/orgs/#{org.slug}/projects/web/logs']") | |
| 30 | + end | |
| 31 | + | |
| 32 | + test "explains how to turn logs on when there are none", %{conn: conn, base: base} do | |
| 33 | + {:ok, view, _} = live(conn, base) | |
| 34 | + assert has_element?(view, "#logs-setup") | |
| 35 | + assert has_element?(view, "#logs-setup-elixir", "logs: [level: :info]") | |
| 36 | + assert has_element?(view, "#logs-setup-python", "enable_logs=True") | |
| 37 | + refute has_element?(view, "#log-search") | |
| 38 | + refute has_element?(view, "#tail-toggle") | |
| 39 | + end | |
| 40 | + | |
| 41 | + test "the first line to arrive replaces the guide", ctx do | |
| 42 | + {:ok, view, _} = live(ctx.conn, ctx.base) | |
| 43 | + send_logs(ctx.project, [log_line("first ever")]) | |
| 44 | + refute has_element?(view, "#logs-setup") | |
| 45 | + assert has_element?(view, "#logs li .log-body", "first ever") | |
| 46 | + end | |
| 47 | + | |
| 48 | + test "rows show time, level badge, source and body", ctx do | |
| 49 | + send_logs(ctx.project, [ | |
| 50 | + log_line("order placed", service: "shop"), | |
| 51 | + log_line("payment failed", level: "error"), | |
| 52 | + log_line("disk gone", level: "fatal") | |
| 53 | + ]) | |
| 54 | + | |
| 55 | + placed = log_by_body(ctx.project, "order placed") | |
| 56 | + failed = log_by_body(ctx.project, "payment failed") | |
| 57 | + fatal = log_by_body(ctx.project, "disk gone") | |
| 58 | + | |
| 59 | + {:ok, view, _} = live(ctx.conn, ctx.base) | |
| 60 | + refute has_element?(view, "#logs-setup") | |
| 61 | + assert has_element?(view, "#log-#{placed.id} .log-level[data-level='info']", "info") | |
| 62 | + assert has_element?(view, "#log-#{placed.id} .log-source", "shop") | |
| 63 | + assert has_element?(view, "#log-#{placed.id} .log-body", "order placed") | |
| 64 | + assert has_element?(view, "#log-#{placed.id} time[datetime]") | |
| 65 | + assert has_element?(view, "#log-#{failed.id} .log-level.badge-error", "error") | |
| 66 | + assert has_element?(view, "#log-#{fatal.id} .log-level.badge-error", "fatal") | |
| 67 | + | |
| 68 | + # Newest first. | |
| 69 | + ids = | |
| 70 | + view | |
| 71 | + |> render() | |
| 72 | + |> LazyHTML.from_fragment() | |
| 73 | + |> LazyHTML.query("#logs > li[data-level]") | |
| 74 | + |> LazyHTML.attribute("id") | |
| 75 | + | |
| 76 | + assert ids == ["log-#{fatal.id}", "log-#{failed.id}", "log-#{placed.id}"] | |
| 77 | + refute has_element?(view, "#load-older") | |
| 78 | + assert has_element?(view, "#log-volume-bars") | |
| 79 | + end | |
| 80 | + | |
| 81 | + test "searches through the URL's q", ctx do | |
| 82 | + send_logs(ctx.project, [ | |
| 83 | + log_line("order placed"), | |
| 84 | + log_line("payment declined", level: "warning", env: "staging") | |
| 85 | + ]) | |
| 86 | + | |
| 87 | + declined = log_by_body(ctx.project, "payment declined") | |
| 88 | + placed = log_by_body(ctx.project, "order placed") | |
| 89 | + | |
| 90 | + {:ok, view, _} = live(ctx.conn, ctx.base <> "?q=declined") | |
| 91 | + assert has_element?(view, "#log-search-q[value='declined']") | |
| 92 | + assert has_element?(view, "#log-#{declined.id}") | |
| 93 | + refute has_element?(view, "#log-#{placed.id}") | |
| 94 | + | |
| 95 | + view |> form("#log-search", %{q: "env:prod"}) |> render_submit() | |
| 96 | + assert_patch(view, logs_q(ctx.org, "env:prod")) | |
| 97 | + assert has_element?(view, "#log-#{placed.id}") | |
| 98 | + refute has_element?(view, "#log-#{declined.id}") | |
| 99 | + | |
| 100 | + view |> form("#log-search", %{q: "nothing-like-this"}) |> render_submit() | |
| 101 | + assert has_element?(view, "#logs-empty", "nothing-like-this") | |
| 102 | + refute has_element?(view, "#logs-setup") | |
| 103 | + end | |
| 104 | + | |
| 105 | + test "level chips and facet menus edit the query", ctx do | |
| 106 | + send_logs(ctx.project, [ | |
| 107 | + log_line("a", level: "error", service: "billing"), | |
| 108 | + log_line("b", level: "warning", env: "staging") | |
| 109 | + ]) | |
| 110 | + | |
| 111 | + {:ok, view, _} = live(ctx.conn, ctx.base <> "?q=timeout") | |
| 112 | + refute has_element?(view, "#level-error[aria-pressed='true']") | |
| 113 | + | |
| 114 | + view |> element("#level-error") |> render_click() | |
| 115 | + assert_patch(view, logs_q(ctx.org, "timeout level:error")) | |
| 116 | + assert has_element?(view, "#level-error[aria-pressed='true']") | |
| 117 | + | |
| 118 | + view |> element("#level-warn") |> render_click() | |
| 119 | + assert_patch(view, logs_q(ctx.org, "timeout level:warn,error")) | |
| 120 | + | |
| 121 | + view |> element("#level-error") |> render_click() | |
| 122 | + assert_patch(view, logs_q(ctx.org, "timeout level:warn")) | |
| 123 | + | |
| 124 | + view |> element("#level-warn") |> render_click() | |
| 125 | + assert_patch(view, logs_q(ctx.org, "timeout")) | |
| 126 | + | |
| 127 | + view | |
| 128 | + |> form("#log-facets", %{facets: %{env: "staging", service: "billing"}}) | |
| 129 | + |> render_change() | |
| 130 | + | |
| 131 | + assert_patch(view, logs_q(ctx.org, "timeout env:staging service:billing")) | |
| 132 | + | |
| 133 | + view |> form("#log-facets", %{facets: %{env: "", service: "billing"}}) |> render_change() | |
| 134 | + assert_patch(view, logs_q(ctx.org, "timeout service:billing")) | |
| 135 | + end | |
| 136 | + | |
| 137 | + test "a row's detail: attributes, add as filter, trace and its errors", ctx do | |
| 138 | + trace = trace_id() | |
| 139 | + trace_error_fixture(ctx.project, trace, "kaboom") | |
| 140 | + | |
| 141 | + send_logs(ctx.project, [ | |
| 142 | + log_line("checkout", trace: trace, attrs: %{"order.id" => 42, "note" => "two words"}) | |
| 143 | + ]) | |
| 144 | + | |
| 145 | + log = log_by_body(ctx.project, "checkout") | |
| 146 | + {:ok, view, _} = live(ctx.conn, ctx.base) | |
| 147 | + detail = "#log-#{log.id}-detail" | |
| 148 | + | |
| 149 | + assert has_element?(view, "#log-#{log.id}-row[aria-controls='log-#{log.id}-detail']") | |
| 150 | + assert has_element?(view, "#{detail}.hidden", "order.id") | |
| 151 | + assert has_element?(view, detail, "42") | |
| 152 | + assert has_element?(view, detail, "prod") | |
| 153 | + | |
| 154 | + assert has_element?( | |
| 155 | + view, | |
| 156 | + "#log-#{log.id}-trace[href='/orgs/#{ctx.org.slug}/traces/#{trace}']" | |
| 157 | + ) | |
| 158 | + | |
| 159 | + issue = Vigil.Logs.trace_issues(ctx.project.id, [trace])[trace] | |
| 160 | + assert has_element?(view, "#log-#{log.id}-issue[href$='/issues/#{issue.short_id}']") | |
| 161 | + | |
| 162 | + view |> element("#{detail} button[phx-value-key='order.id']") |> render_click() | |
| 163 | + assert_patch(view, logs_q(ctx.org, "order.id:42")) | |
| 164 | + assert has_element?(view, "#log-#{log.id}") | |
| 165 | + | |
| 166 | + view |> element("#{detail} button[phx-value-key='note']") |> render_click() | |
| 167 | + assert_patch(view, logs_q(ctx.org, ~s(order.id:42 note:"two words"))) | |
| 168 | + assert has_element?(view, "#log-#{log.id}") | |
| 169 | + | |
| 170 | + view |> element("#{detail} button[phx-value-key='env']") |> render_click() | |
| 171 | + assert_patch(view, logs_q(ctx.org, ~s(order.id:42 note:"two words" env:prod))) | |
| 172 | + end | |
| 173 | + | |
| 174 | + test "the live tail puts matching lines on top and leaves others out", ctx do | |
| 175 | + send_logs(ctx.project, [log_line("old error", level: "error")]) | |
| 176 | + {:ok, view, _} = live(ctx.conn, ctx.base <> "?q=level:error") | |
| 177 | + assert has_element?(view, "#tail-toggle[data-live='true']") | |
| 178 | + | |
| 179 | + send_logs(ctx.project, [ | |
| 180 | + log_line("tail error", level: "error"), | |
| 181 | + log_line("tail info", level: "info") | |
| 182 | + ]) | |
| 183 | + | |
| 184 | + tailed = log_by_body(ctx.project, "tail error") | |
| 185 | + ignored = log_by_body(ctx.project, "tail info") | |
| 186 | + assert has_element?(view, "#log-#{tailed.id}", "tail error") | |
| 187 | + refute has_element?(view, "#log-#{ignored.id}") | |
| 188 | + | |
| 189 | + first = | |
| 190 | + view | |
| 191 | + |> render() | |
| 192 | + |> LazyHTML.from_fragment() | |
| 193 | + |> LazyHTML.query("#logs > li[data-level]") | |
| 194 | + |> LazyHTML.attribute("id") | |
| 195 | + |> hd() | |
| 196 | + | |
| 197 | + assert first == "log-#{tailed.id}" | |
| 198 | + end | |
| 199 | + | |
| 200 | + test "paused, new lines are counted; showing them resumes", ctx do | |
| 201 | + send_logs(ctx.project, [log_line("before")]) | |
| 202 | + {:ok, view, _} = live(ctx.conn, ctx.base) | |
| 203 | + | |
| 204 | + view |> element("#tail-toggle") |> render_click() | |
| 205 | + assert has_element?(view, "#tail-toggle[data-live='false']", "Paused") | |
| 206 | + | |
| 207 | + send_logs(ctx.project, [log_line("one"), log_line("two")]) | |
| 208 | + send_logs(ctx.project, [log_line("three")]) | |
| 209 | + | |
| 210 | + refute has_element?(view, "#logs li .log-body", "one") | |
| 211 | + assert has_element?(view, "#tail-pending", "3 new lines") | |
| 212 | + | |
| 213 | + view |> element("#tail-pending") |> render_click() | |
| 214 | + assert has_element?(view, "#tail-toggle[data-live='true']") | |
| 215 | + refute has_element?(view, "#tail-pending") | |
| 216 | + assert has_element?(view, "#logs li .log-body", "one") | |
| 217 | + assert has_element?(view, "#logs li .log-body", "three") | |
| 218 | + end | |
| 219 | + | |
| 220 | + test "load older pages back from the oldest row", ctx do | |
| 221 | + start = DateTime.add(DateTime.utc_now(), -200, :second) | |
| 222 | + | |
| 223 | + lines = | |
| 224 | + for i <- 1..101, | |
| 225 | + do: log_line("line #{i}", at: DateTime.add(start, i, :second)) | |
| 226 | + | |
| 227 | + send_logs(ctx.project, lines) | |
| 228 | + oldest = log_by_body(ctx.project, "line 1") | |
| 229 | + | |
| 230 | + {:ok, view, _} = live(ctx.conn, ctx.base) | |
| 231 | + refute has_element?(view, "#log-#{oldest.id}") | |
| 232 | + assert has_element?(view, "#load-older") | |
| 233 | + | |
| 234 | + view |> element("#load-older") |> render_click() | |
| 235 | + assert has_element?(view, "#log-#{oldest.id}") | |
| 236 | + refute has_element?(view, "#load-older") | |
| 237 | + end | |
| 238 | + | |
| 239 | + test "a member who can't see the project is refused", %{conn: conn, user: user} do | |
| 240 | + owner = user_fixture() | |
| 241 | + other = organization_fixture(owner) | |
| 242 | + project_fixture(org_scope(owner, other), %{"name" => "hidden"}) | |
| 243 | + member_fixture(other, user, :member) | |
| 244 | + | |
| 245 | + assert {:error, {:live_redirect, %{to: to}}} = | |
| 246 | + live(conn, ~p"/orgs/#{other.slug}/projects/hidden/logs") | |
| 247 | + | |
| 248 | + assert to == ~p"/orgs/#{other.slug}" | |
| 249 | + end | |
| 250 | + | |
| 251 | + describe "the trace page" do | |
| 252 | + test "lists the trace's logs, linked to the project's logs for it", ctx do | |
| 253 | + trace = transaction_fixture(ctx.project) | |
| 254 | + send_logs(ctx.project, [log_line("in the trace", trace: trace), log_line("elsewhere")]) | |
| 255 | + log = log_by_body(ctx.project, "in the trace") | |
| 256 | + | |
| 257 | + {:ok, view, _} = live(ctx.conn, ~p"/orgs/#{ctx.org.slug}/traces/#{trace}") | |
| 258 | + assert has_element?(view, "#trace-logs #trace-log-#{log.id}", "in the trace") | |
| 259 | + refute has_element?(view, "#trace-logs", "elsewhere") | |
| 260 | + | |
| 261 | + assert has_element?( | |
| 262 | + view, | |
| 263 | + "#trace-log-#{log.id} a[href='#{logs_q(ctx.org, "trace:#{trace}")}']" | |
| 264 | + ) | |
| 265 | + end | |
| 266 | + | |
| 267 | + test "shows the logs of a trace with no spans or errors kept", ctx do | |
| 268 | + trace = trace_id() | |
| 269 | + send_logs(ctx.project, [log_line("lonely", trace: trace)]) | |
| 270 | + | |
| 271 | + {:ok, view, _} = live(ctx.conn, ~p"/orgs/#{ctx.org.slug}/traces/#{trace}") | |
| 272 | + assert has_element?(view, "#trace-missing") | |
| 273 | + assert has_element?(view, "#trace-logs", "lonely") | |
| 274 | + end | |
| 275 | + | |
| 276 | + test "no logs, no section", ctx do | |
| 277 | + trace = transaction_fixture(ctx.project) | |
| 278 | + {:ok, view, _} = live(ctx.conn, ~p"/orgs/#{ctx.org.slug}/traces/#{trace}") | |
| 279 | + refute has_element?(view, "#trace-logs") | |
| 280 | + end | |
| 281 | + end | |
| 282 | +end |
Parents: 60e74af