diff --git a/packages/core/src/utils/spanUtils.ts b/packages/core/src/utils/spanUtils.ts index 0878f0b383b3..266a0035b382 100644 --- a/packages/core/src/utils/spanUtils.ts +++ b/packages/core/src/utils/spanUtils.ts @@ -69,7 +69,7 @@ export function spanToTraceHeader(span: Span): string { } /** - * Convert a span time input intp a timestamp in seconds. + * Convert a span time input into a timestamp in seconds. */ export function spanTimeInputToSeconds(input: SpanTimeInput | undefined): number { if (typeof input === 'number') { diff --git a/packages/core/test/lib/utils/spanUtils.test.ts b/packages/core/test/lib/utils/spanUtils.test.ts index 2a4850947e80..d1216378787c 100644 --- a/packages/core/test/lib/utils/spanUtils.test.ts +++ b/packages/core/test/lib/utils/spanUtils.test.ts @@ -43,6 +43,11 @@ describe('spanTimeInputToSeconds', () => { expect(spanTimeInputToSeconds(timestamp)).toEqual(timestamp / 1000); }); + it('works with a timestamp in nanoseconds', () => { + const timestamp = Date.now() * 1e6; + expect(spanTimeInputToSeconds(timestamp)).toEqual(timestamp / 1e9); + }); + it('works with a Date object', () => { const timestamp = new Date(); expect(spanTimeInputToSeconds(timestamp)).toEqual(timestamp.getTime() / 1000); diff --git a/packages/opentelemetry/src/spanExporter.ts b/packages/opentelemetry/src/spanExporter.ts index d139044a713f..3064de5818aa 100644 --- a/packages/opentelemetry/src/spanExporter.ts +++ b/packages/opentelemetry/src/spanExporter.ts @@ -15,6 +15,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getStatusMessage, + spanTimeInputToSeconds, } from '@sentry/core'; import type { SpanJSON, SpanOrigin, TraceContext, TransactionEvent, TransactionSource } from '@sentry/types'; import { dropUndefinedKeys, logger } from '@sentry/utils'; @@ -22,7 +23,6 @@ import { SENTRY_TRACE_STATE_PARENT_SPAN_ID } from './constants'; import { DEBUG_BUILD } from './debug-build'; import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from './semanticAttributes'; -import { convertOtelTimeToSeconds } from './utils/convertOtelTimeToSeconds'; import { getRequestSpanData } from './utils/getRequestSpanData'; import type { SpanNode } from './utils/groupSpansWithParents'; import { getLocalParentId } from './utils/groupSpansWithParents'; @@ -176,7 +176,7 @@ function getCompletedRootNodes(nodes: SpanNode[]): SpanNodeCompleted[] { function shouldCleanupSpan(span: ReadableSpan, maxStartTimeOffsetSeconds: number): boolean { const cutoff = Date.now() / 1000 - maxStartTimeOffsetSeconds; - return convertOtelTimeToSeconds(span.startTime) < cutoff; + return spanTimeInputToSeconds(span.startTime) < cutoff; } function parseSpan(span: ReadableSpan): { op?: string; origin?: SpanOrigin; source?: TransactionSource } { @@ -236,8 +236,8 @@ function createTransactionForOtelSpan(span: ReadableSpan): TransactionEvent { }, }, spans: [], - start_timestamp: convertOtelTimeToSeconds(span.startTime), - timestamp: convertOtelTimeToSeconds(span.endTime), + start_timestamp: spanTimeInputToSeconds(span.startTime), + timestamp: spanTimeInputToSeconds(span.endTime), transaction: description, type: 'transaction', sdkProcessingMetadata: { @@ -294,9 +294,9 @@ function createAndFinishSpanForOtelSpan(node: SpanNode, spans: SpanJSON[], remai data: allData, description, parent_span_id: parentSpanId, - start_timestamp: convertOtelTimeToSeconds(startTime), + start_timestamp: spanTimeInputToSeconds(startTime), // This is [0,0] by default in OTEL, in which case we want to interpret this as no end time - timestamp: convertOtelTimeToSeconds(endTime) || undefined, + timestamp: spanTimeInputToSeconds(endTime) || undefined, status: getStatusMessage(status), // As per protocol, span status is allowed to be undefined op, origin, diff --git a/packages/opentelemetry/src/utils/convertOtelTimeToSeconds.ts b/packages/opentelemetry/src/utils/convertOtelTimeToSeconds.ts deleted file mode 100644 index 112a90572418..000000000000 --- a/packages/opentelemetry/src/utils/convertOtelTimeToSeconds.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** Convert an OTEL time to seconds */ -export function convertOtelTimeToSeconds(otelTime: [number, number] | number): number { - if (!Array.isArray(otelTime)) { - return otelTime; - } - - const [seconds, nano] = otelTime; - - return seconds + nano / 1_000_000_000; -} diff --git a/packages/opentelemetry/test/utils/convertOtelTimeToSeconds.test.ts b/packages/opentelemetry/test/utils/convertOtelTimeToSeconds.test.ts deleted file mode 100644 index 4f4911cee0cb..000000000000 --- a/packages/opentelemetry/test/utils/convertOtelTimeToSeconds.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { convertOtelTimeToSeconds } from '../../src/utils/convertOtelTimeToSeconds'; - -describe('convertOtelTimeToSeconds', () => { - it('works', () => { - expect(convertOtelTimeToSeconds([0, 0])).toEqual(0); - expect(convertOtelTimeToSeconds([1000, 50])).toEqual(1000.00000005); - expect(convertOtelTimeToSeconds([1000, 505])).toEqual(1000.000000505); - }); -});