Skip to content

Commit 6481e3d

Browse files
authored
refactor: cal duration once instead of each get duration call (#412)
1 parent d60d3dc commit 6481e3d

File tree

1 file changed

+10
-14
lines changed
  • packages/opentelemetry-tracing/src

1 file changed

+10
-14
lines changed

packages/opentelemetry-tracing/src/Span.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ export class Span implements types.Span, ReadableSpan {
141141
}
142142
this._ended = true;
143143
this.endTime = timeInputToHrTime(endTime);
144+
145+
this._duration = hrTimeDuration(this.startTime, this.endTime);
146+
if (this._duration[0] < 0) {
147+
this._logger.warn(
148+
'Inconsistent start and end time, startTime > endTime',
149+
this.startTime,
150+
this.endTime
151+
);
152+
}
153+
144154
this._spanProcessor.onEnd(this);
145155
}
146156

@@ -153,20 +163,6 @@ export class Span implements types.Span, ReadableSpan {
153163
}
154164

155165
get duration(): types.HrTime {
156-
if (this._duration[0] !== -1) {
157-
return this._duration;
158-
}
159-
160-
this._duration = hrTimeDuration(this.startTime, this.endTime);
161-
162-
if (this._duration[0] < 0) {
163-
this._logger.warn(
164-
'Inconsistent start and end time, startTime > endTime',
165-
this.startTime,
166-
this.endTime
167-
);
168-
}
169-
170166
return this._duration;
171167
}
172168

0 commit comments

Comments
 (0)