@@ -3,18 +3,25 @@ import * as opentracing from 'opentracing';
3
3
import { SpanContext } from './spancontext' ;
4
4
import { Tracer } from './tracer' ;
5
5
6
- /** JSDoc */
6
+ /**
7
+ * Interface for log entries.
8
+ */
7
9
interface Log {
8
10
data : { [ key : string ] : any } ;
9
11
timestamp ?: number ;
10
12
}
11
13
12
- /** JSDoc */
14
+ /**
15
+ * Span represents a logical unit of work as part of a broader Trace. Examples
16
+ * of span might include remote procedure calls or a in-process function calls
17
+ * to sub-components. A Trace has a single, top-level "root" Span that in turn
18
+ * may have zero or more child Spans, which in turn may have children.
19
+ */
13
20
export class Span extends opentracing . Span implements SpanInterface {
14
- private flushed : boolean = false ;
15
- private finishTime : number = 0 ;
21
+ private _flushed : boolean = false ;
22
+ private _finishTime : number = 0 ;
16
23
17
- private readonly logs : Log [ ] = [ ] ;
24
+ private readonly _logs : Log [ ] = [ ] ;
18
25
19
26
public tags : {
20
27
[ key : string ] : string ;
@@ -25,11 +32,11 @@ export class Span extends opentracing.Span implements SpanInterface {
25
32
} = { } ;
26
33
27
34
public constructor (
28
- private readonly usedTracer : Tracer ,
29
- private operation : string ,
30
- private readonly spanContext : SpanContext ,
31
- private readonly references ?: opentracing . Reference [ ] ,
32
- private readonly startTime : number = Date . now ( ) ,
35
+ private readonly _usedTracer : Tracer ,
36
+ private _operation : string ,
37
+ private readonly _spanContext : SpanContext ,
38
+ private readonly _references ?: opentracing . Reference [ ] ,
39
+ private readonly _startTime : number = Date . now ( ) ,
33
40
) {
34
41
super ( ) ;
35
42
}
@@ -38,29 +45,33 @@ export class Span extends opentracing.Span implements SpanInterface {
38
45
* Returns the context.
39
46
*/
40
47
protected _context ( ) : SpanContext {
41
- return this . spanContext ;
48
+ return this . _spanContext ;
42
49
}
43
50
44
51
/**
45
52
* Returns the tracer passed to the span.
46
53
*/
47
54
protected _tracer ( ) : Tracer {
48
- return this . usedTracer ;
55
+ return this . _usedTracer ;
49
56
}
50
57
51
58
/**
52
59
* Sets the operation name.
53
60
*/
54
61
protected _setOperationName ( name : string ) : void {
55
- this . operation = name ;
62
+ this . _operation = name ;
56
63
}
57
64
58
- /** JSDoc */
65
+ /**
66
+ * Implementation for {@link setBaggageItem}
67
+ */
59
68
protected _setBaggageItem ( key : string , value : string ) : void {
60
69
this . baggage [ key ] = value ;
61
70
}
62
71
63
- /** JSDoc */
72
+ /**
73
+ * Implementation for {@link getBaggageItem}
74
+ */
64
75
protected _getBaggageItem ( key : string ) : string | undefined {
65
76
return this . baggage [ key ] ;
66
77
}
@@ -78,69 +89,69 @@ export class Span extends opentracing.Span implements SpanInterface {
78
89
/**
79
90
* Store log entry.
80
91
*/
81
- protected _log ( data : { [ key : string ] : any } , timestamp ? : number ) : void {
82
- this . logs . push ( {
92
+ protected _log ( data : { [ key : string ] : any } , timestamp : number = Date . now ( ) / 1000 ) : void {
93
+ this . _logs . push ( {
83
94
data,
84
95
timestamp,
85
96
} ) ;
86
97
}
87
98
88
99
/**
89
- * JSDoc
100
+ * Implementation for { @link finish}
90
101
*/
91
- protected _finish ( finishTime ? : number ) : void {
92
- this . finishTime = finishTime || Date . now ( ) ;
102
+ protected _finish ( finishTime : number = Date . now ( ) ) : void {
103
+ this . _finishTime = finishTime ;
93
104
}
94
105
95
106
/**
96
107
* Returns the operationName.
97
108
*/
98
109
public getOperationName ( ) : string {
99
- return this . operation ;
110
+ return this . _operation ;
100
111
}
101
112
102
113
/**
103
114
* Returns the duration of the span.
104
115
*/
105
116
public duration ( ) : number {
106
- return this . finishTime - this . startTime ;
117
+ return this . _finishTime - this . _startTime ;
107
118
}
108
119
109
120
/**
110
121
* Returns wether the span has been finished.
111
122
*/
112
123
public isFinished ( ) : boolean {
113
- return this . finishTime > 0 ;
124
+ return this . _finishTime > 0 ;
114
125
}
115
126
116
127
/**
117
128
* Marks the span as flushed.
118
129
*/
119
130
public flush ( ) : this {
120
- this . flushed = true ;
131
+ this . _flushed = true ;
121
132
return this ;
122
133
}
123
134
124
135
/**
125
136
* Returns wether the span has already be flushed.
126
137
*/
127
138
public isFlushed ( ) : boolean {
128
- return this . flushed ;
139
+ return this . _flushed ;
129
140
}
130
141
131
142
/**
132
143
* @inheritdoc
133
144
*/
134
145
public toJSON ( ) : object {
135
146
return {
136
- finish_time : ( this . finishTime && this . finishTime / 1000 ) || undefined ,
137
- logs : this . logs . length === 0 ? undefined : this . logs ,
138
- operation : this . operation ,
139
- references : this . references && this . references ,
140
- span_id : this . spanContext . spanId ,
141
- start_time : this . startTime / 1000 ,
147
+ finish_time : ( this . _finishTime && this . _finishTime / 1000 ) || undefined ,
148
+ logs : this . _logs . length === 0 ? undefined : this . _logs ,
149
+ operation : this . _operation ,
150
+ references : this . _references && this . _references ,
151
+ span_id : this . _spanContext . spanId ,
152
+ start_time : this . _startTime / 1000 ,
142
153
tags : Object . keys ( this . tags ) . length === 0 ? undefined : this . tags ,
143
- trace_id : this . spanContext . traceId ,
154
+ trace_id : this . _spanContext . traceId ,
144
155
} ;
145
156
}
146
157
}
0 commit comments