15
15
*/
16
16
17
17
import * as api from '@opentelemetry/api' ;
18
+ import { Context , SpanAttributeValue } from '@opentelemetry/api' ;
18
19
import {
19
- isAttributeValue ,
20
- hrTime ,
20
+ Clock ,
21
21
hrTimeDuration ,
22
22
InstrumentationLibrary ,
23
+ isAttributeValue ,
23
24
isTimeInput ,
24
- timeInputToHrTime ,
25
+ otperformance ,
25
26
sanitizeAttributes ,
27
+ timeInputToHrTime
26
28
} from '@opentelemetry/core' ;
27
29
import { Resource } from '@opentelemetry/resources' ;
28
30
import { SemanticAttributes } from '@opentelemetry/semantic-conventions' ;
31
+ import { ExceptionEventName } from './enums' ;
29
32
import { ReadableSpan } from './export/ReadableSpan' ;
33
+ import { SpanProcessor } from './SpanProcessor' ;
30
34
import { TimedEvent } from './TimedEvent' ;
31
35
import { Tracer } from './Tracer' ;
32
- import { SpanProcessor } from './SpanProcessor' ;
33
36
import { SpanLimits } from './types' ;
34
- import { SpanAttributeValue , Context } from '@opentelemetry/api' ;
35
- import { ExceptionEventName } from './enums' ;
36
37
37
38
/**
38
39
* This class represents a span.
@@ -59,8 +60,13 @@ export class Span implements api.Span, ReadableSpan {
59
60
private readonly _spanProcessor : SpanProcessor ;
60
61
private readonly _spanLimits : SpanLimits ;
61
62
private readonly _attributeValueLengthLimit : number ;
63
+ private readonly _clock : Clock ;
62
64
63
- /** Constructs a new Span instance. */
65
+ /**
66
+ * Constructs a new Span instance.
67
+ *
68
+ * @deprecated calling Span constructor directly is not supported. Please use tracer.startSpan.
69
+ * */
64
70
constructor (
65
71
parentTracer : Tracer ,
66
72
context : Context ,
@@ -69,14 +75,16 @@ export class Span implements api.Span, ReadableSpan {
69
75
kind : api . SpanKind ,
70
76
parentSpanId ?: string ,
71
77
links : api . Link [ ] = [ ] ,
72
- startTime : api . TimeInput = hrTime ( )
78
+ startTime ?: api . TimeInput ,
79
+ clock : Clock = otperformance ,
73
80
) {
81
+ this . _clock = clock ;
74
82
this . name = spanName ;
75
83
this . _spanContext = spanContext ;
76
84
this . parentSpanId = parentSpanId ;
77
85
this . kind = kind ;
78
86
this . links = links ;
79
- this . startTime = timeInputToHrTime ( startTime ) ;
87
+ this . startTime = timeInputToHrTime ( startTime ?? clock . now ( ) ) ;
80
88
this . resource = parentTracer . resource ;
81
89
this . instrumentationLibrary = parentTracer . instrumentationLibrary ;
82
90
this . _spanLimits = parentTracer . getSpanLimits ( ) ;
@@ -103,7 +111,7 @@ export class Span implements api.Span, ReadableSpan {
103
111
104
112
if (
105
113
Object . keys ( this . attributes ) . length >=
106
- this . _spanLimits . attributeCountLimit ! &&
114
+ this . _spanLimits . attributeCountLimit ! &&
107
115
! Object . prototype . hasOwnProperty . call ( this . attributes , key )
108
116
) {
109
117
return this ;
@@ -147,7 +155,7 @@ export class Span implements api.Span, ReadableSpan {
147
155
attributesOrStartTime = undefined ;
148
156
}
149
157
if ( typeof startTime === 'undefined' ) {
150
- startTime = hrTime ( ) ;
158
+ startTime = this . _clock . now ( ) ;
151
159
}
152
160
153
161
const attributes = sanitizeAttributes ( attributesOrStartTime ) ;
@@ -171,15 +179,16 @@ export class Span implements api.Span, ReadableSpan {
171
179
return this ;
172
180
}
173
181
174
- end ( endTime : api . TimeInput = hrTime ( ) ) : void {
182
+ end ( endTime ? : api . TimeInput ) : void {
175
183
if ( this . _isSpanEnded ( ) ) {
176
184
api . diag . error ( 'You can only call end() on a span once.' ) ;
177
185
return ;
178
186
}
179
187
this . _ended = true ;
180
- this . endTime = timeInputToHrTime ( endTime ) ;
181
188
189
+ this . endTime = timeInputToHrTime ( endTime ?? this . _clock . now ( ) ) ;
182
190
this . _duration = hrTimeDuration ( this . startTime , this . endTime ) ;
191
+
183
192
if ( this . _duration [ 0 ] < 0 ) {
184
193
api . diag . warn (
185
194
'Inconsistent start and end time, startTime > endTime' ,
@@ -195,7 +204,7 @@ export class Span implements api.Span, ReadableSpan {
195
204
return this . _ended === false ;
196
205
}
197
206
198
- recordException ( exception : api . Exception , time : api . TimeInput = hrTime ( ) ) : void {
207
+ recordException ( exception : api . Exception , time : api . TimeInput = this . _clock . now ( ) ) : void {
199
208
const attributes : api . SpanAttributes = { } ;
200
209
if ( typeof exception === 'string' ) {
201
210
attributes [ SemanticAttributes . EXCEPTION_MESSAGE ] = exception ;
0 commit comments