@@ -1876,6 +1876,151 @@ describe('continueTrace', () => {
18761876
18771877 expect ( result ) . toEqual ( 'aha' ) ;
18781878 } ) ;
1879+
1880+ describe ( 'strictTraceContinuation' , ( ) => {
1881+ const creatOrgIdInDsn = ( orgId : number ) => {
1882+ vi . spyOn ( client , 'getDsn' ) . mockReturnValue ( {
1883+ host : `o${ orgId } .ingest.sentry.io` ,
1884+ protocol : 'https' ,
1885+ projectId : 'projId' ,
1886+ } ) ;
1887+ } ;
1888+
1889+ afterEach ( ( ) => {
1890+ vi . clearAllMocks ( ) ;
1891+ } ) ;
1892+
1893+ it ( 'continues trace when org IDs match' , ( ) => {
1894+ creatOrgIdInDsn ( 123 ) ;
1895+
1896+ const scope = continueTrace (
1897+ {
1898+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1899+ baggage : 'sentry-org_id=123' ,
1900+ } ,
1901+ ( ) => {
1902+ return getCurrentScope ( ) ;
1903+ } ,
1904+ ) ;
1905+
1906+ expect ( scope . getPropagationContext ( ) . traceId ) . toBe ( '12312012123120121231201212312012' ) ;
1907+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBe ( '1121201211212012' ) ;
1908+ } ) ;
1909+
1910+ it ( 'starts new trace when both SDK and baggage org IDs are set and do not match' , ( ) => {
1911+ creatOrgIdInDsn ( 123 ) ;
1912+
1913+ const scope = continueTrace (
1914+ {
1915+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1916+ baggage : 'sentry-org_id=456' ,
1917+ } ,
1918+ ( ) => {
1919+ return getCurrentScope ( ) ;
1920+ } ,
1921+ ) ;
1922+
1923+ // Should start a new trace with a different trace ID
1924+ expect ( scope . getPropagationContext ( ) . traceId ) . not . toBe ( '12312012123120121231201212312012' ) ;
1925+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBeUndefined ( ) ;
1926+ } ) ;
1927+
1928+ describe ( 'when strictTraceContinuation is true' , ( ) => {
1929+ it ( 'starts new trace when baggage org ID is missing' , ( ) => {
1930+ client . getOptions ( ) . strictTraceContinuation = true ;
1931+
1932+ creatOrgIdInDsn ( 123 ) ;
1933+
1934+ const scope = continueTrace (
1935+ {
1936+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1937+ baggage : 'sentry-environment=production' ,
1938+ } ,
1939+ ( ) => {
1940+ return getCurrentScope ( ) ;
1941+ } ,
1942+ ) ;
1943+
1944+ // Should start a new trace with a different trace ID
1945+ expect ( scope . getPropagationContext ( ) . traceId ) . not . toBe ( '12312012123120121231201212312012' ) ;
1946+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBeUndefined ( ) ;
1947+ } ) ;
1948+
1949+ it ( 'starts new trace when SDK org ID is missing' , ( ) => {
1950+ client . getOptions ( ) . strictTraceContinuation = true ;
1951+
1952+ const scope = continueTrace (
1953+ {
1954+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1955+ baggage : 'sentry-org_id=123' ,
1956+ } ,
1957+ ( ) => {
1958+ return getCurrentScope ( ) ;
1959+ } ,
1960+ ) ;
1961+
1962+ // Should start a new trace with a different trace ID
1963+ expect ( scope . getPropagationContext ( ) . traceId ) . not . toBe ( '12312012123120121231201212312012' ) ;
1964+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBeUndefined ( ) ;
1965+ } ) ;
1966+
1967+ it ( 'continues trace when both org IDs are missing' , ( ) => {
1968+ client . getOptions ( ) . strictTraceContinuation = true ;
1969+
1970+ const scope = continueTrace (
1971+ {
1972+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1973+ baggage : 'sentry-environment=production' ,
1974+ } ,
1975+ ( ) => {
1976+ return getCurrentScope ( ) ;
1977+ } ,
1978+ ) ;
1979+
1980+ // Should continue the trace
1981+ expect ( scope . getPropagationContext ( ) . traceId ) . toBe ( '12312012123120121231201212312012' ) ;
1982+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBe ( '1121201211212012' ) ;
1983+ } ) ;
1984+ } ) ;
1985+
1986+ describe ( 'when strictTraceContinuation is false' , ( ) => {
1987+ it ( 'continues trace when baggage org ID is missing' , ( ) => {
1988+ client . getOptions ( ) . strictTraceContinuation = false ;
1989+
1990+ creatOrgIdInDsn ( 123 ) ;
1991+
1992+ const scope = continueTrace (
1993+ {
1994+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
1995+ baggage : 'sentry-environment=production' ,
1996+ } ,
1997+ ( ) => {
1998+ return getCurrentScope ( ) ;
1999+ } ,
2000+ ) ;
2001+
2002+ expect ( scope . getPropagationContext ( ) . traceId ) . toBe ( '12312012123120121231201212312012' ) ;
2003+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBe ( '1121201211212012' ) ;
2004+ } ) ;
2005+
2006+ it ( 'SDK org ID is missing' , ( ) => {
2007+ client . getOptions ( ) . strictTraceContinuation = false ;
2008+
2009+ const scope = continueTrace (
2010+ {
2011+ sentryTrace : '12312012123120121231201212312012-1121201211212012-1' ,
2012+ baggage : 'sentry-org_id=123' ,
2013+ } ,
2014+ ( ) => {
2015+ return getCurrentScope ( ) ;
2016+ } ,
2017+ ) ;
2018+
2019+ expect ( scope . getPropagationContext ( ) . traceId ) . toBe ( '12312012123120121231201212312012' ) ;
2020+ expect ( scope . getPropagationContext ( ) . parentSpanId ) . toBe ( '1121201211212012' ) ;
2021+ } ) ;
2022+ } ) ;
2023+ } ) ;
18792024} ) ;
18802025
18812026describe ( 'getActiveSpan' , ( ) => {
0 commit comments