repro-nest 0.0.211 → 0.0.213
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/dist/index.js +73 -28
- package/package.json +1 -1
- package/src/index.ts +80 -30
- package/tracer/runtime.js +0 -34
package/dist/index.js
CHANGED
|
@@ -103,6 +103,21 @@ function patchMongooseExecCapture(targetMongoose = mongoose) {
|
|
|
103
103
|
Qp.exec = function reproPatchedExec(...args) {
|
|
104
104
|
try {
|
|
105
105
|
this.__repro_is_query = true;
|
|
106
|
+
const ctx = __TRACER__?.getCurrentSpanContext?.();
|
|
107
|
+
const traceId = ctx?.traceId ?? __TRACER__?.getCurrentTraceId?.() ?? null;
|
|
108
|
+
if (ctx || traceId) {
|
|
109
|
+
Object.defineProperty(this, '__repro_span_context', {
|
|
110
|
+
value: {
|
|
111
|
+
traceId,
|
|
112
|
+
spanId: ctx?.spanId ?? null,
|
|
113
|
+
parentSpanId: ctx?.parentSpanId ?? null,
|
|
114
|
+
depth: ctx?.depth ?? null,
|
|
115
|
+
},
|
|
116
|
+
configurable: true,
|
|
117
|
+
writable: true,
|
|
118
|
+
enumerable: false,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
106
121
|
}
|
|
107
122
|
catch { }
|
|
108
123
|
const p = origExec.apply(this, args);
|
|
@@ -139,6 +154,21 @@ function patchMongooseExecCapture(targetMongoose = mongoose) {
|
|
|
139
154
|
Ap.exec = function reproPatchedAggExec(...args) {
|
|
140
155
|
try {
|
|
141
156
|
this.__repro_is_query = true;
|
|
157
|
+
const ctx = __TRACER__?.getCurrentSpanContext?.();
|
|
158
|
+
const traceId = ctx?.traceId ?? __TRACER__?.getCurrentTraceId?.() ?? null;
|
|
159
|
+
if (ctx || traceId) {
|
|
160
|
+
Object.defineProperty(this, '__repro_span_context', {
|
|
161
|
+
value: {
|
|
162
|
+
traceId,
|
|
163
|
+
spanId: ctx?.spanId ?? null,
|
|
164
|
+
parentSpanId: ctx?.parentSpanId ?? null,
|
|
165
|
+
depth: ctx?.depth ?? null,
|
|
166
|
+
},
|
|
167
|
+
configurable: true,
|
|
168
|
+
writable: true,
|
|
169
|
+
enumerable: false,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
142
172
|
}
|
|
143
173
|
catch { }
|
|
144
174
|
const p = origAggExec.apply(this, args);
|
|
@@ -459,17 +489,36 @@ exports.initReproTracing = initReproTracing;
|
|
|
459
489
|
/** Optional helper if users want to check it. */
|
|
460
490
|
function isReproTracingEnabled() { return __TRACER_READY; }
|
|
461
491
|
exports.isReproTracingEnabled = isReproTracingEnabled;
|
|
462
|
-
function
|
|
492
|
+
function captureSpanContextFromTracer(source) {
|
|
463
493
|
try {
|
|
494
|
+
// Prefer a preserved store captured at the call-site for thenables (e.g., Mongoose Query),
|
|
495
|
+
// because the tracer intentionally detaches spans before the query is actually executed.
|
|
496
|
+
const promiseStore = source && source[Symbol.for('__repro_promise_store')];
|
|
497
|
+
if (promiseStore) {
|
|
498
|
+
const stack = Array.isArray(promiseStore.__repro_span_stack) ? promiseStore.__repro_span_stack : [];
|
|
499
|
+
const top = stack.length ? stack[stack.length - 1] : null;
|
|
500
|
+
const spanId = top && top.id != null ? top.id : null;
|
|
501
|
+
const parentSpanId = top && top.parentId != null
|
|
502
|
+
? top.parentId
|
|
503
|
+
: (stack.length >= 2 ? (stack[stack.length - 2]?.id ?? null) : null);
|
|
504
|
+
const depth = top && top.depth != null
|
|
505
|
+
? top.depth
|
|
506
|
+
: (typeof promiseStore.depth === 'number'
|
|
507
|
+
? promiseStore.depth
|
|
508
|
+
: (stack.length ? stack.length : null));
|
|
509
|
+
const traceId = promiseStore.traceId ?? null;
|
|
510
|
+
if (traceId || spanId !== null || parentSpanId !== null) {
|
|
511
|
+
return { traceId, spanId, parentSpanId, depth: depth == null ? null : depth };
|
|
512
|
+
}
|
|
513
|
+
}
|
|
464
514
|
const fromSource = source && source.__repro_span_context;
|
|
465
515
|
if (fromSource) {
|
|
466
|
-
|
|
516
|
+
return {
|
|
467
517
|
traceId: fromSource.traceId ?? null,
|
|
468
518
|
spanId: fromSource.spanId ?? null,
|
|
469
519
|
parentSpanId: fromSource.parentSpanId ?? null,
|
|
470
520
|
depth: fromSource.depth ?? null,
|
|
471
521
|
};
|
|
472
|
-
return span;
|
|
473
522
|
}
|
|
474
523
|
const ctx = __TRACER__?.getCurrentSpanContext?.();
|
|
475
524
|
if (ctx) {
|
|
@@ -493,10 +542,10 @@ function captureSpanContext(source) {
|
|
|
493
542
|
catch { }
|
|
494
543
|
return null;
|
|
495
544
|
}
|
|
496
|
-
function attachSpanContext(target,
|
|
545
|
+
function attachSpanContext(target, span) {
|
|
497
546
|
if (!target)
|
|
498
547
|
return target;
|
|
499
|
-
const ctx =
|
|
548
|
+
const ctx = span ?? captureSpanContextFromTracer();
|
|
500
549
|
if (ctx) {
|
|
501
550
|
try {
|
|
502
551
|
target.spanContext = ctx;
|
|
@@ -1582,7 +1631,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1582
1631
|
wasNew: this.isNew,
|
|
1583
1632
|
before,
|
|
1584
1633
|
collection: resolveCollectionOrWarn(this, 'doc'),
|
|
1585
|
-
spanContext:
|
|
1634
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1586
1635
|
};
|
|
1587
1636
|
next();
|
|
1588
1637
|
});
|
|
@@ -1596,7 +1645,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1596
1645
|
const before = meta.before ?? null;
|
|
1597
1646
|
const after = this.toObject({ depopulate: true });
|
|
1598
1647
|
const collection = meta.collection || resolveCollectionOrWarn(this, 'doc');
|
|
1599
|
-
const spanContext =
|
|
1648
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
1600
1649
|
const query = meta.wasNew
|
|
1601
1650
|
? { op: 'insertOne', doc: after }
|
|
1602
1651
|
: { filter: { _id: this._id }, update: buildMinimalUpdate(before, after), options: { upsert: false } };
|
|
@@ -1626,7 +1675,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1626
1675
|
this.__repro_before = await model.findOne(filter).lean().exec();
|
|
1627
1676
|
this.setOptions({ new: true });
|
|
1628
1677
|
this.__repro_collection = resolveCollectionOrWarn(this, 'query');
|
|
1629
|
-
this.__repro_spanContext =
|
|
1678
|
+
this.__repro_spanContext = captureSpanContextFromTracer(this);
|
|
1630
1679
|
}
|
|
1631
1680
|
catch { }
|
|
1632
1681
|
next();
|
|
@@ -1638,7 +1687,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1638
1687
|
const before = this.__repro_before ?? null;
|
|
1639
1688
|
const after = res ?? null;
|
|
1640
1689
|
const collection = this.__repro_collection || resolveCollectionOrWarn(this, 'query');
|
|
1641
|
-
const spanContext =
|
|
1690
|
+
const spanContext = this.__repro_spanContext || captureSpanContextFromTracer(this);
|
|
1642
1691
|
const pk = after?._id ?? before?._id;
|
|
1643
1692
|
post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, getCtx().sid, {
|
|
1644
1693
|
entries: [{
|
|
@@ -1664,7 +1713,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1664
1713
|
this.__repro_before = await this.model.findOne(filter).lean().exec();
|
|
1665
1714
|
this.__repro_collection = resolveCollectionOrWarn(this, 'query');
|
|
1666
1715
|
this.__repro_filter = filter;
|
|
1667
|
-
this.__repro_spanContext =
|
|
1716
|
+
this.__repro_spanContext = captureSpanContextFromTracer(this);
|
|
1668
1717
|
}
|
|
1669
1718
|
catch { }
|
|
1670
1719
|
next();
|
|
@@ -1678,7 +1727,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1678
1727
|
return;
|
|
1679
1728
|
const collection = this.__repro_collection || resolveCollectionOrWarn(this, 'query');
|
|
1680
1729
|
const filter = this.__repro_filter ?? { _id: before._id };
|
|
1681
|
-
const spanContext =
|
|
1730
|
+
const spanContext = this.__repro_spanContext || captureSpanContextFromTracer(this);
|
|
1682
1731
|
post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, getCtx().sid, {
|
|
1683
1732
|
entries: [{
|
|
1684
1733
|
actionId: getCtx().aid,
|
|
@@ -1727,7 +1776,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1727
1776
|
t0: Date.now(),
|
|
1728
1777
|
collection: this?.model?.collection?.name || 'unknown',
|
|
1729
1778
|
op,
|
|
1730
|
-
spanContext:
|
|
1779
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1731
1780
|
filter: sanitizeDbValue(this.getFilter?.() ?? this._conditions ?? undefined),
|
|
1732
1781
|
update: sanitizeDbValue(this.getUpdate?.() ?? this._update ?? undefined),
|
|
1733
1782
|
projection: sanitizeDbValue(this.projection?.() ?? this._fields ?? undefined),
|
|
@@ -1739,7 +1788,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1739
1788
|
t0: Date.now(),
|
|
1740
1789
|
collection: 'unknown',
|
|
1741
1790
|
op,
|
|
1742
|
-
spanContext:
|
|
1791
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1743
1792
|
};
|
|
1744
1793
|
}
|
|
1745
1794
|
next();
|
|
@@ -1750,7 +1799,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1750
1799
|
return;
|
|
1751
1800
|
const meta = this.__repro_qmeta || { t0: Date.now(), collection: 'unknown', op };
|
|
1752
1801
|
const resultMeta = summarizeQueryResult(op, res);
|
|
1753
|
-
const spanContext =
|
|
1802
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
1754
1803
|
emitDbQuery(cfg, sid, aid, {
|
|
1755
1804
|
collection: meta.collection,
|
|
1756
1805
|
op,
|
|
@@ -1759,7 +1808,6 @@ function reproMongoosePlugin(cfg) {
|
|
|
1759
1808
|
durMs: Date.now() - meta.t0,
|
|
1760
1809
|
t: alignedNow(),
|
|
1761
1810
|
spanContext,
|
|
1762
|
-
spanSource: this,
|
|
1763
1811
|
});
|
|
1764
1812
|
});
|
|
1765
1813
|
}
|
|
@@ -1772,14 +1820,14 @@ function reproMongoosePlugin(cfg) {
|
|
|
1772
1820
|
t0: Date.now(),
|
|
1773
1821
|
collection: this?.collection?.name || this?.model?.collection?.name || 'unknown',
|
|
1774
1822
|
docs: sanitizeDbValue(docs),
|
|
1775
|
-
spanContext:
|
|
1823
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1776
1824
|
};
|
|
1777
1825
|
}
|
|
1778
1826
|
catch {
|
|
1779
1827
|
this.__repro_insert_meta = {
|
|
1780
1828
|
t0: Date.now(),
|
|
1781
1829
|
collection: 'unknown',
|
|
1782
|
-
spanContext:
|
|
1830
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1783
1831
|
};
|
|
1784
1832
|
}
|
|
1785
1833
|
next();
|
|
@@ -1790,7 +1838,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1790
1838
|
return;
|
|
1791
1839
|
const meta = this.__repro_insert_meta || { t0: Date.now(), collection: 'unknown' };
|
|
1792
1840
|
const resultMeta = Array.isArray(docs) ? { inserted: docs.length } : summarizeQueryResult('insertMany', docs);
|
|
1793
|
-
const spanContext =
|
|
1841
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
1794
1842
|
emitDbQuery(cfg, sid, aid, {
|
|
1795
1843
|
collection: meta.collection,
|
|
1796
1844
|
op: 'insertMany',
|
|
@@ -1799,7 +1847,6 @@ function reproMongoosePlugin(cfg) {
|
|
|
1799
1847
|
durMs: Date.now() - meta.t0,
|
|
1800
1848
|
t: alignedNow(),
|
|
1801
1849
|
spanContext,
|
|
1802
|
-
spanSource: this,
|
|
1803
1850
|
});
|
|
1804
1851
|
});
|
|
1805
1852
|
schema.pre('bulkWrite', { document: false, query: false }, function (next, ops) {
|
|
@@ -1808,14 +1855,14 @@ function reproMongoosePlugin(cfg) {
|
|
|
1808
1855
|
t0: Date.now(),
|
|
1809
1856
|
collection: this?.collection?.name || this?.model?.collection?.name || 'unknown',
|
|
1810
1857
|
ops: sanitizeDbValue(ops),
|
|
1811
|
-
spanContext:
|
|
1858
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1812
1859
|
};
|
|
1813
1860
|
}
|
|
1814
1861
|
catch {
|
|
1815
1862
|
this.__repro_bulk_meta = {
|
|
1816
1863
|
t0: Date.now(),
|
|
1817
1864
|
collection: 'unknown',
|
|
1818
|
-
spanContext:
|
|
1865
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1819
1866
|
};
|
|
1820
1867
|
}
|
|
1821
1868
|
next();
|
|
@@ -1827,7 +1874,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1827
1874
|
const meta = this.__repro_bulk_meta || { t0: Date.now(), collection: 'unknown' };
|
|
1828
1875
|
const bulkResult = summarizeBulkResult(res);
|
|
1829
1876
|
const resultMeta = { ...bulkResult, result: sanitizeResultForMeta(res?.result ?? res) };
|
|
1830
|
-
const spanContext =
|
|
1877
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
1831
1878
|
emitDbQuery(cfg, sid, aid, {
|
|
1832
1879
|
collection: meta.collection,
|
|
1833
1880
|
op: 'bulkWrite',
|
|
@@ -1836,7 +1883,6 @@ function reproMongoosePlugin(cfg) {
|
|
|
1836
1883
|
durMs: Date.now() - meta.t0,
|
|
1837
1884
|
t: alignedNow(),
|
|
1838
1885
|
spanContext,
|
|
1839
|
-
spanSource: this,
|
|
1840
1886
|
});
|
|
1841
1887
|
});
|
|
1842
1888
|
// Aggregate middleware (non-intrusive)
|
|
@@ -1848,7 +1894,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1848
1894
|
this?._model?.collection?.name ||
|
|
1849
1895
|
(this?.model && this.model.collection?.name) ||
|
|
1850
1896
|
'unknown',
|
|
1851
|
-
spanContext:
|
|
1897
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1852
1898
|
pipeline: sanitizeDbValue(this.pipeline?.() ?? this._pipeline ?? undefined),
|
|
1853
1899
|
};
|
|
1854
1900
|
}
|
|
@@ -1857,7 +1903,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1857
1903
|
t0: Date.now(),
|
|
1858
1904
|
collection: 'unknown',
|
|
1859
1905
|
pipeline: undefined,
|
|
1860
|
-
spanContext:
|
|
1906
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1861
1907
|
};
|
|
1862
1908
|
}
|
|
1863
1909
|
next();
|
|
@@ -1868,7 +1914,7 @@ function reproMongoosePlugin(cfg) {
|
|
|
1868
1914
|
return;
|
|
1869
1915
|
const meta = this.__repro_aggmeta || { t0: Date.now(), collection: 'unknown' };
|
|
1870
1916
|
const resultMeta = summarizeQueryResult('aggregate', res);
|
|
1871
|
-
const spanContext =
|
|
1917
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
1872
1918
|
emitDbQuery(cfg, sid, aid, {
|
|
1873
1919
|
collection: meta.collection,
|
|
1874
1920
|
op: 'aggregate',
|
|
@@ -1877,7 +1923,6 @@ function reproMongoosePlugin(cfg) {
|
|
|
1877
1923
|
durMs: Date.now() - meta.t0,
|
|
1878
1924
|
t: alignedNow(),
|
|
1879
1925
|
spanContext,
|
|
1880
|
-
spanSource: this,
|
|
1881
1926
|
});
|
|
1882
1927
|
});
|
|
1883
1928
|
};
|
|
@@ -2002,7 +2047,7 @@ function emitDbQuery(cfg, sid, aid, payload) {
|
|
|
2002
2047
|
durMs: payload.durMs ?? undefined,
|
|
2003
2048
|
pk: null, before: null, after: null,
|
|
2004
2049
|
error: payload.error ?? undefined,
|
|
2005
|
-
}, payload?.spanContext
|
|
2050
|
+
}, payload?.spanContext);
|
|
2006
2051
|
post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, sid, {
|
|
2007
2052
|
entries: [{
|
|
2008
2053
|
actionId: aid ?? null,
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -95,7 +95,24 @@ function patchMongooseExecCapture(targetMongoose: any = mongoose) {
|
|
|
95
95
|
if (typeof origExec === 'function') {
|
|
96
96
|
Qp.__repro_exec_patched = true;
|
|
97
97
|
Qp.exec = function reproPatchedExec(this: any, ...args: any[]) {
|
|
98
|
-
try {
|
|
98
|
+
try {
|
|
99
|
+
(this as any).__repro_is_query = true;
|
|
100
|
+
const ctx = __TRACER__?.getCurrentSpanContext?.();
|
|
101
|
+
const traceId = ctx?.traceId ?? __TRACER__?.getCurrentTraceId?.() ?? null;
|
|
102
|
+
if (ctx || traceId) {
|
|
103
|
+
Object.defineProperty(this, '__repro_span_context', {
|
|
104
|
+
value: {
|
|
105
|
+
traceId,
|
|
106
|
+
spanId: ctx?.spanId ?? null,
|
|
107
|
+
parentSpanId: ctx?.parentSpanId ?? null,
|
|
108
|
+
depth: ctx?.depth ?? null,
|
|
109
|
+
},
|
|
110
|
+
configurable: true,
|
|
111
|
+
writable: true,
|
|
112
|
+
enumerable: false,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
} catch {}
|
|
99
116
|
const p = origExec.apply(this, args);
|
|
100
117
|
try {
|
|
101
118
|
if (p && typeof p.then === 'function') {
|
|
@@ -127,7 +144,24 @@ function patchMongooseExecCapture(targetMongoose: any = mongoose) {
|
|
|
127
144
|
if (typeof origAggExec === 'function') {
|
|
128
145
|
Ap.__repro_agg_exec_patched = true;
|
|
129
146
|
Ap.exec = function reproPatchedAggExec(this: any, ...args: any[]) {
|
|
130
|
-
try {
|
|
147
|
+
try {
|
|
148
|
+
(this as any).__repro_is_query = true;
|
|
149
|
+
const ctx = __TRACER__?.getCurrentSpanContext?.();
|
|
150
|
+
const traceId = ctx?.traceId ?? __TRACER__?.getCurrentTraceId?.() ?? null;
|
|
151
|
+
if (ctx || traceId) {
|
|
152
|
+
Object.defineProperty(this, '__repro_span_context', {
|
|
153
|
+
value: {
|
|
154
|
+
traceId,
|
|
155
|
+
spanId: ctx?.spanId ?? null,
|
|
156
|
+
parentSpanId: ctx?.parentSpanId ?? null,
|
|
157
|
+
depth: ctx?.depth ?? null,
|
|
158
|
+
},
|
|
159
|
+
configurable: true,
|
|
160
|
+
writable: true,
|
|
161
|
+
enumerable: false,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
} catch {}
|
|
131
165
|
const p = origAggExec.apply(this, args);
|
|
132
166
|
try {
|
|
133
167
|
if (p && typeof p.then === 'function') {
|
|
@@ -648,17 +682,37 @@ export function initReproTracing(opts?: ReproTracingInitOptions) {
|
|
|
648
682
|
/** Optional helper if users want to check it. */
|
|
649
683
|
export function isReproTracingEnabled() { return __TRACER_READY; }
|
|
650
684
|
|
|
651
|
-
function
|
|
685
|
+
function captureSpanContextFromTracer(source?: any): SpanContext | null {
|
|
652
686
|
try {
|
|
687
|
+
// Prefer a preserved store captured at the call-site for thenables (e.g., Mongoose Query),
|
|
688
|
+
// because the tracer intentionally detaches spans before the query is actually executed.
|
|
689
|
+
const promiseStore = source && source[Symbol.for('__repro_promise_store')];
|
|
690
|
+
if (promiseStore) {
|
|
691
|
+
const stack = Array.isArray(promiseStore.__repro_span_stack) ? promiseStore.__repro_span_stack : [];
|
|
692
|
+
const top = stack.length ? stack[stack.length - 1] : null;
|
|
693
|
+
const spanId = top && top.id != null ? top.id : null;
|
|
694
|
+
const parentSpanId = top && top.parentId != null
|
|
695
|
+
? top.parentId
|
|
696
|
+
: (stack.length >= 2 ? (stack[stack.length - 2]?.id ?? null) : null);
|
|
697
|
+
const depth = top && top.depth != null
|
|
698
|
+
? top.depth
|
|
699
|
+
: (typeof promiseStore.depth === 'number'
|
|
700
|
+
? promiseStore.depth
|
|
701
|
+
: (stack.length ? stack.length : null));
|
|
702
|
+
const traceId = promiseStore.traceId ?? null;
|
|
703
|
+
if (traceId || spanId !== null || parentSpanId !== null) {
|
|
704
|
+
return { traceId, spanId, parentSpanId, depth: depth == null ? null : depth };
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
653
708
|
const fromSource = source && source.__repro_span_context;
|
|
654
709
|
if (fromSource) {
|
|
655
|
-
|
|
710
|
+
return {
|
|
656
711
|
traceId: fromSource.traceId ?? null,
|
|
657
712
|
spanId: fromSource.spanId ?? null,
|
|
658
713
|
parentSpanId: fromSource.parentSpanId ?? null,
|
|
659
714
|
depth: fromSource.depth ?? null,
|
|
660
715
|
};
|
|
661
|
-
return span;
|
|
662
716
|
}
|
|
663
717
|
|
|
664
718
|
const ctx = __TRACER__?.getCurrentSpanContext?.();
|
|
@@ -682,9 +736,9 @@ function captureSpanContext(source?: any): SpanContext | null {
|
|
|
682
736
|
return null;
|
|
683
737
|
}
|
|
684
738
|
|
|
685
|
-
function attachSpanContext<T extends Record<string, any>>(target: T,
|
|
739
|
+
function attachSpanContext<T extends Record<string, any>>(target: T, span?: SpanContext | null): T {
|
|
686
740
|
if (!target) return target;
|
|
687
|
-
const ctx =
|
|
741
|
+
const ctx = span ?? captureSpanContextFromTracer();
|
|
688
742
|
if (ctx) {
|
|
689
743
|
try { (target as any).spanContext = ctx; } catch {}
|
|
690
744
|
}
|
|
@@ -1796,7 +1850,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1796
1850
|
wasNew: this.isNew,
|
|
1797
1851
|
before,
|
|
1798
1852
|
collection: resolveCollectionOrWarn(this, 'doc'),
|
|
1799
|
-
spanContext:
|
|
1853
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1800
1854
|
};
|
|
1801
1855
|
next();
|
|
1802
1856
|
});
|
|
@@ -1810,7 +1864,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1810
1864
|
const before = meta.before ?? null;
|
|
1811
1865
|
const after = this.toObject({ depopulate: true });
|
|
1812
1866
|
const collection = meta.collection || resolveCollectionOrWarn(this, 'doc');
|
|
1813
|
-
const spanContext =
|
|
1867
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
1814
1868
|
|
|
1815
1869
|
const query = meta.wasNew
|
|
1816
1870
|
? { op: 'insertOne', doc: after }
|
|
@@ -1842,7 +1896,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1842
1896
|
(this as any).__repro_before = await model.findOne(filter).lean().exec();
|
|
1843
1897
|
this.setOptions({ new: true });
|
|
1844
1898
|
(this as any).__repro_collection = resolveCollectionOrWarn(this, 'query');
|
|
1845
|
-
(this as any).__repro_spanContext =
|
|
1899
|
+
(this as any).__repro_spanContext = captureSpanContextFromTracer(this);
|
|
1846
1900
|
} catch {}
|
|
1847
1901
|
next();
|
|
1848
1902
|
});
|
|
@@ -1854,7 +1908,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1854
1908
|
const before = (this as any).__repro_before ?? null;
|
|
1855
1909
|
const after = res ?? null;
|
|
1856
1910
|
const collection = (this as any).__repro_collection || resolveCollectionOrWarn(this, 'query');
|
|
1857
|
-
const spanContext =
|
|
1911
|
+
const spanContext = (this as any).__repro_spanContext || captureSpanContextFromTracer(this);
|
|
1858
1912
|
const pk = after?._id ?? before?._id;
|
|
1859
1913
|
|
|
1860
1914
|
post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, (getCtx() as Ctx).sid!, {
|
|
@@ -1880,7 +1934,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1880
1934
|
(this as any).__repro_before = await (this.model as Model<any>).findOne(filter).lean().exec();
|
|
1881
1935
|
(this as any).__repro_collection = resolveCollectionOrWarn(this, 'query');
|
|
1882
1936
|
(this as any).__repro_filter = filter;
|
|
1883
|
-
(this as any).__repro_spanContext =
|
|
1937
|
+
(this as any).__repro_spanContext = captureSpanContextFromTracer(this);
|
|
1884
1938
|
} catch {}
|
|
1885
1939
|
next();
|
|
1886
1940
|
});
|
|
@@ -1891,7 +1945,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1891
1945
|
if (!before) return;
|
|
1892
1946
|
const collection = (this as any).__repro_collection || resolveCollectionOrWarn(this, 'query');
|
|
1893
1947
|
const filter = (this as any).__repro_filter ?? { _id: before._id };
|
|
1894
|
-
const spanContext =
|
|
1948
|
+
const spanContext = (this as any).__repro_spanContext || captureSpanContextFromTracer(this);
|
|
1895
1949
|
post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, (getCtx() as Ctx).sid!, {
|
|
1896
1950
|
entries: [{
|
|
1897
1951
|
actionId: (getCtx() as Ctx).aid!,
|
|
@@ -1944,7 +1998,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1944
1998
|
t0: Date.now(),
|
|
1945
1999
|
collection: this?.model?.collection?.name || 'unknown',
|
|
1946
2000
|
op,
|
|
1947
|
-
spanContext:
|
|
2001
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1948
2002
|
filter: sanitizeDbValue(this.getFilter?.() ?? this._conditions ?? undefined),
|
|
1949
2003
|
update: sanitizeDbValue(this.getUpdate?.() ?? this._update ?? undefined),
|
|
1950
2004
|
projection: sanitizeDbValue(this.projection?.() ?? this._fields ?? undefined),
|
|
@@ -1955,7 +2009,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1955
2009
|
t0: Date.now(),
|
|
1956
2010
|
collection: 'unknown',
|
|
1957
2011
|
op,
|
|
1958
|
-
spanContext:
|
|
2012
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1959
2013
|
};
|
|
1960
2014
|
}
|
|
1961
2015
|
next();
|
|
@@ -1967,7 +2021,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1967
2021
|
|
|
1968
2022
|
const meta = (this as any).__repro_qmeta || { t0: Date.now(), collection: 'unknown', op };
|
|
1969
2023
|
const resultMeta = summarizeQueryResult(op, res);
|
|
1970
|
-
const spanContext =
|
|
2024
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
1971
2025
|
|
|
1972
2026
|
emitDbQuery(cfg, sid, aid, {
|
|
1973
2027
|
collection: meta.collection,
|
|
@@ -1977,7 +2031,6 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1977
2031
|
durMs: Date.now() - meta.t0,
|
|
1978
2032
|
t: alignedNow(),
|
|
1979
2033
|
spanContext,
|
|
1980
|
-
spanSource: this,
|
|
1981
2034
|
});
|
|
1982
2035
|
});
|
|
1983
2036
|
}
|
|
@@ -1992,13 +2045,13 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
1992
2045
|
t0: Date.now(),
|
|
1993
2046
|
collection: this?.collection?.name || this?.model?.collection?.name || 'unknown',
|
|
1994
2047
|
docs: sanitizeDbValue(docs),
|
|
1995
|
-
spanContext:
|
|
2048
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
1996
2049
|
};
|
|
1997
2050
|
} catch {
|
|
1998
2051
|
(this as any).__repro_insert_meta = {
|
|
1999
2052
|
t0: Date.now(),
|
|
2000
2053
|
collection: 'unknown',
|
|
2001
|
-
spanContext:
|
|
2054
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
2002
2055
|
};
|
|
2003
2056
|
}
|
|
2004
2057
|
next();
|
|
@@ -2009,7 +2062,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2009
2062
|
if (!sid) return;
|
|
2010
2063
|
const meta = (this as any).__repro_insert_meta || { t0: Date.now(), collection: 'unknown' };
|
|
2011
2064
|
const resultMeta = Array.isArray(docs) ? { inserted: docs.length } : summarizeQueryResult('insertMany', docs);
|
|
2012
|
-
const spanContext =
|
|
2065
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
2013
2066
|
|
|
2014
2067
|
emitDbQuery(cfg, sid, aid, {
|
|
2015
2068
|
collection: meta.collection,
|
|
@@ -2019,7 +2072,6 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2019
2072
|
durMs: Date.now() - meta.t0,
|
|
2020
2073
|
t: alignedNow(),
|
|
2021
2074
|
spanContext,
|
|
2022
|
-
spanSource: this,
|
|
2023
2075
|
});
|
|
2024
2076
|
} as any);
|
|
2025
2077
|
|
|
@@ -2029,13 +2081,13 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2029
2081
|
t0: Date.now(),
|
|
2030
2082
|
collection: this?.collection?.name || this?.model?.collection?.name || 'unknown',
|
|
2031
2083
|
ops: sanitizeDbValue(ops),
|
|
2032
|
-
spanContext:
|
|
2084
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
2033
2085
|
};
|
|
2034
2086
|
} catch {
|
|
2035
2087
|
(this as any).__repro_bulk_meta = {
|
|
2036
2088
|
t0: Date.now(),
|
|
2037
2089
|
collection: 'unknown',
|
|
2038
|
-
spanContext:
|
|
2090
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
2039
2091
|
};
|
|
2040
2092
|
}
|
|
2041
2093
|
next();
|
|
@@ -2047,7 +2099,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2047
2099
|
const meta = (this as any).__repro_bulk_meta || { t0: Date.now(), collection: 'unknown' };
|
|
2048
2100
|
const bulkResult = summarizeBulkResult(res);
|
|
2049
2101
|
const resultMeta = { ...bulkResult, result: sanitizeResultForMeta(res?.result ?? res) };
|
|
2050
|
-
const spanContext =
|
|
2102
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
2051
2103
|
|
|
2052
2104
|
emitDbQuery(cfg, sid, aid, {
|
|
2053
2105
|
collection: meta.collection,
|
|
@@ -2057,7 +2109,6 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2057
2109
|
durMs: Date.now() - meta.t0,
|
|
2058
2110
|
t: alignedNow(),
|
|
2059
2111
|
spanContext,
|
|
2060
|
-
spanSource: this,
|
|
2061
2112
|
});
|
|
2062
2113
|
} as any);
|
|
2063
2114
|
|
|
@@ -2071,7 +2122,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2071
2122
|
this?._model?.collection?.name ||
|
|
2072
2123
|
(this?.model && this.model.collection?.name) ||
|
|
2073
2124
|
'unknown',
|
|
2074
|
-
spanContext:
|
|
2125
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
2075
2126
|
pipeline: sanitizeDbValue(this.pipeline?.() ?? this._pipeline ?? undefined),
|
|
2076
2127
|
};
|
|
2077
2128
|
} catch {
|
|
@@ -2079,7 +2130,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2079
2130
|
t0: Date.now(),
|
|
2080
2131
|
collection: 'unknown',
|
|
2081
2132
|
pipeline: undefined,
|
|
2082
|
-
spanContext:
|
|
2133
|
+
spanContext: captureSpanContextFromTracer(this),
|
|
2083
2134
|
};
|
|
2084
2135
|
}
|
|
2085
2136
|
next();
|
|
@@ -2091,7 +2142,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2091
2142
|
|
|
2092
2143
|
const meta = (this as any).__repro_aggmeta || { t0: Date.now(), collection: 'unknown' };
|
|
2093
2144
|
const resultMeta = summarizeQueryResult('aggregate', res);
|
|
2094
|
-
const spanContext =
|
|
2145
|
+
const spanContext = meta.spanContext || captureSpanContextFromTracer(this);
|
|
2095
2146
|
|
|
2096
2147
|
emitDbQuery(cfg, sid, aid, {
|
|
2097
2148
|
collection: meta.collection,
|
|
@@ -2101,7 +2152,6 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
|
|
|
2101
2152
|
durMs: Date.now() - meta.t0,
|
|
2102
2153
|
t: alignedNow(),
|
|
2103
2154
|
spanContext,
|
|
2104
|
-
spanSource: this,
|
|
2105
2155
|
});
|
|
2106
2156
|
});
|
|
2107
2157
|
};
|
|
@@ -2219,7 +2269,7 @@ function emitDbQuery(cfg: any, sid?: string, aid?: string, payload?: any) {
|
|
|
2219
2269
|
durMs: payload.durMs ?? undefined,
|
|
2220
2270
|
pk: null, before: null, after: null,
|
|
2221
2271
|
error: payload.error ?? undefined,
|
|
2222
|
-
}, payload?.spanContext
|
|
2272
|
+
}, payload?.spanContext);
|
|
2223
2273
|
post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, sid, {
|
|
2224
2274
|
entries: [{
|
|
2225
2275
|
actionId: aid ?? null,
|
package/tracer/runtime.js
CHANGED
|
@@ -262,12 +262,6 @@ const trace = {
|
|
|
262
262
|
return;
|
|
263
263
|
}
|
|
264
264
|
if (queueQueryFinalizer(rv, finalize)) return;
|
|
265
|
-
setQuerySpanContext(rv, {
|
|
266
|
-
traceId: traceIdAtExit,
|
|
267
|
-
spanId: spanForExit.id,
|
|
268
|
-
parentSpanId: spanForExit.parentId,
|
|
269
|
-
depth: spanForExit.depth ?? depthAtExit
|
|
270
|
-
});
|
|
271
265
|
emitNow({ unawaited: forceUnawaited, returnValue: rv }, spanForExit, spanStackForExit);
|
|
272
266
|
return;
|
|
273
267
|
}
|
|
@@ -278,12 +272,6 @@ const trace = {
|
|
|
278
272
|
}
|
|
279
273
|
|
|
280
274
|
if (isQuery) {
|
|
281
|
-
setQuerySpanContext(rv, {
|
|
282
|
-
traceId: traceIdAtExit,
|
|
283
|
-
spanId: spanInfoPeek.id,
|
|
284
|
-
parentSpanId: spanInfoPeek.parentId,
|
|
285
|
-
depth: spanInfoPeek.depth ?? depthAtExit
|
|
286
|
-
});
|
|
287
275
|
emitNow({ unawaited: forceUnawaited });
|
|
288
276
|
return;
|
|
289
277
|
}
|
|
@@ -853,28 +841,6 @@ function getCurrentTraceId() {
|
|
|
853
841
|
return s && s.traceId || null;
|
|
854
842
|
}
|
|
855
843
|
|
|
856
|
-
function setQuerySpanContext(target, ctx) {
|
|
857
|
-
if (!target || typeof target !== 'object' || !ctx) return;
|
|
858
|
-
const safeCtx = {
|
|
859
|
-
traceId: ctx.traceId || null,
|
|
860
|
-
spanId: ctx.spanId ?? null,
|
|
861
|
-
parentSpanId: ctx.parentSpanId ?? null,
|
|
862
|
-
depth: ctx.depth ?? null
|
|
863
|
-
};
|
|
864
|
-
try {
|
|
865
|
-
if (!target.__repro_span_context) {
|
|
866
|
-
Object.defineProperty(target, '__repro_span_context', {
|
|
867
|
-
value: safeCtx,
|
|
868
|
-
configurable: true,
|
|
869
|
-
writable: true,
|
|
870
|
-
enumerable: false
|
|
871
|
-
});
|
|
872
|
-
}
|
|
873
|
-
} catch {
|
|
874
|
-
try { target.__repro_span_context = safeCtx; } catch {}
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
|
|
878
844
|
function getCurrentSpanContext() {
|
|
879
845
|
try {
|
|
880
846
|
const store = als.getStore();
|