repro-nest 0.0.209 → 0.0.211

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 CHANGED
@@ -459,8 +459,18 @@ exports.initReproTracing = initReproTracing;
459
459
  /** Optional helper if users want to check it. */
460
460
  function isReproTracingEnabled() { return __TRACER_READY; }
461
461
  exports.isReproTracingEnabled = isReproTracingEnabled;
462
- function captureSpanContextFromTracer() {
462
+ function captureSpanContext(source) {
463
463
  try {
464
+ const fromSource = source && source.__repro_span_context;
465
+ if (fromSource) {
466
+ const span = {
467
+ traceId: fromSource.traceId ?? null,
468
+ spanId: fromSource.spanId ?? null,
469
+ parentSpanId: fromSource.parentSpanId ?? null,
470
+ depth: fromSource.depth ?? null,
471
+ };
472
+ return span;
473
+ }
464
474
  const ctx = __TRACER__?.getCurrentSpanContext?.();
465
475
  if (ctx) {
466
476
  const span = {
@@ -483,10 +493,10 @@ function captureSpanContextFromTracer() {
483
493
  catch { }
484
494
  return null;
485
495
  }
486
- function attachSpanContext(target, span) {
496
+ function attachSpanContext(target, spanSource, fallbackSource) {
487
497
  if (!target)
488
498
  return target;
489
- const ctx = span ?? captureSpanContextFromTracer();
499
+ const ctx = spanSource ?? captureSpanContext(fallbackSource);
490
500
  if (ctx) {
491
501
  try {
492
502
  target.spanContext = ctx;
@@ -1572,7 +1582,7 @@ function reproMongoosePlugin(cfg) {
1572
1582
  wasNew: this.isNew,
1573
1583
  before,
1574
1584
  collection: resolveCollectionOrWarn(this, 'doc'),
1575
- spanContext: captureSpanContextFromTracer(),
1585
+ spanContext: captureSpanContext(this),
1576
1586
  };
1577
1587
  next();
1578
1588
  });
@@ -1586,7 +1596,7 @@ function reproMongoosePlugin(cfg) {
1586
1596
  const before = meta.before ?? null;
1587
1597
  const after = this.toObject({ depopulate: true });
1588
1598
  const collection = meta.collection || resolveCollectionOrWarn(this, 'doc');
1589
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
1599
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
1590
1600
  const query = meta.wasNew
1591
1601
  ? { op: 'insertOne', doc: after }
1592
1602
  : { filter: { _id: this._id }, update: buildMinimalUpdate(before, after), options: { upsert: false } };
@@ -1616,7 +1626,7 @@ function reproMongoosePlugin(cfg) {
1616
1626
  this.__repro_before = await model.findOne(filter).lean().exec();
1617
1627
  this.setOptions({ new: true });
1618
1628
  this.__repro_collection = resolveCollectionOrWarn(this, 'query');
1619
- this.__repro_spanContext = captureSpanContextFromTracer();
1629
+ this.__repro_spanContext = captureSpanContext(this);
1620
1630
  }
1621
1631
  catch { }
1622
1632
  next();
@@ -1628,7 +1638,7 @@ function reproMongoosePlugin(cfg) {
1628
1638
  const before = this.__repro_before ?? null;
1629
1639
  const after = res ?? null;
1630
1640
  const collection = this.__repro_collection || resolveCollectionOrWarn(this, 'query');
1631
- const spanContext = this.__repro_spanContext || captureSpanContextFromTracer();
1641
+ const spanContext = captureSpanContext(this) || this.__repro_spanContext || captureSpanContext();
1632
1642
  const pk = after?._id ?? before?._id;
1633
1643
  post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, getCtx().sid, {
1634
1644
  entries: [{
@@ -1654,7 +1664,7 @@ function reproMongoosePlugin(cfg) {
1654
1664
  this.__repro_before = await this.model.findOne(filter).lean().exec();
1655
1665
  this.__repro_collection = resolveCollectionOrWarn(this, 'query');
1656
1666
  this.__repro_filter = filter;
1657
- this.__repro_spanContext = captureSpanContextFromTracer();
1667
+ this.__repro_spanContext = captureSpanContext(this);
1658
1668
  }
1659
1669
  catch { }
1660
1670
  next();
@@ -1668,7 +1678,7 @@ function reproMongoosePlugin(cfg) {
1668
1678
  return;
1669
1679
  const collection = this.__repro_collection || resolveCollectionOrWarn(this, 'query');
1670
1680
  const filter = this.__repro_filter ?? { _id: before._id };
1671
- const spanContext = this.__repro_spanContext || captureSpanContextFromTracer();
1681
+ const spanContext = captureSpanContext(this) || this.__repro_spanContext || captureSpanContext();
1672
1682
  post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, getCtx().sid, {
1673
1683
  entries: [{
1674
1684
  actionId: getCtx().aid,
@@ -1717,7 +1727,7 @@ function reproMongoosePlugin(cfg) {
1717
1727
  t0: Date.now(),
1718
1728
  collection: this?.model?.collection?.name || 'unknown',
1719
1729
  op,
1720
- spanContext: captureSpanContextFromTracer(),
1730
+ spanContext: null,
1721
1731
  filter: sanitizeDbValue(this.getFilter?.() ?? this._conditions ?? undefined),
1722
1732
  update: sanitizeDbValue(this.getUpdate?.() ?? this._update ?? undefined),
1723
1733
  projection: sanitizeDbValue(this.projection?.() ?? this._fields ?? undefined),
@@ -1729,7 +1739,7 @@ function reproMongoosePlugin(cfg) {
1729
1739
  t0: Date.now(),
1730
1740
  collection: 'unknown',
1731
1741
  op,
1732
- spanContext: captureSpanContextFromTracer(),
1742
+ spanContext: null,
1733
1743
  };
1734
1744
  }
1735
1745
  next();
@@ -1740,7 +1750,7 @@ function reproMongoosePlugin(cfg) {
1740
1750
  return;
1741
1751
  const meta = this.__repro_qmeta || { t0: Date.now(), collection: 'unknown', op };
1742
1752
  const resultMeta = summarizeQueryResult(op, res);
1743
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
1753
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
1744
1754
  emitDbQuery(cfg, sid, aid, {
1745
1755
  collection: meta.collection,
1746
1756
  op,
@@ -1749,6 +1759,7 @@ function reproMongoosePlugin(cfg) {
1749
1759
  durMs: Date.now() - meta.t0,
1750
1760
  t: alignedNow(),
1751
1761
  spanContext,
1762
+ spanSource: this,
1752
1763
  });
1753
1764
  });
1754
1765
  }
@@ -1761,14 +1772,14 @@ function reproMongoosePlugin(cfg) {
1761
1772
  t0: Date.now(),
1762
1773
  collection: this?.collection?.name || this?.model?.collection?.name || 'unknown',
1763
1774
  docs: sanitizeDbValue(docs),
1764
- spanContext: captureSpanContextFromTracer(),
1775
+ spanContext: null,
1765
1776
  };
1766
1777
  }
1767
1778
  catch {
1768
1779
  this.__repro_insert_meta = {
1769
1780
  t0: Date.now(),
1770
1781
  collection: 'unknown',
1771
- spanContext: captureSpanContextFromTracer(),
1782
+ spanContext: null,
1772
1783
  };
1773
1784
  }
1774
1785
  next();
@@ -1779,7 +1790,7 @@ function reproMongoosePlugin(cfg) {
1779
1790
  return;
1780
1791
  const meta = this.__repro_insert_meta || { t0: Date.now(), collection: 'unknown' };
1781
1792
  const resultMeta = Array.isArray(docs) ? { inserted: docs.length } : summarizeQueryResult('insertMany', docs);
1782
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
1793
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
1783
1794
  emitDbQuery(cfg, sid, aid, {
1784
1795
  collection: meta.collection,
1785
1796
  op: 'insertMany',
@@ -1788,6 +1799,7 @@ function reproMongoosePlugin(cfg) {
1788
1799
  durMs: Date.now() - meta.t0,
1789
1800
  t: alignedNow(),
1790
1801
  spanContext,
1802
+ spanSource: this,
1791
1803
  });
1792
1804
  });
1793
1805
  schema.pre('bulkWrite', { document: false, query: false }, function (next, ops) {
@@ -1796,14 +1808,14 @@ function reproMongoosePlugin(cfg) {
1796
1808
  t0: Date.now(),
1797
1809
  collection: this?.collection?.name || this?.model?.collection?.name || 'unknown',
1798
1810
  ops: sanitizeDbValue(ops),
1799
- spanContext: captureSpanContextFromTracer(),
1811
+ spanContext: null,
1800
1812
  };
1801
1813
  }
1802
1814
  catch {
1803
1815
  this.__repro_bulk_meta = {
1804
1816
  t0: Date.now(),
1805
1817
  collection: 'unknown',
1806
- spanContext: captureSpanContextFromTracer(),
1818
+ spanContext: null,
1807
1819
  };
1808
1820
  }
1809
1821
  next();
@@ -1815,7 +1827,7 @@ function reproMongoosePlugin(cfg) {
1815
1827
  const meta = this.__repro_bulk_meta || { t0: Date.now(), collection: 'unknown' };
1816
1828
  const bulkResult = summarizeBulkResult(res);
1817
1829
  const resultMeta = { ...bulkResult, result: sanitizeResultForMeta(res?.result ?? res) };
1818
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
1830
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
1819
1831
  emitDbQuery(cfg, sid, aid, {
1820
1832
  collection: meta.collection,
1821
1833
  op: 'bulkWrite',
@@ -1824,6 +1836,7 @@ function reproMongoosePlugin(cfg) {
1824
1836
  durMs: Date.now() - meta.t0,
1825
1837
  t: alignedNow(),
1826
1838
  spanContext,
1839
+ spanSource: this,
1827
1840
  });
1828
1841
  });
1829
1842
  // Aggregate middleware (non-intrusive)
@@ -1835,7 +1848,7 @@ function reproMongoosePlugin(cfg) {
1835
1848
  this?._model?.collection?.name ||
1836
1849
  (this?.model && this.model.collection?.name) ||
1837
1850
  'unknown',
1838
- spanContext: captureSpanContextFromTracer(),
1851
+ spanContext: null,
1839
1852
  pipeline: sanitizeDbValue(this.pipeline?.() ?? this._pipeline ?? undefined),
1840
1853
  };
1841
1854
  }
@@ -1844,7 +1857,7 @@ function reproMongoosePlugin(cfg) {
1844
1857
  t0: Date.now(),
1845
1858
  collection: 'unknown',
1846
1859
  pipeline: undefined,
1847
- spanContext: captureSpanContextFromTracer(),
1860
+ spanContext: null,
1848
1861
  };
1849
1862
  }
1850
1863
  next();
@@ -1855,7 +1868,7 @@ function reproMongoosePlugin(cfg) {
1855
1868
  return;
1856
1869
  const meta = this.__repro_aggmeta || { t0: Date.now(), collection: 'unknown' };
1857
1870
  const resultMeta = summarizeQueryResult('aggregate', res);
1858
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
1871
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
1859
1872
  emitDbQuery(cfg, sid, aid, {
1860
1873
  collection: meta.collection,
1861
1874
  op: 'aggregate',
@@ -1864,6 +1877,7 @@ function reproMongoosePlugin(cfg) {
1864
1877
  durMs: Date.now() - meta.t0,
1865
1878
  t: alignedNow(),
1866
1879
  spanContext,
1880
+ spanSource: this,
1867
1881
  });
1868
1882
  });
1869
1883
  };
@@ -1988,7 +2002,7 @@ function emitDbQuery(cfg, sid, aid, payload) {
1988
2002
  durMs: payload.durMs ?? undefined,
1989
2003
  pk: null, before: null, after: null,
1990
2004
  error: payload.error ?? undefined,
1991
- }, payload?.spanContext);
2005
+ }, payload?.spanContext, payload?.spanSource);
1992
2006
  post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, sid, {
1993
2007
  entries: [{
1994
2008
  actionId: aid ?? null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "repro-nest",
3
- "version": "0.0.209",
3
+ "version": "0.0.211",
4
4
  "description": "Repro Nest SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -648,8 +648,19 @@ export function initReproTracing(opts?: ReproTracingInitOptions) {
648
648
  /** Optional helper if users want to check it. */
649
649
  export function isReproTracingEnabled() { return __TRACER_READY; }
650
650
 
651
- function captureSpanContextFromTracer(): SpanContext | null {
651
+ function captureSpanContext(source?: any): SpanContext | null {
652
652
  try {
653
+ const fromSource = source && source.__repro_span_context;
654
+ if (fromSource) {
655
+ const span: SpanContext = {
656
+ traceId: fromSource.traceId ?? null,
657
+ spanId: fromSource.spanId ?? null,
658
+ parentSpanId: fromSource.parentSpanId ?? null,
659
+ depth: fromSource.depth ?? null,
660
+ };
661
+ return span;
662
+ }
663
+
653
664
  const ctx = __TRACER__?.getCurrentSpanContext?.();
654
665
  if (ctx) {
655
666
  const span: SpanContext = {
@@ -671,9 +682,9 @@ function captureSpanContextFromTracer(): SpanContext | null {
671
682
  return null;
672
683
  }
673
684
 
674
- function attachSpanContext<T extends Record<string, any>>(target: T, span?: SpanContext | null): T {
685
+ function attachSpanContext<T extends Record<string, any>>(target: T, spanSource?: SpanContext | null, fallbackSource?: any): T {
675
686
  if (!target) return target;
676
- const ctx = span ?? captureSpanContextFromTracer();
687
+ const ctx = spanSource ?? captureSpanContext(fallbackSource);
677
688
  if (ctx) {
678
689
  try { (target as any).spanContext = ctx; } catch {}
679
690
  }
@@ -1785,7 +1796,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1785
1796
  wasNew: this.isNew,
1786
1797
  before,
1787
1798
  collection: resolveCollectionOrWarn(this, 'doc'),
1788
- spanContext: captureSpanContextFromTracer(),
1799
+ spanContext: captureSpanContext(this),
1789
1800
  };
1790
1801
  next();
1791
1802
  });
@@ -1799,7 +1810,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1799
1810
  const before = meta.before ?? null;
1800
1811
  const after = this.toObject({ depopulate: true });
1801
1812
  const collection = meta.collection || resolveCollectionOrWarn(this, 'doc');
1802
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
1813
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
1803
1814
 
1804
1815
  const query = meta.wasNew
1805
1816
  ? { op: 'insertOne', doc: after }
@@ -1831,7 +1842,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1831
1842
  (this as any).__repro_before = await model.findOne(filter).lean().exec();
1832
1843
  this.setOptions({ new: true });
1833
1844
  (this as any).__repro_collection = resolveCollectionOrWarn(this, 'query');
1834
- (this as any).__repro_spanContext = captureSpanContextFromTracer();
1845
+ (this as any).__repro_spanContext = captureSpanContext(this);
1835
1846
  } catch {}
1836
1847
  next();
1837
1848
  });
@@ -1843,7 +1854,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1843
1854
  const before = (this as any).__repro_before ?? null;
1844
1855
  const after = res ?? null;
1845
1856
  const collection = (this as any).__repro_collection || resolveCollectionOrWarn(this, 'query');
1846
- const spanContext = (this as any).__repro_spanContext || captureSpanContextFromTracer();
1857
+ const spanContext = captureSpanContext(this) || (this as any).__repro_spanContext || captureSpanContext();
1847
1858
  const pk = after?._id ?? before?._id;
1848
1859
 
1849
1860
  post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, (getCtx() as Ctx).sid!, {
@@ -1869,7 +1880,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1869
1880
  (this as any).__repro_before = await (this.model as Model<any>).findOne(filter).lean().exec();
1870
1881
  (this as any).__repro_collection = resolveCollectionOrWarn(this, 'query');
1871
1882
  (this as any).__repro_filter = filter;
1872
- (this as any).__repro_spanContext = captureSpanContextFromTracer();
1883
+ (this as any).__repro_spanContext = captureSpanContext(this);
1873
1884
  } catch {}
1874
1885
  next();
1875
1886
  });
@@ -1880,7 +1891,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1880
1891
  if (!before) return;
1881
1892
  const collection = (this as any).__repro_collection || resolveCollectionOrWarn(this, 'query');
1882
1893
  const filter = (this as any).__repro_filter ?? { _id: before._id };
1883
- const spanContext = (this as any).__repro_spanContext || captureSpanContextFromTracer();
1894
+ const spanContext = captureSpanContext(this) || (this as any).__repro_spanContext || captureSpanContext();
1884
1895
  post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, (getCtx() as Ctx).sid!, {
1885
1896
  entries: [{
1886
1897
  actionId: (getCtx() as Ctx).aid!,
@@ -1933,7 +1944,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1933
1944
  t0: Date.now(),
1934
1945
  collection: this?.model?.collection?.name || 'unknown',
1935
1946
  op,
1936
- spanContext: captureSpanContextFromTracer(),
1947
+ spanContext: null,
1937
1948
  filter: sanitizeDbValue(this.getFilter?.() ?? this._conditions ?? undefined),
1938
1949
  update: sanitizeDbValue(this.getUpdate?.() ?? this._update ?? undefined),
1939
1950
  projection: sanitizeDbValue(this.projection?.() ?? this._fields ?? undefined),
@@ -1944,7 +1955,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1944
1955
  t0: Date.now(),
1945
1956
  collection: 'unknown',
1946
1957
  op,
1947
- spanContext: captureSpanContextFromTracer(),
1958
+ spanContext: null,
1948
1959
  };
1949
1960
  }
1950
1961
  next();
@@ -1956,7 +1967,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1956
1967
 
1957
1968
  const meta = (this as any).__repro_qmeta || { t0: Date.now(), collection: 'unknown', op };
1958
1969
  const resultMeta = summarizeQueryResult(op, res);
1959
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
1970
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
1960
1971
 
1961
1972
  emitDbQuery(cfg, sid, aid, {
1962
1973
  collection: meta.collection,
@@ -1966,6 +1977,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1966
1977
  durMs: Date.now() - meta.t0,
1967
1978
  t: alignedNow(),
1968
1979
  spanContext,
1980
+ spanSource: this,
1969
1981
  });
1970
1982
  });
1971
1983
  }
@@ -1980,13 +1992,13 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1980
1992
  t0: Date.now(),
1981
1993
  collection: this?.collection?.name || this?.model?.collection?.name || 'unknown',
1982
1994
  docs: sanitizeDbValue(docs),
1983
- spanContext: captureSpanContextFromTracer(),
1995
+ spanContext: null,
1984
1996
  };
1985
1997
  } catch {
1986
1998
  (this as any).__repro_insert_meta = {
1987
1999
  t0: Date.now(),
1988
2000
  collection: 'unknown',
1989
- spanContext: captureSpanContextFromTracer(),
2001
+ spanContext: null,
1990
2002
  };
1991
2003
  }
1992
2004
  next();
@@ -1997,7 +2009,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
1997
2009
  if (!sid) return;
1998
2010
  const meta = (this as any).__repro_insert_meta || { t0: Date.now(), collection: 'unknown' };
1999
2011
  const resultMeta = Array.isArray(docs) ? { inserted: docs.length } : summarizeQueryResult('insertMany', docs);
2000
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
2012
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
2001
2013
 
2002
2014
  emitDbQuery(cfg, sid, aid, {
2003
2015
  collection: meta.collection,
@@ -2007,6 +2019,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
2007
2019
  durMs: Date.now() - meta.t0,
2008
2020
  t: alignedNow(),
2009
2021
  spanContext,
2022
+ spanSource: this,
2010
2023
  });
2011
2024
  } as any);
2012
2025
 
@@ -2016,13 +2029,13 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
2016
2029
  t0: Date.now(),
2017
2030
  collection: this?.collection?.name || this?.model?.collection?.name || 'unknown',
2018
2031
  ops: sanitizeDbValue(ops),
2019
- spanContext: captureSpanContextFromTracer(),
2032
+ spanContext: null,
2020
2033
  };
2021
2034
  } catch {
2022
2035
  (this as any).__repro_bulk_meta = {
2023
2036
  t0: Date.now(),
2024
2037
  collection: 'unknown',
2025
- spanContext: captureSpanContextFromTracer(),
2038
+ spanContext: null,
2026
2039
  };
2027
2040
  }
2028
2041
  next();
@@ -2034,7 +2047,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
2034
2047
  const meta = (this as any).__repro_bulk_meta || { t0: Date.now(), collection: 'unknown' };
2035
2048
  const bulkResult = summarizeBulkResult(res);
2036
2049
  const resultMeta = { ...bulkResult, result: sanitizeResultForMeta(res?.result ?? res) };
2037
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
2050
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
2038
2051
 
2039
2052
  emitDbQuery(cfg, sid, aid, {
2040
2053
  collection: meta.collection,
@@ -2044,6 +2057,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
2044
2057
  durMs: Date.now() - meta.t0,
2045
2058
  t: alignedNow(),
2046
2059
  spanContext,
2060
+ spanSource: this,
2047
2061
  });
2048
2062
  } as any);
2049
2063
 
@@ -2057,7 +2071,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
2057
2071
  this?._model?.collection?.name ||
2058
2072
  (this?.model && this.model.collection?.name) ||
2059
2073
  'unknown',
2060
- spanContext: captureSpanContextFromTracer(),
2074
+ spanContext: null,
2061
2075
  pipeline: sanitizeDbValue(this.pipeline?.() ?? this._pipeline ?? undefined),
2062
2076
  };
