@@ -31,6 +31,7 @@ import type { Span } from '../../../src/types-hoist/span';
3131import type { StartSpanOptions } from '../../../src/types-hoist/startSpanOptions' ;
3232import { _setSpanForScope } from '../../../src/utils/spanOnScope' ;
3333import { getActiveSpan , getRootSpan , getSpanDescendants , spanIsSampled } from '../../../src/utils/spanUtils' ;
34+ import { extractOrgIdFromDsnHost } from '../../../src/utils-hoist/dsn' ;
3435import { getDefaultTestClientOptions , TestClient } from '../../mocks/client' ;
3536
3637const enum Type {
@@ -1733,6 +1734,151 @@ describe('continueTrace', () => {
17331734
17341735 expect ( result ) . toEqual ( 'aha' ) ;
17351736 } ) ;
1737+
1738+ describe ( 'strictTraceContinuation' , ( ) => {
1739+ const creatOrgIdInDsn = ( orgId : number ) => {
1740+ vi . spyOn ( client , 'getDsn' ) . mockReturnValue ( {
1741+ host : `o${ orgId } .ingest.sentry.io` ,
1742+ protocol : 'https' ,
1743+ projectId : 'projId' ,
1744+ } ) ;
1745+ } ;
1746+
1747+ afterEach ( ( ) => {
1748+ vi . clearAllMocks ( ) ;
1749+ } ) ;
1750+
1751+ it ( 'continues trace when org IDs match' , ( ) => {
1752+ creatOrgIdInDsn ( 123 ) ;
1753+
1754+ const scope = continueTrace (
1755+ {
1756+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1757+ baggage : 'sentry-org_id=123' ,
1758+ } ,
1759+ ( ) => {
1760+ return getCurrentScope ( ) ;
1761+ } ,
1762+ ) ;
1763+
1764+ expect ( scope . getPropagationContext ( ) . traceId ) . toBe ( '12312012123120121231201212312012' ) ;
1765+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBe ( '1121201211212012' ) ;
1766+ } ) ;
1767+
1768+ it ( 'starts new trace when both SDK and baggage org IDs are set and do not match' , ( ) => {
1769+ creatOrgIdInDsn ( 123 ) ;
1770+
1771+ const scope = continueTrace (
1772+ {
1773+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1774+ baggage : 'sentry-org_id=456' ,
1775+ } ,
1776+ ( ) => {
1777+ return getCurrentScope ( ) ;
1778+ } ,
1779+ ) ;
1780+
1781+ // Should start a new trace with a different trace ID
1782+ expect ( scope . getPropagationContext ( ) . traceId ) . not . toBe ( '12312012123120121231201212312012' ) ;
1783+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBeUndefined ( ) ;
1784+ } ) ;
1785+
1786+ describe ( 'when strictTraceContinuation is true' , ( ) => {
1787+ it ( 'starts new trace when baggage org ID is missing' , ( ) => {
1788+ client . getOptions ( ) . strictTraceContinuation = true ;
1789+
1790+ creatOrgIdInDsn ( 123 ) ;
1791+
1792+ const scope = continueTrace (
1793+ {
1794+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1795+ baggage : 'sentry-environment=production' ,
1796+ } ,
1797+ ( ) => {
1798+ return getCurrentScope ( ) ;
1799+ } ,
1800+ ) ;
1801+
1802+ // Should start a new trace with a different trace ID
1803+ expect ( scope . getPropagationContext ( ) . traceId ) . not . toBe ( '12312012123120121231201212312012' ) ;
1804+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBeUndefined ( ) ;
1805+ } ) ;
1806+
1807+ it ( 'starts new trace when SDK org ID is missing' , ( ) => {
1808+ client . getOptions ( ) . strictTraceContinuation = true ;
1809+
1810+ const scope = continueTrace (
1811+ {
1812+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1813+ baggage : 'sentry-org_id=123' ,
1814+ } ,
1815+ ( ) => {
1816+ return getCurrentScope ( ) ;
1817+ } ,
1818+ ) ;
1819+
1820+ // Should start a new trace with a different trace ID
1821+ expect ( scope . getPropagationContext ( ) . traceId ) . not . toBe ( '12312012123120121231201212312012' ) ;
1822+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBeUndefined ( ) ;
1823+ } ) ;
1824+
1825+ it ( 'continues trace when both org IDs are missing' , ( ) => {
1826+ client . getOptions ( ) . strictTraceContinuation = true ;
1827+
1828+ const scope = continueTrace (
1829+ {
1830+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1831+ baggage : 'sentry-environment=production' ,
1832+ } ,
1833+ ( ) => {
1834+ return getCurrentScope ( ) ;
1835+ } ,
1836+ ) ;
1837+
1838+ // Should continue the trace
1839+ expect ( scope . getPropagationContext ( ) . traceId ) . toBe ( '12312012123120121231201212312012' ) ;
1840+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBe ( '1121201211212012' ) ;
1841+ } ) ;
1842+ } ) ;
1843+
1844+ describe ( 'when strictTraceContinuation is false' , ( ) => {
1845+ it ( 'continues trace when baggage org ID is missing' , ( ) => {
1846+ client . getOptions ( ) . strictTraceContinuation = false ;
1847+
1848+ creatOrgIdInDsn ( 123 ) ;
1849+
1850+ const scope = continueTrace (
1851+ {
1852+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1853+ baggage : 'sentry-environment=production' ,
1854+ } ,
1855+ ( ) => {
1856+ return getCurrentScope ( ) ;
1857+ } ,
1858+ ) ;
1859+
1860+ expect ( scope . getPropagationContext ( ) . traceId ) . toBe ( '12312012123120121231201212312012' ) ;
1861+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBe ( '1121201211212012' ) ;
1862+ } ) ;
1863+
1864+ it ( 'SDK org ID is missing' , ( ) => {
1865+ client . getOptions ( ) . strictTraceContinuation = false ;
1866+
1867+ const scope = continueTrace (
1868+ {
1869+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1870+ baggage : 'sentry-org_id=123' ,
1871+ } ,
1872+ ( ) => {
1873+ return getCurrentScope ( ) ;
1874+ } ,
1875+ ) ;
1876+
1877+ expect ( scope . getPropagationContext ( ) . traceId ) . toBe ( '12312012123120121231201212312012' ) ;
1878+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBe ( '1121201211212012' ) ;
1879+ } ) ;
1880+ } ) ;
1881+ } ) ;
17361882} ) ;
17371883
17381884describe ( 'getActiveSpan' , ( ) => {
0 commit comments