Skip to content

Commit 0bf3fe6

Browse files
authored
Merge pull request #7 from ertgl/feat/tracer-hooks
Introduce tracer hooks
2 parents 54a8069 + c23b7ee commit 0bf3fe6

38 files changed

+1004
-261
lines changed

README.md

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# tapable-tracer
22

33
Trace the connections and flows between
4-
[tapable](https://github.com/webpack/tapable) hooks.
4+
[tapable](https://github.com/webpack/tapable) hooks in real-time.
55

66
Collect structured stack traces, and optionally export them as UML diagrams.
77

@@ -11,12 +11,16 @@ Collect structured stack traces, and optionally export them as UML diagrams.
1111
- [Features](#features)
1212
- [Installation](#installation)
1313
- [Usage](#usage)
14-
- [UML Export](#uml-export)
14+
- [Initialize Tracer](#initialize-tracer)
15+
- [Register Hooks](#register-hooks)
16+
- [Export Frames](#export-frames)
17+
- [Generate UML](#generate-uml)
18+
- [Tracer Internals](#tracer-internals)
1519
- [Configuration](#configuration)
16-
- [Tracer Options](#tracer-options)
17-
- [Hook Tracing Options](#hook-tracing-options)
20+
- [Global Options](#global-options)
21+
- [Per-Hook Options](#per-hook-options)
1822
- [Technical Details](#technical-details)
19-
- [Output](#output)
23+
- [Examples](#examples)
2024
- [License](#license)
2125

2226
## Overview
@@ -45,110 +49,117 @@ View the full interactive version here:
4549

4650
## Features
4751

48-
- **Real-time**: Observe `tap` and `call` flows with a callback.
49-
- **Structured**: Frames are stored as a directed graph.
50-
- **Dynamic**: No need to alter the hook source code or application logic.
51-
- **Configurable**: Include or exclude the triggers, customize labels.
52-
- **UML Support**: Generate a UML representation of your system from the trace.
53-
- **Independent**: Works with any tapable usage, including but not limited to
54-
webpack.
52+
- **Real-time**: Observe hooks as they're tapped and called.
53+
- **Structured**: Frames represent a directed graph.
54+
- **Dynamic**: No patching or rewriting needed.
55+
- **UML Export**: Visualize traces via Mermaid diagrams.
56+
- **Configurable**: Include or exclude the triggers.
57+
- **Customizable**: Embed information to be visible on diagrams.
58+
- **Universal**: Works with any tapable-based code, not just webpack.
5559

5660
## Installation
5761

58-
To install `tapable-tracer`, use your favorite package manager.
59-
60-
For example, with Yarn:
61-
6262
```sh
6363
yarn add tapable-tracer
6464
```
6565

6666
## Usage
6767

68-
Import the tracer utilities and start tracing your tapable hooks.
68+
### Initialize Tracer
69+
70+
To start tracing hooks, first create a tracer:
6971

7072
```ts
71-
import {
72-
createTracer,
73-
dumpStackTrace,
74-
traceHook,
75-
} from "tapable-tracer";
73+
import { createTracer } from "tapable-tracer";
7674

77-
// Create a tracer.
7875
const tracer = createTracer();
76+
```
77+
78+
### Register Hooks
79+
80+
To capture hook activity, register each hook with the tracer:
81+
82+
```ts
83+
import { traceHook } from "tapable-tracer";
7984

80-
// Start tracing the hooks.
8185
traceHook(tracer, hook1);
8286
traceHook(tracer, hook2);
87+
```
88+
89+
### Export Frames
90+
91+
Export the captured frames as encodable array:
92+
93+
```ts
94+
import { dumpStackTrace } from "tapable-tracer";
8395

84-
// Get the encodable frames.
8596
const frames = dumpStackTrace(tracer.trace);
8697
```
8798

88-
### UML Export
99+
### Generate UML
89100

90-
`tapable-tracer` has a built-in extension for exporting the collected trace
91-
data as a UML diagram code using the Mermaid format. This allows for
92-
visualization of the relationships and flows between the tapable hooks.
101+
Generate a Mermaid-compatible diagram code:
93102

94103
```ts
95104
import { generateMermaidUML } from "tapable-tracer/extensions/mermaid";
96105

97-
// Export the diagram code.
98106
const uml = generateMermaidUML(frames);
99107
```
100108

109+
### Tracer Internals
110+
111+
`tapable-tracer` exposes its own hooks (via tapable) for further
112+
instrumentation:
113+
114+
- [PreCallHook](src/tracer/hooks/pre-call/PreCallHook.ts): Before a `Tap.fn`
115+
called.
116+
- [PostCallHook](src/tracer/hooks/post-call/PostCallHook.ts): After a `Tap.fn`
117+
completes.
118+
- [HandleStackFrameHook](src/tracer/hooks/handle-stack-frame/HandleStackFrameHook.ts):
119+
When a new stack frame is emitted.
120+
101121
## Configuration
102122

103-
### Tracer Options
123+
### Global Options
104124

105-
To configure the tracer, pass a [`TracerOptions`](src/tracer/TracerOptions.ts)
106-
object to the `createTracer` function.
125+
Pass [`TracerOptions`](src/tracer/options/TracerOptions.ts) to
126+
`createTracer()`:
107127

108-
The available options are listed below:
128+
The available options are:
109129

110-
- **handleStackFrame** ([`StackFrameHandler`](src/tracer/callbacks/stack-frame-handler/StackFrameHandler.ts)):
111-
Real-time callback function.
112130
- **interceptorName** (`string`): Name of the interceptor to use.
113131
- **labelHook** ([`HookLabellerFunction`](src/hook/label/HookLabellerFunction.ts)):
114132
Function to label hooks.
115133
- **labelTap** ([`TapLabellerFunction`](src/tap/label/TapLabellerFunction.ts)):
116134
Function to label taps.
117135

118-
### Hook Tracing Options
136+
### Per-Hook Options
119137

120-
To configure the process of tracing a single hook, pass a
121-
[`HookTracingOptions`](src/tracer/HookTracingOptions.ts) object to the
122-
`traceHook` function.
138+
Pass [`HookTracingOptions`](src/tracer/options/HookTracingOptions.ts) to
139+
`traceHook()`:
123140

124-
The available options are listed below:
141+
The available options are:
125142

126143
- **includeTrigger** (`boolean`): Whether to include the trigger in the trace.
127144
- **key** (`string`): The hook's identifier in a container data-structure
128145
inside the system. Also used as the fallback label for the hook.
129146

130147
## Technical Details
131148

132-
This section describes the internal mechanism of the tracer and how it captures
133-
the frames of the hooks.
134-
135149
The tracer captures three different frame types:
136150

137-
- [`CallFrame`](src/stack-frame/CallFrame.ts): Represents a call to a tapable
138-
hook.
139-
- [`TapFrame`](src/stack-frame/TapFrame.ts): Represents a tap added to a hook.
140-
- [`TriggerFrame`](src/stack-frame/TriggerFrame.ts): Represents the
141-
deterministic callback point of a tap. Useful if the tap shares the same name
142-
with the parent trigger (such as plugins in webpack).
151+
- [`TapFrame`](src/stack-frame/TapFrame.ts): A tap is registered to a hook.
152+
- [`TriggerFrame`](src/stack-frame/TriggerFrame.ts): A delegate is called
153+
before the actual `Tap.fn`.
154+
- [`CallFrame`](src/stack-frame/CallFrame.ts): A `Tap.fn` is called.
143155

144156
Additionally, it uses a [`CallSite`](src/tracer/stack/CallSite.ts) context
145157
object per tap, for storing the hook, tap, and the original callback function.
146158

147-
To capture these frames the tracer uses two separate states:
159+
To capture the frames the tracer uses two separate states:
148160

149-
1. Stack: A stack of `CallSite` objects that represents the current call
150-
stack.
151-
2. Trace: A list of frames that represents the entire trace of the hooks.
161+
1. Stack: A stack of `CallSite` objects that represents the current call stack.
162+
2. Trace: A list of frames that represents the entire trace of the flows.
152163

153164
For tracing a hook, the tracer intercepts the hook's `tap`, `call` and `loop`
154165
events.
@@ -170,10 +181,7 @@ When a `call` or `loop` event occurs:
170181
4. Pop the `CallSite` object from the `stack`.
171182
5. Create and push a `CallFrame` onto the `trace` list.
172183

173-
### Output
174-
175-
The output of the tracer is a list of frames that can be used to generate
176-
UML diagrams or other visualizations.
184+
### Examples
177185

178186
<details>
179187
<summary>
@@ -191,6 +199,12 @@ UML diagrams or other visualizations.
191199
{ callee: 'hook4', caller: 'hook3', type: 'call' }
192200
]
193201
```
202+
</details>
203+
204+
<details>
205+
<summary>
206+
<b>Example: Graph visualization of the output without triggers</b>
207+
</summary>
194208

195209
<picture>
196210
<source
@@ -223,6 +237,12 @@ UML diagrams or other visualizations.
223237
{ callee: 'hook4', caller: 'Plugin4', type: 'call' }
224238
]
225239
```
240+
</details>
241+
242+
<details>
243+
<summary>
244+
<b>Example: Graph visualization of the output with triggers</b>
245+
</summary>
226246

227247
<picture>
228248
<source

0 commit comments

Comments
 (0)