2063
2077
  } catch {
@@ -2065,7 +2079,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
2065
2079
  t0: Date.now(),
2066
2080
  collection: 'unknown',
2067
2081
  pipeline: undefined,
2068
- spanContext: captureSpanContextFromTracer(),
2082
+ spanContext: null,
2069
2083
  };
2070
2084
  }
2071
2085
  next();
@@ -2077,7 +2091,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
2077
2091
 
2078
2092
  const meta = (this as any).__repro_aggmeta || { t0: Date.now(), collection: 'unknown' };
2079
2093
  const resultMeta = summarizeQueryResult('aggregate', res);
2080
- const spanContext = meta.spanContext || captureSpanContextFromTracer();
2094
+ const spanContext = captureSpanContext(this) || meta.spanContext || captureSpanContext();
2081
2095
 
2082
2096
  emitDbQuery(cfg, sid, aid, {
2083
2097
  collection: meta.collection,
@@ -2087,6 +2101,7 @@ export function reproMongoosePlugin(cfg: { appId: string; tenantId: string; appS
2087
2101
  durMs: Date.now() - meta.t0,
2088
2102
  t: alignedNow(),
2089
2103
  spanContext,
2104
+ spanSource: this,
2090
2105
  });
2091
2106
  });
2092
2107
  };
