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

Commit 3d59c94

Browse files
djonathascardososilva-fabio
authored andcommitted
refactor: create interfaces for model classes
1 parent 7ccff25 commit 3d59c94

6 files changed

Lines changed: 373 additions & 324 deletions

File tree

packages/opencensus-core/src/trace/instrumentation/plugingtypes.ts renamed to packages/opencensus-core/src/trace/instrumentation/types.ts

File renamed without changes.

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

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,109 +18,91 @@ import * as uuid from 'uuid';
1818

1919
import {Clock} from '../../internal/clock';
2020
import {debug} from '../../internal/util';
21-
import {OnEndSpanEventListener, SpanBaseModel, TraceContext, TraceOptions} from '../types/tracetypes';
22-
import {Span} from './span';
23-
import {Tracer} from './tracer';
21+
import {OnEndSpanEventListener, RootSpan, TraceContext, TraceOptions} from '../types';
22+
23+
import {SpanImpl} from './span';
24+
import {SpanBaseModel} from './spanbasemodel';
25+
import {TracerImpl} from './tracer';
2426

2527
/** Defines a root span */
26-
export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
27-
private tracer: Tracer;
28-
readonly traceId: string;
29-
private spansLocal: Span[] = [];
28+
export class RootSpanImpl extends SpanBaseModel implements RootSpan {
29+
private tracer: TracerImpl;
30+
private spansLocal: SpanImpl[];
31+
private traceIdLocal: string;
3032

31-
/**
32-
* Constructs a new RootSpan instance.
33-
* @param tracer
34-
* @param context
35-
*/
36-
constructor(tracer: Tracer, context?: TraceOptions) {
33+
// TODO - improve root name setup
34+
constructor(tracer: TracerImpl, context?: TraceOptions) {
3735
super();
3836
this.tracer = tracer;
39-
this.traceId =
37+
this.traceIdLocal =
4038
context && context.traceContext && context.traceContext.traceId ?
4139
context.traceContext.traceId :
4240
(uuid.v4().split('-').join(''));
43-
// TODO - improve root name setup
4441
this.name = context && context.name ? context.name : 'undefined';
4542
if (context && context.traceContext) {
46-
this.setParentSpanId(context.traceContext.spanId || '');
43+
this.parentSpanId = context.traceContext.spanId || '';
4744
}
45+
this.spansLocal = [];
4846
}
4947

50-
/** Returns a list of the trace's spans. */
5148
get spans() {
5249
return this.spansLocal;
5350
}
5451

55-
/** Starts the root span. */
52+
get traceId() {
53+
return this.traceIdLocal;
54+
}
55+
5656
start() {
5757
super.start();
58-
debug('starting %s %o', this._className, {
59-
traceId: this.traceId,
60-
id: this.id,
61-
parentSpanId: this.getParentSpanId()
62-
});
58+
debug(
59+
'starting %s %o', this.className,
60+
{traceId: this.traceId, id: this.id, parentSpanId: this.parentSpanId});
6361
}
6462

65-
/** Ends the root span. */
6663
end() {
6764
super.end();
6865

6966
// TODO - Define logic for list of spans
70-
this.spansLocal.map(span => {
67+
for (const span of this.spansLocal) {
7168
if (span.ended || !span.started) return;
7269
span.truncate();
73-
});
74-
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-
});
70+
}
8371

8472
this.tracer.onEndSpan(this);
8573
}
8674

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});
75+
onEndSpan(span: SpanImpl) {
76+
debug('ending span %o', {
77+
id: span.id,
78+
traceId: span.traceId,
79+
name: span.name,
80+
startTime: span.startTime,
81+
endTime: span.endTime,
82+
duration: span.duration
83+
});
9384
}
9485

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-
*/
10186
startSpan(name: string, type: string, parentSpanId?: string) {
10287
if (!this.started) {
10388
debug(
104-
'calling %s.startSpan() on un-started %s %o', this._className,
105-
this._className, {id: this.id, name: this.name, type: this.type});
89+
'calling %s.startSpan() on un-started %s %o', this.className,
90+
this.className, {id: this.id, name: this.name, type: this.type});
10691
return;
10792
}
10893
if (this.ended) {
10994
debug(
110-
'calling %s.startSpan() on ended %s %o', this._className,
111-
this._className, {id: this.id, name: this.name, type: this.type});
95+
'calling %s.startSpan() on ended %s %o', this.className,
96+
this.className, {id: this.id, name: this.name, type: this.type});
11297
return;
11398
}
114-
const newSpan = new Span(this);
99+
const newSpan = new SpanImpl(this);
115100
if (name) {
116101
newSpan.name = name;
117102
}
118103
if (type) {
119104
newSpan.type = type;
120105
}
121-
if (type) {
122-
newSpan.setParentSpanId(parentSpanId || '');
123-
}
124106
newSpan.start();
125107
this.spansLocal.push(newSpan);
126108
return newSpan;
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import { Clock } from '../../internal/clock';
2+
import { debug, randomSpanId } from '../../internal/util';
3+
import { Sampler } from '../config/sampler';
4+
import { TraceContext } from '../types';
5+
6+
export interface MapLabels { [propName: string]: string; }
7+
export interface MapObjects { [propName: string]: any; }
8+
9+
export abstract class SpanBaseModel {
10+
protected className: string;
11+
/** The clock used to mesure the beginning and ending of a span */
12+
private clock: Clock = null;
13+
/** Indicates if this span was started */
14+
private startedLocal = false;
15+
/** Indicates if this span was ended */
16+
private endedLocal = false;
17+
/** A set of attributes, each in the format [KEY]:[VALUE] */
18+
private attributes: MapLabels = {};
19+
/** A set of attributes on the annotation describing an event */
20+
private annotations: MapObjects = {};
21+
/** Indicates if this span was forced to end */
22+
private truncated = false;
23+
/** The Span ID of this span */
24+
readonly id: string;
25+
remoteParent: string;
26+
/** The span ID of this span's parent. If it's a root span, must be empty */
27+
parentSpanId = '';
28+
/** The resource name of the span */
29+
name: string = null;
30+
/** Type of span. Used to specify additional relationships between spans */
31+
type: string = null;
32+
/** A final status for this span */
33+
status: number;
34+
/** A sampler that will decide if the span will be sampled or not */
35+
sampler: Sampler;
36+
37+
/** Constructs a new SpanBaseModel instance. */
38+
constructor() {
39+
this.className = this.constructor.name;
40+
this.id = randomSpanId();
41+
}
42+
43+
abstract get traceId(): string;
44+
45+
/** Indicates if span was started. */
46+
get started(): boolean {
47+
return this.startedLocal;
48+
}
49+
50+
/** Indicates if span was ended. */
51+
get ended(): boolean {
52+
return this.endedLocal;
53+
}
54+
55+
/**
56+
* Gives a timestap that indicates the span's start time in RFC3339 UTC
57+
* "Zulu" format.
58+
*/
59+
get startTime(): Date {
60+
if (this.clock) return this.clock.startTime;
61+
}
62+
63+
/**
64+
* Gives a timestap that indicates the span's end time in RFC3339 UTC
65+
* "Zulu" format.
66+
*/
67+
get endTime(): Date {
68+
if (this.clock) return this.clock.endTime;
69+
}
70+
71+
/**
72+
* Gives a timestap that indicates the span's duration in RFC3339 UTC
73+
* "Zulu" format.
74+
*/
75+
get duration(): number {
76+
if (this.clock) return this.clock.duration;
77+
}
78+
79+
/** Gives the TraceContext of the span. */
80+
get traceContext(): TraceContext {
81+
return {
82+
traceId: this.traceId.toString(),
83+
spanId: this.id.toString(),
84+
parentSpanId: this.parentSpanId
85+
} as TraceContext;
86+
}
87+
88+
/**
89+
* Adds an atribute to the span.
90+
* @param key Describes the value added.
91+
* @param value The result of an operation.
92+
*/
93+
addAtribute(key: string, value: string) {
94+
// TODO: maybe key and values must be truncate
95+
this.attributes[key] = value;
96+
}
97+
98+
/**
99+
* Adds an annotation to the span.
100+
* @param key Describes the value added.
101+
* @param value The result of an operation.
102+
*/
103+
addAnotation(key: string, value: string|number|boolean) {
104+
// TODO: maybe keys and values must be truncate
105+
this.annotations[key] = value;
106+
}
107+
108+
/** Starts a span. */
109+
start() {
110+
if (this.started) {
111+
debug(
112+
'calling %s.start() on already started %s %o', this.className,
113+
this.className, {id: this.id, name: this.name, type: this.type});
114+
return;
115+
}
116+
this.clock = new Clock();
117+
this.startedLocal = true;
118+
}
119+
120+
/** Ends a span. */
121+
end(): void {
122+
if (!this.started) {
123+
debug(
124+
'calling %s.end() on un-started %s %o', this.className,
125+
this.className, {id: this.id, name: this.name, type: this.type});
126+
return;
127+
}
128+
if (this.ended) {
129+
debug(
130+
'calling %s.end() on already ended %s %o', this.className,
131+
this.className, {id: this.id, name: this.name, type: this.type});
132+
return;
133+
}
134+
this.startedLocal = false;
135+
this.endedLocal = true;
136+
this.clock.end();
137+
}
138+
139+
140+
/** Forces to end a span. */
141+
truncate() {
142+
// TODO: review
143+
if (!this.started) {
144+
debug(
145+
'calling truncate non-started %s - ignoring %o', this.className,
146+
{id: this.id, name: this.name, type: this.type});
147+
return;
148+
} else if (this.ended) {
149+
debug(
150+
'calling truncate already ended %s - ignoring %o', this.className,
151+
{id: this.id, name: this.name, type: this.type});
152+
return;
153+
}
154+
this.truncated = true;
155+
this.end();
156+
debug('truncating %s %o', this.className, {id: this.id, name: this.name});
157+
}
158+
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@
1616

1717
import {Clock} from '../../internal/clock';
1818
import {debug, randomSpanId} from '../../internal/util';
19-
import {SpanBaseModel, TraceContext} from '../types/tracetypes';
20-
import {RootSpan} from './rootspan';
19+
import {Span, TraceContext} from '../types';
20+
21+
import {RootSpanImpl} from './rootspan';
22+
import {SpanBaseModel} from './spanbasemodel';
2123

2224
/**
2325
* This class represent a span.
2426
*/
25-
export class Span extends SpanBaseModel {
26-
private root: RootSpan;
27+
export class SpanImpl extends SpanBaseModel implements Span {
28+
private root: RootSpanImpl;
2729

2830
/**
2931
* Constructs a new Span instance.
3032
* @param root
3133
*/
32-
constructor(root: RootSpan) {
34+
constructor(root: RootSpanImpl) {
3335
super();
3436
this.root = root;
3537
}

0 commit comments

Comments
 (0)