@@ -2,6 +2,7 @@ import type { Span } from '@opentelemetry/api';
22import { SpanKind } from '@opentelemetry/api' ;
33import { TraceFlags , context , trace } from '@opentelemetry/api' ;
44import type { ReadableSpan } from '@opentelemetry/sdk-trace-base' ;
5+ import { Span as SpanClass } from '@opentelemetry/sdk-trace-base' ;
56import type { PropagationContext } from '@sentry/types' ;
67
78import { getClient } from '../src/custom/hub' ;
@@ -260,6 +261,28 @@ describe('trace', () => {
260261 } ,
261262 ) ;
262263 } ) ;
264+
265+ describe ( 'onlyIfParent' , ( ) => {
266+ it ( 'does not create a span if there is no parent' , ( ) => {
267+ const span = startSpan ( { name : 'test span' , onlyIfParent : true } , span => {
268+ return span ;
269+ } ) ;
270+
271+ expect ( span ) . not . toBeInstanceOf ( SpanClass ) ;
272+ } ) ;
273+
274+ it ( 'creates a span if there is a parent' , ( ) => {
275+ const span = startSpan ( { name : 'parent span' } , ( ) => {
276+ const span = startSpan ( { name : 'test span' , onlyIfParent : true } , span => {
277+ return span ;
278+ } ) ;
279+
280+ return span ;
281+ } ) ;
282+
283+ expect ( span ) . toBeInstanceOf ( SpanClass ) ;
284+ } ) ;
285+ } ) ;
263286 } ) ;
264287
265288 describe ( 'startInactiveSpan' , ( ) => {
@@ -349,6 +372,24 @@ describe('trace', () => {
349372 } ) ;
350373 expect ( getSpanKind ( span ) ) . toEqual ( SpanKind . CLIENT ) ;
351374 } ) ;
375+
376+ describe ( 'onlyIfParent' , ( ) => {
377+ it ( 'does not create a span if there is no parent' , ( ) => {
378+ const span = startInactiveSpan ( { name : 'test span' , onlyIfParent : true } ) ;
379+
380+ expect ( span ) . not . toBeInstanceOf ( SpanClass ) ;
381+ } ) ;
382+
383+ it ( 'creates a span if there is a parent' , ( ) => {
384+ const span = startSpan ( { name : 'parent span' } , ( ) => {
385+ const span = startInactiveSpan ( { name : 'test span' , onlyIfParent : true } ) ;
386+
387+ return span ;
388+ } ) ;
389+
390+ expect ( span ) . toBeInstanceOf ( SpanClass ) ;
391+ } ) ;
392+ } ) ;
352393 } ) ;
353394
354395 describe ( 'startSpanManual' , ( ) => {
@@ -419,6 +460,28 @@ describe('trace', () => {
419460 ) ;
420461 } ) ;
421462 } ) ;
463+
464+ describe ( 'onlyIfParent' , ( ) => {
465+ it ( 'does not create a span if there is no parent' , ( ) => {
466+ const span = startSpanManual ( { name : 'test span' , onlyIfParent : true } , span => {
467+ return span ;
468+ } ) ;
469+
470+ expect ( span ) . not . toBeInstanceOf ( SpanClass ) ;
471+ } ) ;
472+
473+ it ( 'creates a span if there is a parent' , ( ) => {
474+ const span = startSpan ( { name : 'parent span' } , ( ) => {
475+ const span = startSpanManual ( { name : 'test span' , onlyIfParent : true } , span => {
476+ return span ;
477+ } ) ;
478+
479+ return span ;
480+ } ) ;
481+
482+ expect ( span ) . toBeInstanceOf ( SpanClass ) ;
483+ } ) ;
484+ } ) ;
422485} ) ;
423486
424487describe ( 'trace (tracing disabled)' , ( ) => {
0 commit comments