@@ -2204,7 +2219,7 @@ function emitDbQuery(cfg: any, sid?: string, aid?: string, payload?: any) {
2204
2219
  durMs: payload.durMs ?? undefined,
2205
2220
  pk: null, before: null, after: null,
2206
2221
  error: payload.error ?? undefined,
2207
- }, payload?.spanContext);
2222
+ }, payload?.spanContext, payload?.spanSource);
2208
2223
  post(cfg.apiBase, cfg.tenantId, cfg.appId, cfg.appSecret, sid, {
2209
2224
  entries: [{
2210
2225
  actionId: aid ?? null,
package/tracer/runtime.js CHANGED
@@ -262,6 +262,12 @@ 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
+ });
265
271
  emitNow({ unawaited: forceUnawaited, returnValue: rv }, spanForExit, spanStackForExit);
266
272
  return;
267
273
  }
@@ -272,6 +278,12 @@ const trace = {
272
278
  }
273
279
 
274
280
  if (isQuery) {
281
+ setQuerySpanContext(rv, {
282
+ traceId: traceIdAtExit,
283
+ spanId: spanInfoPeek.id,
284
+ parentSpanId: spanInfoPeek.parentId,
285
+ depth: spanInfoPeek.depth ?? depthAtExit
286
+ });
275
287
  emitNow({ unawaited: forceUnawaited });
276
288
  return;
277
289
  }
@@ -841,6 +853,28 @@ function getCurrentTraceId() {
841
853
  return s && s.traceId || null;
842
854
  }
843
855
 
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
+
844
878
  function getCurrentSpanContext() {
845
879
  try {
846
880
  const store = als.getStore();