Skip to content

fix(tracing): Ensure sent spans are limited to 1000 #12252

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
May 28, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [],
tracesSampleRate: 1,
});

Sentry.startSpan({ name: 'parent' }, () => {
for (let i = 0; i < 5000; i++) {
Sentry.startInactiveSpan({ name: `child ${i}` }).end();
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect } from '@playwright/test';
import { sentryTest } from '../../../utils/fixtures';
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequestOnUrl } from '../../../utils/helpers';

sentryTest('it limits spans to 1000', async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });
await page.goto(url);

const req = await waitForTransactionRequestOnUrl(page, url);
const transaction = envelopeRequestParser(req);

expect(transaction.spans).toHaveLength(1000);
expect(transaction.spans).toContainEqual(expect.objectContaining({ description: 'child 0' }));
expect(transaction.spans).toContainEqual(expect.objectContaining({ description: 'child 999' }));
expect(transaction.spans).not.toContainEqual(expect.objectContaining({ description: 'child 1000' }));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
});

Sentry.startSpan({ name: 'parent' }, () => {
for (let i = 0; i < 5000; i++) {
Sentry.startInactiveSpan({ name: `child ${i}` }).end();
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { SpanJSON } from '@sentry/types';
import { createRunner } from '../../../utils/runner';

test('it limits spans to 1000', done => {
const expectedSpans: SpanJSON[] = [];
for (let i = 0; i < 1000; i++) {
expectedSpans.push(expect.objectContaining({ description: `child ${i}` }));
}

createRunner(__dirname, 'scenario.ts')
.ignore('session', 'sessions')
.expect({
transaction: {
transaction: 'parent',
spans: expectedSpans,
},
})
.start(done);
});
9 changes: 8 additions & 1 deletion packages/core/src/tracing/sentrySpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { logSpanEnd } from './logSpans';
import { timedEventsToMeasurements } from './measurement';
import { getCapturedScopesOnSpan } from './utils';

const MAX_SPAN_COUNT = 1000;

/**
* Span contains all data about a span
*/
Expand Down Expand Up @@ -310,7 +312,12 @@ export class SentrySpan implements Span {
contexts: {
trace: spanToTransactionTraceContext(this),
},
spans,
spans:
// spans.sort() mutates the array, but `spans` is already a copy so we can safely do this here
// we do not use spans anymore after this point
spans.length > MAX_SPAN_COUNT
? spans.sort((a, b) => a.start_timestamp - b.start_timestamp).slice(0, MAX_SPAN_COUNT)
: spans,
start_timestamp: this._startTime,
timestamp: this._endTime,
transaction: this._name,
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 @@ -208,7 +208,7 @@ export function addChildSpanToSpan(span: SpanWithPotentialChildren, childSpan: S

// We store a list of child spans on the parent span
// We need this for `getSpanDescendants()` to work
if (span[CHILD_SPANS_FIELD] && span[CHILD_SPANS_FIELD].size < 1000) {
if (span[CHILD_SPANS_FIELD]) {
span[CHILD_SPANS_FIELD].add(childSpan);
} else {
addNonEnumerableProperty(span, CHILD_SPANS_FIELD, new Set([childSpan]));
Expand Down
9 changes: 8 additions & 1 deletion packages/opentelemetry/src/spanExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { parseSpanDescription } from './utils/parseSpanDescription';

type SpanNodeCompleted = SpanNode & { span: ReadableSpan };

const MAX_SPAN_COUNT = 1000;

/**
* A Sentry-specific exporter that converts OpenTelemetry Spans to Sentry Spans & Transactions.
*/
Expand Down Expand Up @@ -140,7 +142,12 @@ function maybeSend(spans: ReadableSpan[]): ReadableSpan[] {
createAndFinishSpanForOtelSpan(child, spans, remaining);
});

transactionEvent.spans = spans;
// spans.sort() mutates the array, but we do not use this anymore after this point
// so we can safely mutate it here
transactionEvent.spans =
spans.length > MAX_SPAN_COUNT
? spans.sort((a, b) => a.start_timestamp - b.start_timestamp).slice(0, MAX_SPAN_COUNT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.sort() is not pure and will mutate the array. I would recommend shallow cloning the array first and making this very explicit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but here we already have a copy of an array that is not used anywhere else, so this is safe and "slightly more efficient". But I can leave a comment to make this clear!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment here (and also in the other place) to be explicit about this! :)

: spans;

const measurements = timedEventsToMeasurements(span.events);
if (measurements) {
Expand Down
Loading