-
Notifications
You must be signed in to change notification settings - Fork 20
Refactor: Add DisplayNewTraces hook
#670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
lib/live_debugger_refactor/app/debugger/callback_tracing/web/hooks/display_new_traces.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| defmodule LiveDebuggerRefactor.App.Debugger.CallbackTracing.Web.Hooks.DisplayNewTraces do | ||
| @moduledoc """ | ||
| This hook is responsible for displaying new traces. | ||
| It is used to display new traces and filter them by execution time when the user starts tracing. | ||
|
|
||
| It incorporates debouncing `TraceCalled` event. It helps to reduce inserting and deleting traces from | ||
| a stream in a very short intervals (e.g. when callback execution time is only couple of microseconds | ||
| and they don't match the filter they appear in the UI and are removed very quickly which casuses blinking effect). | ||
| """ | ||
|
|
||
| use LiveDebuggerRefactor.App.Web, :hook | ||
|
|
||
| alias LiveDebuggerRefactor.Structs.Trace | ||
| alias LiveDebuggerRefactor.API.TracesStorage | ||
| alias LiveDebuggerRefactor.App.Debugger.CallbackTracing.Web.Helpers.Filters, as: FiltersHelpers | ||
| alias LiveDebuggerRefactor.App.Debugger.CallbackTracing.Structs.TraceDisplay | ||
|
|
||
| alias LiveDebuggerRefactor.Services.CallbackTracer.Events.TraceCalled | ||
| alias LiveDebuggerRefactor.Services.CallbackTracer.Events.TraceReturned | ||
| alias LiveDebuggerRefactor.Services.CallbackTracer.Events.TraceErrored | ||
|
|
||
| @debounce_timeout_ms 10 | ||
|
|
||
| @required_assigns [ | ||
| :current_filters, | ||
| :traces_empty? | ||
| ] | ||
|
|
||
| @doc """ | ||
| Initializes the hook by attaching the hook to the socket and checking the required assigns, other hooks and streams. | ||
| """ | ||
| @spec init(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t() | ||
| def init(socket) do | ||
| socket | ||
| |> check_hook!(:tracing_fuse) | ||
| |> check_assigns!(@required_assigns) | ||
| |> check_stream!(:existing_traces) | ||
| |> check_private!(:live_stream_limit) | ||
| |> put_private(:trace_insertion_canceled, false) | ||
| |> attach_hook(:display_new_traces, :handle_info, &handle_info/2) | ||
| |> register_hook(:display_new_traces) | ||
| end | ||
|
|
||
| defp handle_info(%TraceCalled{} = trace_called, socket) do | ||
| debounce_trace_event(trace_called) | ||
|
|
||
| socket | ||
| |> put_private(:trace_insertion_canceled, false) | ||
| |> halt() | ||
| end | ||
|
|
||
| defp handle_info(%TraceReturned{trace_id: trace_id, ets_ref: table}, socket) do | ||
| trace = TracesStorage.get_by_id!(table, trace_id) | ||
|
|
||
| socket | ||
| |> stream_update_trace(trace) | ||
| |> push_event("stop-timer", %{}) | ||
| |> halt() | ||
| end | ||
|
|
||
| defp handle_info(%TraceErrored{trace_id: trace_id, ets_ref: table}, socket) do | ||
| trace = TracesStorage.get_by_id!(table, trace_id) | ||
|
|
||
| socket | ||
| |> stream_update_trace(trace) | ||
| |> push_event("stop-timer", %{}) | ||
| |> halt() | ||
| end | ||
|
|
||
| defp handle_info({:debounce, _}, socket) when socket.private.trace_insertion_canceled do | ||
| {:halt, socket} | ||
| end | ||
|
|
||
| defp handle_info({:debounce, %TraceCalled{trace_id: trace_id, ets_ref: table}}, socket) do | ||
| trace = TracesStorage.get_by_id!(table, trace_id) | ||
|
|
||
| socket | ||
| |> stream_insert_trace(trace) | ||
| |> assign(traces_empty?: false) | ||
| |> halt() | ||
| end | ||
|
|
||
| defp handle_info(_, socket), do: {:cont, socket} | ||
|
|
||
| defp stream_update_trace(socket, trace) do | ||
| if matches_execution_time_filter?(socket, trace) do | ||
| socket | ||
| |> stream_insert_trace(trace) | ||
| |> assign(traces_empty?: false) | ||
| else | ||
| socket | ||
| |> stream_delete(:existing_traces, TraceDisplay.from_trace(trace, true)) | ||
| end | ||
| |> put_private(:trace_insertion_canceled, true) | ||
| end | ||
|
|
||
| defp stream_insert_trace(socket, trace) do | ||
| stream_insert( | ||
| socket, | ||
| :existing_traces, | ||
| TraceDisplay.from_trace(trace, true), | ||
| at: 0, | ||
| limit: socket.private.live_stream_limit | ||
| ) | ||
| end | ||
|
|
||
| defp matches_execution_time_filter?(socket, %Trace{execution_time: execution_time}) do | ||
| execution_time_limits = FiltersHelpers.get_execution_times(socket.assigns.current_filters) | ||
|
|
||
| min_time = Map.get(execution_time_limits, "exec_time_min", 0) | ||
| max_time = Map.get(execution_time_limits, "exec_time_max", :infinity) | ||
|
|
||
| execution_time >= min_time and execution_time <= max_time | ||
| end | ||
|
|
||
| defp debounce_trace_event(event) do | ||
| Process.send_after(self(), {:debounce, event}, @debounce_timeout_ms) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.