Skip to content

feat(opentelemetry): Use core getRootSpan functionality #11004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export {
getStatusMessage,
getRootSpan,
getActiveSpan,
addChildSpanToSpan,
} from './utils/spanUtils';
export { applySdkMetadata } from './utils/sdkMetadata';
export { DEFAULT_ENVIRONMENT } from './constants';
Expand Down
3 changes: 1 addition & 2 deletions packages/node-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export { cron } from './cron';

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

export { getRootSpan } from '@sentry/opentelemetry';

export {
addRequestDataToEvent,
DEFAULT_USER_INCLUDES,
Expand Down Expand Up @@ -106,6 +104,7 @@ export {
startInactiveSpan,
getActiveSpan,
withActiveSpan,
getRootSpan,
} from '@sentry/core';

export type {
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export {

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

export { getActiveSpan, getRootSpan } from './utils/getActiveSpan';
export { getActiveSpan } from './utils/getActiveSpan';
export { startSpan, startSpanManual, startInactiveSpan, withActiveSpan } from './trace';

// eslint-disable-next-line deprecation/deprecation
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry/src/setupEventContextTrace.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getRootSpan } from '@sentry/core';
import type { Client } from '@sentry/types';
import { dropUndefinedKeys } from '@sentry/utils';

import { getActiveSpan, getRootSpan } from './utils/getActiveSpan';
import { getActiveSpan } from './utils/getActiveSpan';
import { spanHasName, spanHasParentId } from './utils/spanTypes';

/** Ensure the `trace` context is set on all events. */
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry/src/spanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Context } from '@opentelemetry/api';
import { ROOT_CONTEXT, trace } from '@opentelemetry/api';
import type { Span, SpanProcessor as SpanProcessorInterface } from '@opentelemetry/sdk-trace-base';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { getClient, getDefaultCurrentScope, getDefaultIsolationScope } from '@sentry/core';
import { addChildSpanToSpan, getClient, getDefaultCurrentScope, getDefaultIsolationScope } from '@sentry/core';
import { logger } from '@sentry/utils';

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

// The root context does not have scopes stored, so we check for this specifically
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
getClient,
getCurrentScope,
getRootSpan,
handleCallbackErrors,
} from '@sentry/core';
import type { Client, Scope } from '@sentry/types';
Expand All @@ -19,7 +20,6 @@ import { SENTRY_TRACE_STATE_DSC } from './constants';
import type { OpenTelemetryClient, OpenTelemetrySpanContext } from './types';
import { getContextFromScope } from './utils/contextData';
import { getDynamicSamplingContextFromSpan } from './utils/dynamicSamplingContext';
import { getRootSpan } from './utils/getActiveSpan';
import { setSpanMetadata } from './utils/spanData';

/**
Expand Down
16 changes: 0 additions & 16 deletions packages/opentelemetry/src/utils/getActiveSpan.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import type { Span } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';

import { getSpanParent } from './spanData';

/**
* Returns the currently active span.
*/
export function getActiveSpan(): Span | undefined {
return trace.getActiveSpan();
}

/**
* Get the root span for the given span.
* The given span may be the root span itself.
*/
export function getRootSpan(span: Span): Span {
let parent: Span = span;

while (getSpanParent(parent)) {
parent = getSpanParent(parent) as Span;
}

return parent;
}
3 changes: 2 additions & 1 deletion packages/opentelemetry/test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
getClient,
getCurrentScope,
getRootSpan,
withScope,
} from '@sentry/core';
import type { Event, Scope } from '@sentry/types';

import { startInactiveSpan, startSpan, startSpanManual } from '../src/trace';
import type { AbstractSpan } from '../src/types';
import { getActiveSpan, getRootSpan } from '../src/utils/getActiveSpan';
import { getActiveSpan } from '../src/utils/getActiveSpan';
import { getSpanKind } from '../src/utils/getSpanKind';
import { getSpanMetadata } from '../src/utils/spanData';
import { spanHasAttributes, spanHasName } from '../src/utils/spanTypes';
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry/test/utils/getActiveSpan.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { trace } from '@opentelemetry/api';
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { getRootSpan } from '@sentry/core';

import { getActiveSpan, getRootSpan } from '../../src/utils/getActiveSpan';
import { getActiveSpan } from '../../src/utils/getActiveSpan';
import { TestClient, getDefaultTestClientOptions } from '../helpers/TestClient';
import { setupOtel } from '../helpers/initOtel';
import { cleanupOtel } from '../helpers/mockSdkInit';
Expand Down