|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import { Span } from './span' |
18 | | -import { Clock } from '../../internal/clock' |
19 | 17 | import * as uuid from 'uuid'; |
20 | | -import { debug } from '../../internal/util' |
21 | | -import { SpanBaseModel, TraceOptions, TraceContext, OnEndSpanEventListener } from '../types/tracetypes' |
22 | | -import { Tracer } from './tracer' |
23 | 18 |
|
24 | | -export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener { |
| 19 | +import {Clock} from '../../internal/clock'; |
| 20 | +import {debug} from '../../internal/util'; |
| 21 | +import {OnEndSpanEventListener, SpanBaseModel, TraceContext, TraceOptions} from '../types/tracetypes'; |
| 22 | +import {Span} from './span'; |
| 23 | +import {Tracer} from './tracer'; |
25 | 24 |
|
26 | | - private tracer: Tracer; |
27 | | - private _spans: Span[]; |
28 | | - private _traceId: string; |
| 25 | +/** Defines a root span */ |
| 26 | +export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener { |
| 27 | + private tracer: Tracer; |
| 28 | + readonly traceId: string; |
| 29 | + private spansLocal: Span[] = []; |
29 | 30 |
|
30 | | - //TODO - improve root name setup |
31 | | - constructor(tracer: Tracer, context?: TraceOptions) { |
32 | | - super() |
33 | | - this.tracer = tracer; |
34 | | - this._traceId = context && context.traceContext && context.traceContext.traceId ? context.traceContext.traceId : (uuid.v4().split('-').join('')); |
35 | | - this.name = context && context.name ? context.name : 'undefined'; |
36 | | - if (context && context.traceContext) { |
37 | | - this.setParentSpanId(context.traceContext.spanId || '') |
38 | | - } |
39 | | - this._spans = []; |
| 31 | + /** |
| 32 | + * Constructs a new RootSpan instance. |
| 33 | + * @param tracer |
| 34 | + * @param context |
| 35 | + */ |
| 36 | + constructor(tracer: Tracer, context?: TraceOptions) { |
| 37 | + super(); |
| 38 | + this.tracer = tracer; |
| 39 | + this.traceId = |
| 40 | + context && context.traceContext && context.traceContext.traceId ? |
| 41 | + context.traceContext.traceId : |
| 42 | + (uuid.v4().split('-').join('')); |
| 43 | + // TODO - improve root name setup |
| 44 | + this.name = context && context.name ? context.name : 'undefined'; |
| 45 | + if (context && context.traceContext) { |
| 46 | + this.setParentSpanId(context.traceContext.spanId || ''); |
40 | 47 | } |
| 48 | + } |
41 | 49 |
|
42 | | - public get spans() { |
43 | | - return this._spans; |
44 | | - } |
| 50 | + /** Returns a list of the trace's spans. */ |
| 51 | + get spans() { |
| 52 | + return this.spansLocal; |
| 53 | + } |
45 | 54 |
|
46 | | - public get traceId() { |
47 | | - return this._traceId; |
48 | | - } |
| 55 | + /** Starts the root span. */ |
| 56 | + start() { |
| 57 | + super.start(); |
| 58 | + debug('starting %s %o', this._className, { |
| 59 | + traceId: this.traceId, |
| 60 | + id: this.id, |
| 61 | + parentSpanId: this.getParentSpanId() |
| 62 | + }); |
| 63 | + } |
49 | 64 |
|
50 | | - public start() { |
51 | | - super.start() |
52 | | - debug('starting %s %o', this._className, { traceId: this.traceId, id: this.id, parentSpanId: this.getParentSpanId() }) |
53 | | - } |
| 65 | + /** Ends the root span. */ |
| 66 | + end() { |
| 67 | + super.end(); |
54 | 68 |
|
55 | | - public end() { |
56 | | - super.end() |
| 69 | + // TODO - Define logic for list of spans |
| 70 | + this.spansLocal.map(span => { |
| 71 | + if (span.ended || !span.started) return; |
| 72 | + span.truncate(); |
| 73 | + }); |
57 | 74 |
|
58 | | - //TODO - Define logic for list of spans |
59 | | - this._spans.forEach(function (span) { |
60 | | - if (span.ended || !span.started) return |
61 | | - span.truncate() |
62 | | - }) |
| 75 | + debug('ending %s %o', this._className, { |
| 76 | + id: this.id, |
| 77 | + traceId: this.traceId, |
| 78 | + name: this.name, |
| 79 | + startTime: this.startTime, |
| 80 | + endTime: this.endTime, |
| 81 | + duration: this.duration |
| 82 | + }); |
63 | 83 |
|
64 | | - debug('ending %s %o', |
65 | | - this._className, |
66 | | - { |
67 | | - id: this.id, |
68 | | - traceId: this.traceId, |
69 | | - name: this.name, |
70 | | - startTime: this.startTime, |
71 | | - endTime: this.endTime, |
72 | | - duration: this.duration |
73 | | - }) |
| 84 | + this.tracer.onEndSpan(this); |
| 85 | + } |
74 | 86 |
|
75 | | - this.tracer.onEndSpan(this) |
76 | | - } |
| 87 | + /** |
| 88 | + * Happens when a span is ended. |
| 89 | + * @param span |
| 90 | + */ |
| 91 | + onEndSpan(span: Span) { |
| 92 | + debug('%s notified ending by %o', {id: span.id, name: span.name}); |
| 93 | + } |
77 | 94 |
|
78 | | - public onEndSpan(span: Span) { |
79 | | - debug('%s notified ending by %o', { id: span.id, name: span.name }) |
| 95 | + /** |
| 96 | + * Starts a span inside the respective trace. |
| 97 | + * @param name Span's name |
| 98 | + * @param type Spans's type |
| 99 | + * @param parentSpanId Span's parent ID |
| 100 | + */ |
| 101 | + startSpan(name: string, type: string, parentSpanId?: string) { |
| 102 | + if (!this.started) { |
| 103 | + debug( |
| 104 | + 'calling %s.startSpan() on un-started %s %o', this._className, |
| 105 | + this._className, {id: this.id, name: this.name, type: this.type}); |
| 106 | + return; |
80 | 107 | } |
81 | | - |
82 | | - public startSpan(name: string, type: string, parentSpanId?: string) { |
83 | | - if (!this.started) { |
84 | | - debug('calling %s.startSpan() on un-started %s %o', |
85 | | - this._className, this._className, |
86 | | - { id: this.id, name: this.name, type: this.type }) |
87 | | - return |
88 | | - } |
89 | | - if (this.ended) { |
90 | | - debug('calling %s.startSpan() on ended %s %o', |
91 | | - this._className, this._className, |
92 | | - { id: this.id, name: this.name, type: this.type }) |
93 | | - return |
94 | | - } |
95 | | - let newSpan = new Span(this); |
96 | | - if (name) { newSpan.name = name } |
97 | | - if (type) { newSpan.type = type } |
98 | | - if (type) { newSpan.setParentSpanId(parentSpanId || '') } |
99 | | - newSpan.start(); |
100 | | - this._spans.push(newSpan); |
101 | | - return newSpan; |
| 108 | + if (this.ended) { |
| 109 | + debug( |
| 110 | + 'calling %s.startSpan() on ended %s %o', this._className, |
| 111 | + this._className, {id: this.id, name: this.name, type: this.type}); |
| 112 | + return; |
| 113 | + } |
| 114 | + const newSpan = new Span(this); |
| 115 | + if (name) { |
| 116 | + newSpan.name = name; |
| 117 | + } |
| 118 | + if (type) { |
| 119 | + newSpan.type = type; |
102 | 120 | } |
| 121 | + if (type) { |
| 122 | + newSpan.setParentSpanId(parentSpanId || ''); |
| 123 | + } |
| 124 | + newSpan.start(); |
| 125 | + this.spansLocal.push(newSpan); |
| 126 | + return newSpan; |
| 127 | + } |
103 | 128 | } |
104 | | - |
|
0 commit comments