|  | 
|  | 1 | +import type { Span } from '@sentry/types'; | 
|  | 2 | +import { logger } from '@sentry/utils'; | 
|  | 3 | +import { DEBUG_BUILD } from '../debug-build'; | 
|  | 4 | +import { getRootSpan, spanIsSampled, spanToJSON } from '../utils/spanUtils'; | 
|  | 5 | + | 
|  | 6 | +/** | 
|  | 7 | + * Print a log message for a started span. | 
|  | 8 | + */ | 
|  | 9 | +export function logSpanStart(span: Span): void { | 
|  | 10 | +  if (!DEBUG_BUILD) return; | 
|  | 11 | + | 
|  | 12 | +  const { description = '< unknown name >', op = '< unknown op >', parent_span_id: parentSpanId } = spanToJSON(span); | 
|  | 13 | +  const { spanId } = span.spanContext(); | 
|  | 14 | + | 
|  | 15 | +  const sampled = spanIsSampled(span); | 
|  | 16 | +  const rootSpan = getRootSpan(span); | 
|  | 17 | +  const isRootSpan = rootSpan === span; | 
|  | 18 | + | 
|  | 19 | +  const header = `[Tracing] Starting ${sampled ? 'sampled' : 'unsampled'} ${isRootSpan ? 'root ' : ''}span`; | 
|  | 20 | + | 
|  | 21 | +  const infoParts: string[] = [`op: ${op}`, `name: ${description}`, `ID: ${spanId}`]; | 
|  | 22 | + | 
|  | 23 | +  if (parentSpanId) { | 
|  | 24 | +    infoParts.push(`parent ID: ${parentSpanId}`); | 
|  | 25 | +  } | 
|  | 26 | + | 
|  | 27 | +  if (!isRootSpan) { | 
|  | 28 | +    const { op, description } = spanToJSON(rootSpan); | 
|  | 29 | +    infoParts.push(`root ID: ${rootSpan.spanContext().spanId}`); | 
|  | 30 | +    if (op) { | 
|  | 31 | +      infoParts.push(`root op: ${op}`); | 
|  | 32 | +    } | 
|  | 33 | +    if (description) { | 
|  | 34 | +      infoParts.push(`root description: ${description}`); | 
|  | 35 | +    } | 
|  | 36 | +  } | 
|  | 37 | + | 
|  | 38 | +  logger.log(`${header} | 
|  | 39 | +  ${infoParts.join('\n  ')}`); | 
|  | 40 | +} | 
|  | 41 | + | 
|  | 42 | +/** | 
|  | 43 | + * Print a log message for an ended span. | 
|  | 44 | + */ | 
|  | 45 | +export function logSpanEnd(span: Span): void { | 
|  | 46 | +  if (!DEBUG_BUILD) return; | 
|  | 47 | + | 
|  | 48 | +  const { description = '< unknown name >', op = '< unknown op >' } = spanToJSON(span); | 
|  | 49 | +  const { spanId } = span.spanContext(); | 
|  | 50 | +  const rootSpan = getRootSpan(span); | 
|  | 51 | +  const isRootSpan = rootSpan === span; | 
|  | 52 | + | 
|  | 53 | +  const msg = `[Tracing] Finishing "${op}" ${isRootSpan ? 'root ' : ''}span "${description}" with ID ${spanId}`; | 
|  | 54 | +  logger.log(msg); | 
|  | 55 | +} | 
0 commit comments