Skip to content

Commit 5a04fcb

Browse files
committed
feat(opentelemetry): Use core getRootSpan functionality
1 parent e249f36 commit 5a04fcb

File tree

9 files changed

+12
-24
lines changed

9 files changed

+12
-24
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export {
9393
getStatusMessage,
9494
getRootSpan,
9595
getActiveSpan,
96+
addChildSpanToSpan,
9697
} from './utils/spanUtils';
9798
export { applySdkMetadata } from './utils/sdkMetadata';
9899
export { DEFAULT_ENVIRONMENT } from './constants';

packages/node-experimental/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ export { cron } from './cron';
4141

4242
export type { NodeOptions } from './types';
4343

44-
export { getRootSpan } from '@sentry/opentelemetry';
45-
4644
export {
4745
addRequestDataToEvent,
4846
DEFAULT_USER_INCLUDES,
@@ -106,6 +104,7 @@ export {
106104
startInactiveSpan,
107105
getActiveSpan,
108106
withActiveSpan,
107+
getRootSpan,
109108
} from '@sentry/core';
110109

111110
export type {

packages/opentelemetry/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export {
2626

2727
export { isSentryRequestSpan } from './utils/isSentryRequest';
2828

29-
export { getActiveSpan, getRootSpan } from './utils/getActiveSpan';
29+
export { getActiveSpan } from './utils/getActiveSpan';
3030
export { startSpan, startSpanManual, startInactiveSpan, withActiveSpan } from './trace';
3131

3232
// eslint-disable-next-line deprecation/deprecation

packages/opentelemetry/src/setupEventContextTrace.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { getRootSpan } from '@sentry/core';
12
import type { Client } from '@sentry/types';
23
import { dropUndefinedKeys } from '@sentry/utils';
34

4-
import { getActiveSpan, getRootSpan } from './utils/getActiveSpan';
5+
import { getActiveSpan } from './utils/getActiveSpan';
56
import { spanHasName, spanHasParentId } from './utils/spanTypes';
67

78
/** Ensure the `trace` context is set on all events. */

packages/opentelemetry/src/spanProcessor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Context } from '@opentelemetry/api';
22
import { ROOT_CONTEXT, trace } from '@opentelemetry/api';
33
import type { Span, SpanProcessor as SpanProcessorInterface } from '@opentelemetry/sdk-trace-base';
44
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
5-
import { getClient, getDefaultCurrentScope, getDefaultIsolationScope } from '@sentry/core';
5+
import { addChildSpanToSpan, getClient, getDefaultCurrentScope, getDefaultIsolationScope } from '@sentry/core';
66
import { logger } from '@sentry/utils';
77

88
import { DEBUG_BUILD } from './debug-build';
@@ -21,6 +21,7 @@ function onSpanStart(span: Span, parentContext: Context): void {
2121
// We need access to the parent span in order to be able to move up the span tree for breadcrumbs
2222
if (parentSpan) {
2323
setSpanParent(span, parentSpan);
24+
addChildSpanToSpan(parentSpan, span);
2425
}
2526

2627
// The root context does not have scopes stored, so we check for this specifically

packages/opentelemetry/src/trace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1111
getClient,
1212
getCurrentScope,
13+
getRootSpan,
1314
handleCallbackErrors,
1415
} from '@sentry/core';
1516
import type { Client, Scope } from '@sentry/types';
@@ -19,7 +20,6 @@ import { SENTRY_TRACE_STATE_DSC } from './constants';
1920
import type { OpenTelemetryClient, OpenTelemetrySpanContext } from './types';
2021
import { getContextFromScope } from './utils/contextData';
2122
import { getDynamicSamplingContextFromSpan } from './utils/dynamicSamplingContext';
22-
import { getRootSpan } from './utils/getActiveSpan';
2323
import { setSpanMetadata } from './utils/spanData';
2424

2525
/**
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
import type { Span } from '@opentelemetry/api';
22
import { trace } from '@opentelemetry/api';
33

4-
import { getSpanParent } from './spanData';
5-
64
/**
75
* Returns the currently active span.
86
*/
97
export function getActiveSpan(): Span | undefined {
108
return trace.getActiveSpan();
119
}
12-
13-
/**
14-
* Get the root span for the given span.
15-
* The given span may be the root span itself.
16-
*/
17-
export function getRootSpan(span: Span): Span {
18-
let parent: Span = span;
19-
20-
while (getSpanParent(parent)) {
21-
parent = getSpanParent(parent) as Span;
22-
}
23-
24-
return parent;
25-
}

packages/opentelemetry/test/trace.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import {
1111
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1212
getClient,
1313
getCurrentScope,
14+
getRootSpan,
1415
withScope,
1516
} from '@sentry/core';
1617
import type { Event, Scope } from '@sentry/types';
1718

1819
import { startInactiveSpan, startSpan, startSpanManual } from '../src/trace';
1920
import type { AbstractSpan } from '../src/types';
20-
import { getActiveSpan, getRootSpan } from '../src/utils/getActiveSpan';
21+
import { getActiveSpan } from '../src/utils/getActiveSpan';
2122
import { getSpanKind } from '../src/utils/getSpanKind';
2223
import { getSpanMetadata } from '../src/utils/spanData';
2324
import { spanHasAttributes, spanHasName } from '../src/utils/spanTypes';

packages/opentelemetry/test/utils/getActiveSpan.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { trace } from '@opentelemetry/api';
22
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
3+
import { getRootSpan } from '@sentry/core';
34

4-
import { getActiveSpan, getRootSpan } from '../../src/utils/getActiveSpan';
5+
import { getActiveSpan } from '../../src/utils/getActiveSpan';
56
import { TestClient, getDefaultTestClientOptions } from '../helpers/TestClient';
67
import { setupOtel } from '../helpers/initOtel';
78
import { cleanupOtel } from '../helpers/mockSdkInit';

0 commit comments

Comments
 (0)