trickle-observe 0.2.102 → 0.2.104

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.
@@ -61,6 +61,17 @@ function patchFetch(environment, debugMode) {
61
61
  if (url.includes('/api/ingest') || url.includes('/api/functions') || url.includes('/api/health')) {
62
62
  return originalFetch.call(globalThis, input, init);
63
63
  }
64
+ // Inject distributed trace headers
65
+ if (!init)
66
+ init = {};
67
+ if (!init.headers)
68
+ init.headers = {};
69
+ const traceId = globalThis.__trickle_trace_id || require('crypto').randomBytes(8).toString('hex');
70
+ globalThis.__trickle_trace_id = traceId;
71
+ if (typeof init.headers === 'object' && !Array.isArray(init.headers)) {
72
+ init.headers['X-Trickle-Trace-Id'] = traceId;
73
+ init.headers['X-Trickle-Service'] = process.env.TRICKLE_SERVICE_NAME || require('path').basename(process.cwd());
74
+ }
64
75
  // Make the actual request with timing
65
76
  const startTime = performance.now();
66
77
  const response = await originalFetch.call(globalThis, input, init);
@@ -71,6 +82,19 @@ function patchFetch(environment, debugMode) {
71
82
  if (!contentType.includes('json')) {
72
83
  return response;
73
84
  }
85
+ // Write distributed trace span
86
+ try {
87
+ const tracesDir = process.env.TRICKLE_LOCAL_DIR || require('path').join(process.cwd(), '.trickle');
88
+ const tracesFile = require('path').join(tracesDir, 'traces.jsonl');
89
+ const span = {
90
+ kind: 'trace', traceId, spanId: require('crypto').randomBytes(4).toString('hex'),
91
+ parentSpanId: '0', service: process.env.TRICKLE_SERVICE_NAME || require('path').basename(process.cwd()),
92
+ operation: `${method} ${url}`, durationMs, status: String(statusCode), timestamp: Date.now(),
93
+ metadata: { direction: 'outgoing' },
94
+ };
95
+ require('fs').appendFileSync(tracesFile, JSON.stringify(span) + '\n');
96
+ }
97
+ catch { }
74
98
  // Clone and intercept: read the clone's JSON in background
75
99
  try {
76
100
  const cloned = response.clone();
@@ -40,6 +40,7 @@ const wrap_1 = require("./wrap");
40
40
  const fetch_observer_1 = require("./fetch-observer");
41
41
  const express_1 = require("./express");
42
42
  const trace_var_1 = require("./trace-var");
43
+ const call_trace_1 = require("./call-trace");
43
44
  const vite_plugin_1 = require("./vite-plugin");
44
45
  // ── Source map support ──
45
46
  // Lightweight VLQ decoder for mapping compiled JS lines back to original TS lines
@@ -1016,6 +1017,8 @@ if (enabled) {
1016
1017
  if (process.env.TRICKLE_TRACE_VARS !== '0') {
1017
1018
  (0, trace_var_1.initVarTracer)({ debug });
1018
1019
  }
1020
+ // ── Hook 0b2: Initialize call trace ──
1021
+ (0, call_trace_1.initCallTrace)();
1019
1022
  // ── Hook 0c: Capture environment snapshot ──
1020
1023
  try {
1021
1024
  const envDir = process.env.TRICKLE_LOCAL_DIR || path_1.default.join(process.cwd(), '.trickle');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trickle-observe",
3
- "version": "0.2.102",
3
+ "version": "0.2.104",
4
4
  "description": "Runtime type observability for JavaScript applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -66,6 +66,16 @@ export function patchFetch(environment: string, debugMode: boolean): void {
66
66
  return originalFetch.call(globalThis, input, init);
67
67
  }
68
68
 
69
+ // Inject distributed trace headers
70
+ if (!init) init = {};
71
+ if (!init.headers) init.headers = {};
72
+ const traceId = (globalThis as any).__trickle_trace_id || require('crypto').randomBytes(8).toString('hex');
73
+ (globalThis as any).__trickle_trace_id = traceId;
74
+ if (typeof init.headers === 'object' && !Array.isArray(init.headers)) {
75
+ (init.headers as any)['X-Trickle-Trace-Id'] = traceId;
76
+ (init.headers as any)['X-Trickle-Service'] = process.env.TRICKLE_SERVICE_NAME || require('path').basename(process.cwd());
77
+ }
78
+
69
79
  // Make the actual request with timing
70
80
  const startTime = performance.now();
71
81
  const response = await originalFetch.call(globalThis, input, init);
@@ -78,6 +88,19 @@ export function patchFetch(environment: string, debugMode: boolean): void {
78
88
  return response;
79
89
  }
80
90
 
91
+ // Write distributed trace span
92
+ try {
93
+ const tracesDir = process.env.TRICKLE_LOCAL_DIR || require('path').join(process.cwd(), '.trickle');
94
+ const tracesFile = require('path').join(tracesDir, 'traces.jsonl');
95
+ const span = {
96
+ kind: 'trace', traceId, spanId: require('crypto').randomBytes(4).toString('hex'),
97
+ parentSpanId: '0', service: process.env.TRICKLE_SERVICE_NAME || require('path').basename(process.cwd()),
98
+ operation: `${method} ${url}`, durationMs, status: String(statusCode), timestamp: Date.now(),
99
+ metadata: { direction: 'outgoing' },
100
+ };
101
+ require('fs').appendFileSync(tracesFile, JSON.stringify(span) + '\n');
102
+ } catch {}
103
+
81
104
  // Clone and intercept: read the clone's JSON in background
82
105
  try {
83
106
  const cloned = response.clone();
@@ -37,6 +37,7 @@ import { WrapOptions } from './types';
37
37
  import { patchFetch } from './fetch-observer';
38
38
  import { instrumentExpress, trickleMiddleware } from './express';
39
39
  import { initVarTracer, traceVar } from './trace-var';
40
+ import { initCallTrace } from './call-trace';
40
41
  import {
41
42
  findReassignments,
42
43
  findForLoopVars,
@@ -1004,6 +1005,9 @@ if (enabled) {
1004
1005
  initVarTracer({ debug });
1005
1006
  }
1006
1007
 
1008
+ // ── Hook 0b2: Initialize call trace ──
1009
+ initCallTrace();
1010
+
1007
1011
  // ── Hook 0c: Capture environment snapshot ──
1008
1012
  try {
1009
1013
  const envDir = process.env.TRICKLE_LOCAL_DIR || path.join(process.cwd(), '.trickle');