Skip to content

ref(opentelemetry): Remove parent span map #11014

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 2 commits 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
5 changes: 1 addition & 4 deletions packages/opentelemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ export type { OpenTelemetryClient } from './types';
export { wrapClientClass } from './custom/client';

export { getSpanKind } from './utils/getSpanKind';
export {
getSpanParent,
getSpanScopes,
} from './utils/spanData';
export { getSpanScopes } from './utils/spanData';

export { getScopesFromContext } from './utils/contextData';

Expand Down
5 changes: 0 additions & 5 deletions packages/opentelemetry/src/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { isNaN, logger } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
import { getPropagationContextFromSpanContext } from './propagator';
import { InternalSentrySemanticAttributes } from './semanticAttributes';
import { setIsSetup } from './utils/setupCheck';

/**
Expand Down Expand Up @@ -69,10 +68,6 @@ export class SentrySampler implements Sampler {
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: Number(sampleRate),
};

if (typeof parentSampled === 'boolean') {
attributes[InternalSentrySemanticAttributes.PARENT_SAMPLED] = parentSampled;
}

// Since this is coming from the user (or from a function provided by the user), who knows what we might get. (The
// only valid values are booleans or numbers between 0 and 1.)
if (!isValidSampleRate(sampleRate)) {
Expand Down
9 changes: 2 additions & 7 deletions packages/opentelemetry/src/semanticAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* These are internal and are not supposed to be used/depended on by external parties.
* No guarantees apply to these attributes, and the may change/disappear at any time.
*/
export const InternalSentrySemanticAttributes = {
PARENT_SAMPLED: 'sentry.parentSampled',
} as const;
/** If this attribute is true, it means that the parent is a remote span. */
export const SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE = 'sentry.parentIsRemote';
4 changes: 2 additions & 2 deletions packages/opentelemetry/src/spanExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { SpanJSON, SpanOrigin, TraceContext, TransactionEvent, TransactionS
import { dropUndefinedKeys, logger } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
import { InternalSentrySemanticAttributes } from './semanticAttributes';
import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from './semanticAttributes';
import { convertOtelTimeToSeconds } from './utils/convertOtelTimeToSeconds';
import { getDynamicSamplingContextFromSpan } from './utils/dynamicSamplingContext';
import { getRequestSpanData } from './utils/getRequestSpanData';
Expand Down Expand Up @@ -297,8 +297,8 @@ function removeSentryAttributes(data: Record<string, unknown>): Record<string, u
const cleanedData = { ...data };

/* eslint-disable @typescript-eslint/no-dynamic-delete */
delete cleanedData[InternalSentrySemanticAttributes.PARENT_SAMPLED];
delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE];
/* eslint-enable @typescript-eslint/no-dynamic-delete */

return cleanedData;
Expand Down
9 changes: 7 additions & 2 deletions packages/opentelemetry/src/spanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { addChildSpanToSpan, getClient, getDefaultCurrentScope, getDefaultIsolat
import { logger } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from './semanticAttributes';
import { SentrySpanExporter } from './spanExporter';
import { maybeCaptureExceptionForTimedEvent } from './utils/captureExceptionForTimedEvent';
import { getScopesFromContext } from './utils/contextData';
import { setIsSetup } from './utils/setupCheck';
import { setSpanParent, setSpanScopes } from './utils/spanData';
import { setSpanScopes } from './utils/spanData';

function onSpanStart(span: Span, parentContext: Context): void {
// This is a reliable way to get the parent span - because this is exactly how the parent is identified in the OTEL SDK
Expand All @@ -20,10 +21,14 @@ 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);
}

// We need this in the span exporter
if (parentSpan && parentSpan.spanContext().isRemote) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE, true);
}

// The root context does not have scopes stored, so we check for this specifically
// As fallback we attach the global scopes
if (parentContext === ROOT_CONTEXT) {
Expand Down
6 changes: 2 additions & 4 deletions packages/opentelemetry/src/utils/groupSpansWithParents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';

import { getSpanParent } from './spanData';
import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from '../semanticAttributes';

export interface SpanNode {
id: string;
Expand Down Expand Up @@ -28,8 +27,7 @@ export function groupSpansWithParents(spans: ReadableSpan[]): SpanNode[] {
}

function createOrUpdateSpanNodeAndRefs(nodeMap: SpanMap, span: ReadableSpan): void {
const parentSpan = getSpanParent(span);
const parentIsRemote = parentSpan ? !!parentSpan.spanContext().isRemote : false;
const parentIsRemote = span.attributes[SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE] === true;

const id = span.spanContext().spanId;

Expand Down
12 changes: 0 additions & 12 deletions packages/opentelemetry/src/utils/spanData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Span } from '@opentelemetry/api';
import type { Scope } from '@sentry/types';

import type { AbstractSpan } from '../types';
Expand All @@ -13,17 +12,6 @@ const SpanScopes = new WeakMap<
isolationScope: Scope;
}
>();
const SpanParent = new WeakMap<AbstractSpan, Span>();

/** Set the parent OTEL span on an OTEL span. */
export function setSpanParent(span: AbstractSpan, parentSpan: Span): void {
SpanParent.set(span, parentSpan);
}

/** Get the parent OTEL span of an OTEL span. */
export function getSpanParent(span: AbstractSpan): Span | undefined {
return SpanParent.get(span);
}

/**
* Set the Sentry scope to be used for finishing a given OTEL span.
Expand Down