taist 0.2.0 → 0.2.2

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.
Files changed (2) hide show
  1. package/instrument.js +45 -11
  2. package/package.json +1 -1
package/instrument.js CHANGED
@@ -163,6 +163,33 @@ export function instrumentExpress(app, options = {}) {
163
163
  const useContext = options.useContext !== false;
164
164
  const reporter = getGlobalReporter();
165
165
 
166
+ // Add early middleware to set up correlation ID for ALL requests
167
+ // This runs before any route handlers or other middleware
168
+ const debug = process.env.TAIST_DEBUG === 'true';
169
+ app.use((req, res, next) => {
170
+ // Only set up if not already set (avoid double-instrumentation)
171
+ if (!req.taistCorrelationId) {
172
+ const correlationId = generateId();
173
+ req.taistCorrelationId = correlationId;
174
+ setCorrelationId(correlationId);
175
+
176
+ if (debug) {
177
+ logger.log('[taist middleware] Set correlationId:', correlationId, 'for', req.method, req.path);
178
+ }
179
+
180
+ // Clear correlation ID when response finishes
181
+ res.on('finish', () => {
182
+ if (debug) {
183
+ logger.log('[taist middleware] Clearing correlationId for', req.method, req.path);
184
+ }
185
+ clearCorrelationId();
186
+ });
187
+ } else if (debug) {
188
+ logger.log('[taist middleware] Already has correlationId:', req.taistCorrelationId, 'for', req.method, req.path);
189
+ }
190
+ next();
191
+ });
192
+
166
193
  // Instrument route handlers
167
194
  ['get', 'post', 'put', 'delete', 'patch', 'options', 'head'].forEach(method => {
168
195
  const original = app[method];
@@ -176,14 +203,15 @@ export function instrumentExpress(app, options = {}) {
176
203
  return async function (req, res, next) {
177
204
  const traceName = `Route.${method.toUpperCase()} ${path}`;
178
205
  const id = generateId();
179
- const correlationId = generateId(); // Unique per HTTP request
206
+ // Use existing correlationId from early middleware, or create one
207
+ const correlationId = req.taistCorrelationId || generateId();
180
208
  const start = performance.now();
181
209
 
182
- // Store correlationId on req for Apollo/GraphQL integration
183
- req.taistCorrelationId = correlationId;
184
-
185
- // Set fallback correlation ID for frameworks that break AsyncLocalStorage
186
- setCorrelationId(correlationId);
210
+ // Ensure correlationId is on req (in case early middleware didn't run)
211
+ if (!req.taistCorrelationId) {
212
+ req.taistCorrelationId = correlationId;
213
+ setCorrelationId(correlationId);
214
+ }
187
215
 
188
216
  // Report function for success/error
189
217
  const reportResult = (error = null) => {
@@ -217,8 +245,7 @@ export function instrumentExpress(app, options = {}) {
217
245
  if (!responseSent) {
218
246
  responseSent = true;
219
247
  reportResult();
220
- // Clear correlation ID after request completes
221
- clearCorrelationId();
248
+ // Note: correlationId is cleared by early middleware's res.on('finish')
222
249
  }
223
250
  return originalEnd.apply(this, args);
224
251
  };
@@ -241,7 +268,6 @@ export function instrumentExpress(app, options = {}) {
241
268
  if (!responseSent) {
242
269
  responseSent = true;
243
270
  reportResult(err);
244
- clearCorrelationId();
245
271
  }
246
272
  throw err;
247
273
  }
@@ -253,7 +279,6 @@ export function instrumentExpress(app, options = {}) {
253
279
  if (!responseSent) {
254
280
  responseSent = true;
255
281
  reportResult(err);
256
- clearCorrelationId();
257
282
  }
258
283
  throw err;
259
284
  }
@@ -332,7 +357,16 @@ export function instrumentServiceWithContext(service, name) {
332
357
  * };
333
358
  */
334
359
  export function bridgeContext(req) {
335
- const correlationId = req?.taistCorrelationId || getCorrelationId();
360
+ const debug = process.env.TAIST_DEBUG === 'true';
361
+ const reqCorrelationId = req?.taistCorrelationId;
362
+ const fallbackCorrelationId = getCorrelationId();
363
+ const correlationId = reqCorrelationId || fallbackCorrelationId;
364
+
365
+ if (debug) {
366
+ logger.log('[bridgeContext] req.taistCorrelationId:', reqCorrelationId);
367
+ logger.log('[bridgeContext] getCorrelationId():', fallbackCorrelationId);
368
+ logger.log('[bridgeContext] using:', correlationId);
369
+ }
336
370
 
337
371
  // Also set the fallback so resolvers can access it even without context prop-drilling
338
372
  if (correlationId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "taist",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Token-Optimized Testing Framework for AI-Assisted Development",
5
5
  "main": "index.js",
6
6
  "type": "module",