Skip to content

Commit

Permalink
Replace with spanTimeInputToSeconds
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Jul 1, 2024
1 parent 5c25ba1 commit ae4a0f2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export {
getRootSpan,
getActiveSpan,
addChildSpanToSpan,
spanTimeInputToSeconds,
} from './utils/spanUtils';
export { parseSampleRate } from './utils/parseSampleRate';
export { applySdkMetadata } from './utils/sdkMetadata';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/spanUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/lib/utils/spanUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions packages/opentelemetry/src/spanExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ 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';
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';
Expand Down Expand Up @@ -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 } {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 0 additions & 10 deletions packages/opentelemetry/src/utils/convertOtelTimeToSeconds.ts

This file was deleted.

This file was deleted.

0 comments on commit ae4a0f2

Please sign in to comment.