diff --git a/api/src/trace/NonRecordingSpan.ts b/api/src/trace/NonRecordingSpan.ts index a9e5bcaf9be..3908c650801 100644 --- a/api/src/trace/NonRecordingSpan.ts +++ b/api/src/trace/NonRecordingSpan.ts @@ -15,7 +15,7 @@ */ import { Exception } from '../common/Exception'; -import { TimeInput } from '../common/Time'; +import { HrTime, TimeInput } from '../common/Time'; import { SpanAttributes } from './attributes'; import { INVALID_SPAN_CONTEXT } from './invalid-span-constants'; import { Span } from './span'; @@ -72,4 +72,8 @@ export class NonRecordingSpan implements Span { // By default does nothing recordException(_exception: Exception, _time?: TimeInput): void {} + + currentTime(): HrTime { + return [0, 0]; + } } diff --git a/api/src/trace/span.ts b/api/src/trace/span.ts index d80b8c2626d..bd4c36abd4f 100644 --- a/api/src/trace/span.ts +++ b/api/src/trace/span.ts @@ -15,7 +15,7 @@ */ import { Exception } from '../common/Exception'; -import { TimeInput } from '../common/Time'; +import { HrTime, TimeInput } from '../common/Time'; import { SpanAttributes, SpanAttributeValue } from './attributes'; import { SpanContext } from './span_context'; import { SpanStatus } from './status'; @@ -126,4 +126,10 @@ export interface Span { * use the current time. */ recordException(exception: Exception, time?: TimeInput): void; + + /** + * Returns current time based on the anchored clock. + * Used to snapshot time when the span has to be ended later. + */ + currentTime(): HrTime; } diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index f30c185bce8..fb4fffba046 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -278,7 +278,7 @@ export class FetchInstrumentation extends InstrumentationBase> spanData: SpanData, response: FetchResponse ) { - const endTime = core.hrTime(); + const endTime = span.currentTime(); this._addFinalSpanAttributes(span, response); setTimeout(() => { @@ -308,7 +308,8 @@ export class FetchInstrumentation extends InstrumentationBase> if (!createdSpan) { return original.apply(this, args); } - const spanData = plugin._prepareSpanData(url); + const startTime = createdSpan.currentTime(); + const spanData = plugin._prepareSpanData(url, startTime); function endSpanOnError(span: api.Span, error: FetchError) { plugin._applyAttributesAfterFetch(span, options, error); @@ -427,8 +428,7 @@ export class FetchInstrumentation extends InstrumentationBase> * resources * @param spanUrl */ - private _prepareSpanData(spanUrl: string): SpanData { - const startTime = core.hrTime(); + private _prepareSpanData(spanUrl: string, startTime: api.HrTime): SpanData { const entries: PerformanceResourceTiming[] = []; if (typeof PerformanceObserver !== 'function') { return { entries, startTime, spanUrl }; diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts index 062d6fde4ac..4cc7c1d4480 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts @@ -21,7 +21,7 @@ import { InstrumentationConfig, safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation'; -import { hrTime, isUrlIgnored, otperformance } from '@opentelemetry/core'; +import { isUrlIgnored, otperformance } from '@opentelemetry/core'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import { addSpanNetworkEvents, @@ -431,7 +431,7 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase { plugin._tasksCount++; - xhrMem.sendStartTime = hrTime(); + xhrMem.sendStartTime = currentSpan.currentTime(); currentSpan.addEvent(EventNames.METHOD_SEND); this.addEventListener('abort', onAbort); diff --git a/packages/opentelemetry-sdk-trace-base/src/Span.ts b/packages/opentelemetry-sdk-trace-base/src/Span.ts index fe0b0d04704..63108885916 100644 --- a/packages/opentelemetry-sdk-trace-base/src/Span.ts +++ b/packages/opentelemetry-sdk-trace-base/src/Span.ts @@ -243,6 +243,10 @@ export class Span implements api.Span, ReadableSpan { return this._ended; } + currentTime(): api.HrTime { + return timeInputToHrTime(this._clock.now()); + } + private _isSpanEnded(): boolean { if (this._ended) { api.diag.warn(`Can not execute the operation on ended Span {traceId: ${this._spanContext.traceId}, spanId: ${this._spanContext.spanId}}`);