Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 6f76275

Browse files
committed
refactor: apply code guidelines rules to rootspan.ts file
1 parent cc12672 commit 6f76275

1 file changed

Lines changed: 96 additions & 72 deletions

File tree

packages/opencensus-core/src/trace/model/root-span.ts

Lines changed: 96 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,91 +14,115 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Span } from './span'
18-
import { Clock } from '../../internal/clock'
1917
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'
2318

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';
2524

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[] = [];
2930

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 || '');
4047
}
48+
}
4149

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+
}
4554

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+
}
4964

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();
5468

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+
});
5774

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+
});
6383

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+
}
7486

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+
}
7794

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;
80107
}
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;
102120
}
121+
if (type) {
122+
newSpan.setParentSpanId(parentSpanId || '');
123+
}
124+
newSpan.start();
125+
this.spansLocal.push(newSpan);
126+
return newSpan;
127+
}
103128
}
104-

0 commit comments

Comments
 (0)