@use-tusk/drift-node-sdk 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,30 +12,33 @@ The Node.js Tusk Drift SDK enables fast and deterministic API testing by capturi
12
12
 
13
13
  ## Documentation
14
14
 
15
- For comprehensive guides and API reference, visit our [full documentation](https://docs.usetusk.ai/automated-tests/installation#setup).
15
+ For comprehensive guides and API reference, visit our [full documentation](https://docs.usetusk.ai/api-tests/installation#setup).
16
16
 
17
17
  ## Requirements
18
18
 
19
19
  Tusk Drift currently supports the following packages and versions:
20
20
 
21
21
  - **HTTP/HTTPS**: All versions (Node.js built-in)
22
+ - **GRPC**: `@grpc/grpc-js@1.x` (Outbound requests only)
22
23
  - **PG**: `pg@8.x`, `pg-pool@2.x-3.x`
23
24
  - **Postgres**: `postgres@3.x`
25
+ - **MySQL**: `mysql2@3.x`
26
+ - **IORedis**: `ioredis@4.x-5.x`
27
+ - **GraphQL**: `graphql@15.x-16.x`
24
28
  - **JSON Web Tokens**: `jsonwebtoken@5.x-9.x`
25
29
  - **JWKS RSA**: `jwks-rsa@1.x-3.x`
26
- - **GraphQL**: `graphql@15.x-16.x`
27
30
 
28
31
  If you're using packages or versions not listed above, please create an issue with the package + version you'd like an instrumentation for.
29
32
 
30
33
  ## Installation
31
34
 
32
- ### Step 1: CLI Setup
35
+ ### Step 1: Install the CLI
33
36
 
34
- First, install and configure the Tusk Drift CLI by following our [CLI installation guide](https://github.com/Use-Tusk/tusk-drift-cli?tab=readme-ov-file#install).
37
+ First, install and configure the Tusk Drift CLI by following our [CLI installation guide](https://github.com/Use-Tusk/tusk-drift-cli?tab=readme-ov-file#install). The CLI helps set up your Tusk configuration file and replays tests.
35
38
 
36
39
  The wizard will eventually direct you back here when it's time to set up the SDK.
37
40
 
38
- ### Step 2: SDK Installation
41
+ ### Step 2: Install the SDK
39
42
 
40
43
  After completing the CLI wizard, install the SDK:
41
44
 
@@ -43,290 +46,24 @@ After completing the CLI wizard, install the SDK:
43
46
  npm install @use-tusk/drift-node-sdk
44
47
  ```
45
48
 
46
- ## Initialization
47
-
48
- ### Prerequisites
49
-
50
- Before setting up the SDK, ensure you have:
51
-
52
- - Completed the [CLI wizard](https://github.com/Use-Tusk/tusk-drift-cli?tab=readme-ov-file#quick-start)
53
- - Obtained an API key from the [Tusk Drift dashboard](https://usetusk.ai/app/settings/api-keys) (only required if using Tusk Cloud)
54
-
55
- Follow these steps in order to properly initialize the Tusk Drift SDK:
56
-
57
- ### 1. Create SDK Initialization File
58
-
59
- Create a separate file (e.g. `tuskDriftInit.ts`) to initialize the Tusk Drift SDK. This ensures the SDK is initialized as early as possible before any other modules are loaded.
60
-
61
- **IMPORTANT**: Ensure that `TuskDrift` is initialized before any other telemetry providers (e.g. OpenTelemetry, Sentry, etc.). If not, your existing telemetry may not work properly.
62
-
63
- #### For CommonJS Applications
64
-
65
- ```typescript
66
- // tuskDriftInit.ts
67
- import { TuskDrift } from "@use-tusk/drift-node-sdk";
68
-
69
- // Initialize SDK immediately
70
- TuskDrift.initialize({
71
- apiKey: process.env.TUSK_DRIFT_API_KEY,
72
- env: process.env.NODE_ENV,
73
- });
74
-
75
- export { TuskDrift };
76
- ```
77
-
78
- #### For ESM Applications
79
-
80
- ESM applications require additional setup to properly intercept module imports:
81
-
82
- ```typescript
83
- // tuskDriftInit.ts
84
- import { register } from 'node:module';
85
- import { pathToFileURL } from 'node:url';
86
-
87
- // Register the ESM loader
88
- // This enables interception of ESM module imports
89
- register('@use-tusk/drift-node-sdk/hook.mjs', pathToFileURL('./'));
90
-
91
- import { TuskDrift } from "@use-tusk/drift-node-sdk";
92
-
93
- // Initialize SDK immediately
94
- TuskDrift.initialize({
95
- apiKey: process.env.TUSK_DRIFT_API_KEY,
96
- env: process.env.NODE_ENV,
97
- });
98
-
99
- export { TuskDrift };
100
- ```
101
-
102
- **Why the ESM loader is needed**: ESM imports are statically analyzed and hoisted, meaning all imports are resolved before any code runs. The `register()` call sets up Node.js loader hooks that intercept module imports, allowing the SDK to instrument packages like `postgres`, `http`, etc. Without this, the SDK cannot patch ESM modules.
103
-
104
- #### Configuration Options
105
-
106
- <table>
107
- <thead>
108
- <tr>
109
- <th>Option</th>
110
- <th>Type</th>
111
- <th>Default</th>
112
- <th>Description</th>
113
- </tr>
114
- </thead>
115
- <tbody>
116
- <tr>
117
- <td><code>apiKey</code></td>
118
- <td><code>string</code></td>
119
- <td><b>Required if using Tusk Cloud</b></td>
120
- <td>Your Tusk Drift API key.</td>
121
- </tr>
122
- <tr>
123
- <td><code>env</code></td>
124
- <td><code>string</code></td>
125
- <td><code>process.env.NODE_ENV</code></td>
126
- <td>The environment name.</td>
127
- </tr>
128
- <tr>
129
- <td><code>logLevel</code></td>
130
- <td><code>'silent' | 'error' | 'warn' | 'info' | 'debug'</code></td>
131
- <td><code>'info'</code></td>
132
- <td>The logging level.</td>
133
- </tr>
134
- </tbody>
135
- </table>
136
-
137
- ### 2. Import SDK at Application Entry Point
138
-
139
- #### For CommonJS Applications
140
-
141
- In your main server file (e.g., `server.ts`, `index.ts`, `app.ts`), require the initialized SDK **at the very top**, before any other requires:
142
-
143
- ```typescript
144
- // server.ts
145
- import { TuskDrift } from "./tuskDriftInit"; // MUST be the first import
146
-
147
- // ... other imports ...
148
-
149
- // Your application setup...
150
- ```
151
-
152
- > **IMPORTANT**: Ensure NO require calls are made before requiring the SDK initialization file. This guarantees proper instrumentation of all dependencies.
153
-
154
- #### For ESM Applications
155
-
156
- For ESM applications, you **cannot** control import order within your application code because all imports are hoisted. Instead, use the `--import` flag:
157
-
158
- **Update your package.json scripts**:
159
-
160
- ```json
161
- {
162
- "scripts": {
163
- "dev": "node --import ./dist/tuskDriftInit.js dist/server.js",
164
- "dev:record": "TUSK_DRIFT_MODE=RECORD node --import ./dist/tuskDriftInit.js dist/server.js"
165
- }
166
- }
167
- ```
168
-
169
- **Why `--import` is required for ESM**: In ESM, all `import` statements are hoisted and evaluated before any code runs, making it impossible to control import order within a file. The `--import` flag ensures the SDK initialization (including loader registration) happens in a separate phase before your application code loads, guaranteeing proper module interception.
170
-
171
- ### 3. Update Configuration File
172
-
173
- Update the configuration file `.tusk/config.yaml` to include a `recording` section. Example `recording` configuration:
174
-
175
- ```yaml
176
- # ... existing configuration ...
177
-
178
- recording:
179
- sampling_rate: 0.1
180
- export_spans: true
181
- enable_env_var_recording: true
182
- ```
183
-
184
- #### Configuration Options
185
-
186
- <table>
187
- <thead>
188
- <tr>
189
- <th>Option</th>
190
- <th>Type</th>
191
- <th>Default</th>
192
- <th>Description</th>
193
- </tr>
194
- </thead>
195
- <tbody>
196
- <tr>
197
- <td><code>sampling_rate</code></td>
198
- <td><code>number</code></td>
199
- <td><code>1.0</code></td>
200
- <td>The sampling rate (0.0 - 1.0). 1.0 means 100% of requests are recorded, 0.0 means 0% of requests are recorded.</td>
201
- </tr>
202
- <tr>
203
- <td><code>export_spans</code></td>
204
- <td><code>boolean</code></td>
205
- <td><code>false</code></td>
206
- <td>Whether to export spans to Tusk backend or local files (<code>.tusk/traces</code>). If false, spans are only exported to local files.</td>
207
- </tr>
208
- <tr>
209
- <td><code>enable_env_var_recording</code></td>
210
- <td><code>boolean</code></td>
211
- <td><code>false</code></td>
212
- <td>Whether to enable environment variable recording and replaying. Recommended if your application's business logic depends on environment variables, as this ensures the most accurate replay behavior.</td>
213
- </tr>
214
- </tbody>
215
- </table>
216
-
217
- ### 4. Mark App as Ready
218
-
219
- Once your application has completed initialization (database connections, middleware setup, etc.), mark it as ready:
49
+ ### Step 3: Initialize the SDK for your service
220
50
 
221
- ```typescript
222
- // server.ts
223
- import { TuskDrift } from "./tuskDriftInit";
51
+ Refer to our [initialization guide](docs/initialization.md) to set up the SDK for your service.
224
52
 
225
- // ... other imports ...
53
+ ### Step 4: Run Your First Test
226
54
 
227
- const app = express();
228
-
229
- // Your application setup...
230
-
231
- app.listen(8000, () => {
232
- // Mark app as ready for recording/replay
233
- TuskDrift.markAppAsReady();
234
- console.log("Server started and ready for Tusk Drift");
235
- });
236
- ```
237
-
238
- ## Run Your First Test
239
-
240
- Let's walk through recording and replaying your first trace:
241
-
242
- ### Step 1: Set sampling rate to 1.0
243
-
244
- Set the `sampling_rate` in `.tusk/config.yaml` to 1.0 to ensure that all requests are recorded.
245
-
246
- ### Step 2: Start server in record mode
247
-
248
- Run your server in record mode using the `TUSK_DRIFT_MODE` environment variable:
249
-
250
- ```bash
251
- TUSK_DRIFT_MODE=RECORD node server.js
252
- ```
253
-
254
- You should see logs indicating Tusk Drift is active:
255
-
256
- ```
257
- [TuskDrift] SDK initialized in RECORD mode
258
- [TuskDrift] App marked as ready
259
- ```
260
-
261
- ### Step 3: Generate Traffic
262
-
263
- Make a request to a simple endpoint that includes some database and/or network calls:
264
-
265
- ```bash
266
- curl http://localhost:8000/api/test/weather
267
- ```
268
-
269
- ### Step 4: Stop Recording
270
-
271
- Wait for a few seconds and then stop your server with `Ctrl+C`. This will give time for traces to be exported.
272
-
273
- ### Step 5: List Recorded Traces
274
-
275
- In your project directory, list the recorded traces:
276
-
277
- ```bash
278
- tusk list
279
- ```
280
-
281
- You should see output similar to:
282
-
283
- <img src="images/tusk-list-output.png">
284
-
285
- Press `ESC` to exit the list view.
286
-
287
- Need to install the Tusk CLI? See [CLI installation guide](https://github.com/Use-Tusk/tusk-drift-cli?tab=readme-ov-file#install).
288
-
289
- ### Step 6: Replay the Trace
290
-
291
- Replay the recorded test:
292
-
293
- ```bash
294
- tusk run
295
- ```
296
-
297
- You should see output similar to:
298
-
299
- <img src="images/tusk-run-output.png">
300
-
301
- **Success!** You've recorded and replayed your first trace.
55
+ Follow along our [quick start guide](docs/quickstart.md) to record and replay your first test!
302
56
 
303
57
  ## Troubleshooting
304
58
 
305
- ### Common Issues
306
-
307
- #### No traces being recorded
308
-
309
- 1. **Check sampling rate**: Ensure `sampling_rate` in `.tusk/config.yaml` is 1.0
310
- 2. **Verify app readiness**: Make sure you're calling `TuskDrift.markAppAsReady()`
311
- 3. **Use debug mode in SDK**: Add `logLevel: 'debug'` to the initialization parameters
312
-
313
- #### Existing telemetry not working
314
-
315
- Ensure that `TuskDrift.initialize()` is called before any other telemetry providers (e.g. OpenTelemetry, Sentry, etc.).
316
-
317
- #### Replay failures
318
-
319
- 1. **Enable service and CLI logs**:
320
-
321
- ```bash
322
- tusk run --debug
323
- ```
59
+ Having issues?
324
60
 
325
- Logs will be written to `.tusk/logs/`
61
+ - Read our [troubleshooting doc](docs/troubleshooting.md)
62
+ - Create an issue or reach us at [support@usetusk.ai](mailto:support@usetusk.ai).
326
63
 
327
- 2. **Test with simple endpoint**: Start with endpoints that only return static data
64
+ ## Contributing
328
65
 
329
- 3. **Check dependencies**: Verify you're using supported package versions
66
+ We appreciate feedback and contributions. See [CONTRIBUTING.md](/CONTRIBUTING.md).
330
67
 
331
68
  ## License
332
69