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