@sentry/opentelemetry 9.24.0 → 9.25.1

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.
@@ -1619,6 +1619,9 @@ class SentrySpanExporter {
1619
1619
  * bucket array, and letting garbage collection take care of the rest.
1620
1620
  */
1621
1621
 
1622
+ // Essentially a a set of span ids that are already sent. The values are expiration
1623
+ // times in this cache so we don't hold onto them indefinitely.
1624
+
1622
1625
  constructor(options
1623
1626
 
1624
1627
  ) {
@@ -1626,6 +1629,48 @@ class SentrySpanExporter {
1626
1629
  this._finishedSpanBuckets = new Array(this._finishedSpanBucketSize).fill(undefined);
1627
1630
  this._lastCleanupTimestampInS = Math.floor(Date.now() / 1000);
1628
1631
  this._spansToBucketEntry = new WeakMap();
1632
+ this._sentSpans = new Map();
1633
+ }
1634
+
1635
+ /**
1636
+ * Check if a span with the given ID has already been sent using the `_sentSpans` as a cache.
1637
+ * Purges "expired" spans from the cache upon checking.
1638
+ * @param spanId The span id to check.
1639
+ * @returns Whether the span is already sent in the past X seconds.
1640
+ */
1641
+ isSpanAlreadySent(spanId) {
1642
+ const expirationTime = this._sentSpans.get(spanId);
1643
+ if (expirationTime) {
1644
+ if (Date.now() >= expirationTime) {
1645
+ this._sentSpans.delete(spanId); // Remove expired span
1646
+ } else {
1647
+ return true;
1648
+ }
1649
+ }
1650
+ return false;
1651
+ }
1652
+
1653
+ /** Remove "expired" span id entries from the _sentSpans cache. */
1654
+ flushSentSpanCache() {
1655
+ const currentTimestamp = Date.now();
1656
+ // Note, it is safe to delete items from the map as we go: https://stackoverflow.com/a/35943995/90297
1657
+ for (const [spanId, expirationTime] of this._sentSpans.entries()) {
1658
+ if (expirationTime <= currentTimestamp) {
1659
+ this._sentSpans.delete(spanId);
1660
+ }
1661
+ }
1662
+ }
1663
+
1664
+ /** Check if a node is a completed root node or a node whose parent has already been sent */
1665
+ nodeIsCompletedRootNode(node) {
1666
+ return !!node.span && (!node.parentNode || this.isSpanAlreadySent(node.parentNode.id));
1667
+ }
1668
+
1669
+ /** Get all completed root nodes from a list of nodes */
1670
+ getCompletedRootNodes(nodes) {
1671
+ // TODO: We should be able to remove the explicit `node is SpanNodeCompleted` type guard
1672
+ // once we stop supporting TS < 5.5
1673
+ return nodes.filter((node) => this.nodeIsCompletedRootNode(node));
1629
1674
  }
1630
1675
 
1631
1676
  /** Export a single span. */
@@ -1659,7 +1704,8 @@ class SentrySpanExporter {
1659
1704
  this._spansToBucketEntry.set(span, currentBucket);
1660
1705
 
1661
1706
  // If the span doesn't have a local parent ID (it's a root span), we're gonna flush all the ended spans
1662
- if (!getLocalParentId(span)) {
1707
+ const localParentId = getLocalParentId(span);
1708
+ if (!localParentId || this.isSpanAlreadySent(localParentId)) {
1663
1709
  this._clearTimeout();
1664
1710
 
1665
1711
  // If we got a parent span, we try to send the span tree
@@ -1674,30 +1720,29 @@ class SentrySpanExporter {
1674
1720
  flush() {
1675
1721
  this._clearTimeout();
1676
1722
 
1677
- const finishedSpans = [];
1678
- this._finishedSpanBuckets.forEach(bucket => {
1679
- if (bucket) {
1680
- finishedSpans.push(...bucket.spans);
1681
- }
1682
- });
1723
+ const finishedSpans = this._finishedSpanBuckets.flatMap(bucket =>
1724
+ bucket ? Array.from(bucket.spans) : [],
1725
+ );
1683
1726
 
1684
- const sentSpans = maybeSend(finishedSpans);
1727
+ this.flushSentSpanCache();
1728
+ const sentSpans = this._maybeSend(finishedSpans);
1729
+ for (const span of finishedSpans) {
1730
+ this._sentSpans.set(span.spanContext().spanId, Date.now() + DEFAULT_TIMEOUT * 1000);
1731
+ }
1685
1732
 
1686
1733
  const sentSpanCount = sentSpans.size;
1687
-
1688
1734
  const remainingOpenSpanCount = finishedSpans.length - sentSpanCount;
1689
-
1690
1735
  DEBUG_BUILD &&
1691
1736
  core.logger.log(
1692
1737
  `SpanExporter exported ${sentSpanCount} spans, ${remainingOpenSpanCount} spans are waiting for their parent spans to finish`,
1693
1738
  );
1694
1739
 
1695
- sentSpans.forEach(span => {
1740
+ for (const span of sentSpans) {
1696
1741
  const bucketEntry = this._spansToBucketEntry.get(span);
1697
1742
  if (bucketEntry) {
1698
1743
  bucketEntry.spans.delete(span);
1699
1744
  }
1700
- });
1745
+ }
1701
1746
  }
1702
1747
 
1703
1748
  /** Clear the exporter. */
@@ -1713,59 +1758,51 @@ class SentrySpanExporter {
1713
1758
  this._flushTimeout = undefined;
1714
1759
  }
1715
1760
  }
1716
- }
1717
1761
 
1718
- /**
1719
- * Send the given spans, but only if they are part of a finished transaction.
1720
- *
1721
- * Returns the sent spans.
1722
- * Spans remain unsent when their parent span is not yet finished.
1723
- * This will happen regularly, as child spans are generally finished before their parents.
1724
- * But it _could_ also happen because, for whatever reason, a parent span was lost.
1725
- * In this case, we'll eventually need to clean this up.
1726
- */
1727
- function maybeSend(spans) {
1728
- const grouped = groupSpansWithParents(spans);
1729
- const sentSpans = new Set();
1730
-
1731
- const rootNodes = getCompletedRootNodes(grouped);
1732
-
1733
- rootNodes.forEach(root => {
1734
- const span = root.span;
1735
- sentSpans.add(span);
1736
- const transactionEvent = createTransactionForOtelSpan(span);
1762
+ /**
1763
+ * Send the given spans, but only if they are part of a finished transaction.
1764
+ *
1765
+ * Returns the sent spans.
1766
+ * Spans remain unsent when their parent span is not yet finished.
1767
+ * This will happen regularly, as child spans are generally finished before their parents.
1768
+ * But it _could_ also happen because, for whatever reason, a parent span was lost.
1769
+ * In this case, we'll eventually need to clean this up.
1770
+ */
1771
+ _maybeSend(spans) {
1772
+ const grouped = groupSpansWithParents(spans);
1773
+ const sentSpans = new Set();
1737
1774
 
1738
- // We'll recursively add all the child spans to this array
1739
- const spans = transactionEvent.spans || [];
1775
+ const rootNodes = this.getCompletedRootNodes(grouped);
1740
1776
 
1741
- root.children.forEach(child => {
1742
- createAndFinishSpanForOtelSpan(child, spans, sentSpans);
1743
- });
1777
+ for (const root of rootNodes) {
1778
+ const span = root.span;
1779
+ sentSpans.add(span);
1780
+ const transactionEvent = createTransactionForOtelSpan(span);
1744
1781
 
1745
- // spans.sort() mutates the array, but we do not use this anymore after this point
1746
- // so we can safely mutate it here
1747
- transactionEvent.spans =
1748
- spans.length > MAX_SPAN_COUNT
1749
- ? spans.sort((a, b) => a.start_timestamp - b.start_timestamp).slice(0, MAX_SPAN_COUNT)
1750
- : spans;
1782
+ // We'll recursively add all the child spans to this array
1783
+ const spans = transactionEvent.spans || [];
1751
1784
 
1752
- const measurements = core.timedEventsToMeasurements(span.events);
1753
- if (measurements) {
1754
- transactionEvent.measurements = measurements;
1755
- }
1785
+ for (const child of root.children) {
1786
+ createAndFinishSpanForOtelSpan(child, spans, sentSpans);
1787
+ }
1756
1788
 
1757
- core.captureEvent(transactionEvent);
1758
- });
1789
+ // spans.sort() mutates the array, but we do not use this anymore after this point
1790
+ // so we can safely mutate it here
1791
+ transactionEvent.spans =
1792
+ spans.length > MAX_SPAN_COUNT
1793
+ ? spans.sort((a, b) => a.start_timestamp - b.start_timestamp).slice(0, MAX_SPAN_COUNT)
1794
+ : spans;
1759
1795
 
1760
- return sentSpans;
1761
- }
1796
+ const measurements = core.timedEventsToMeasurements(span.events);
1797
+ if (measurements) {
1798
+ transactionEvent.measurements = measurements;
1799
+ }
1762
1800
 
1763
- function nodeIsCompletedRootNode(node) {
1764
- return !!node.span && !node.parentNode;
1765
- }
1801
+ core.captureEvent(transactionEvent);
1802
+ }
1766
1803
 
1767
- function getCompletedRootNodes(nodes) {
1768
- return nodes.filter(nodeIsCompletedRootNode);
1804
+ return sentSpans;
1805
+ }
1769
1806
  }
1770
1807
 
1771
1808
  function parseSpan(span) {