GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(core): Capture # of dropped spans through `beforeSendTransaction` by mydea · Pull Request #13022 · getsentry/sentry-javascript

feat(core): Capture # of dropped spans through beforeSendTransaction #13022

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.

Sign up for GitHub

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
mydea merged 4 commits into develop from fn/dropped-spans-beforeSendTransaction
Jul 29, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

feat(core): Capture # of dropped spans through beforeSendTransaction #13022

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
6b7d131
feat(core): Capture # of dropped spans through `beforeSendTransaction`
mydea Jul 23, 2024
495dbd7
small ref
mydea Jul 23, 2024
08c0ff7
add tests
mydea Jul 24, 2024
f6ddc1c
adjust size limits
mydea Jul 24, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

4 changes: 2 additions & 2 deletions 4 .size-limit.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ module.exports = [
path: 'packages/browser/build/npm/esm/index.js',
import: createImport('init', 'browserTracingIntegration', 'replayIntegration', 'replayCanvasIntegration'),
gzip: true,
limit: '76 KB',
limit: '78 KB',
},
{
name: '@sentry/browser (incl. Tracing, Replay, Feedback)',
path: 'packages/browser/build/npm/esm/index.js',
import: createImport('init', 'browserTracingIntegration', 'replayIntegration', 'feedbackIntegration'),
gzip: true,
limit: '89 KB',
limit: '90 KB',
},
{
name: '@sentry/browser (incl. Tracing, Replay, Feedback, metrics)',
Expand Down
38 changes: 30 additions & 8 deletions 38 packages/core/src/baseclient.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -371,20 +371,21 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
/**
* @inheritDoc
*/
public recordDroppedEvent(reason: EventDropReason, category: DataCategory, _event?: Event): void {
// Note: we use `event` in replay, where we overwrite this hook.

public recordDroppedEvent(reason: EventDropReason, category: DataCategory, eventOrCount?: Event | number): void {
if (this._options.sendClientReports) {
// TODO v9: We do not need the `event` passed as third argument anymore, and can possibly remove this overload
// If event is passed as third argument, we assume this is a count of 1
const count = typeof eventOrCount === 'number' ? eventOrCount : 1;

// We want to track each category (error, transaction, session, replay_event) separately
// but still keep the distinction between different type of outcomes.
// We could use nested maps, but it's much easier to read and type this way.
// A correct type for map-based implementation if we want to go that route
// would be `Partial<Record<SentryRequestType, Partial<Record<Outcome, number>>>>`
// With typescript 4.1 we could even use template literal types
const key = `${reason}:${category}`;
DEBUG_BUILD && logger.log(`Adding outcome: "${key}"`);

this._outcomes[key] = (this._outcomes[key] || 0) + 1;
DEBUG_BUILD && logger.log(`Recording outcome: "${key}"${count > 1 ? ` (${count} times)` : ''}`);
this._outcomes[key] = (this._outcomes[key] || 0) + count;
}
}

Expand Down Expand Up @@ -794,11 +795,11 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
.then(processedEvent => {
if (processedEvent === null) {
this.recordDroppedEvent('before_send', dataCategory, event);
if (isTransactionEvent(event)) {
if (isTransaction) {
const spans = event.spans || [];
// the transaction itself counts as one span, plus all the child spans that are added
const spanCount = 1 + spans.length;
this._outcomes['span'] = (this._outcomes['span'] || 0) + spanCount;
Comment thread

@mydea mydea Jul 23, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Noticed that this was actually buggy, because the key is built from reason + category 😬 now this is unified and should work consistently.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Lms24 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
this.recordDroppedEvent('before_send', 'span', spanCount);
}
throw new SentryError(`${beforeSendLabel} returned \`null\`, will not send event.`, 'log');
}
Expand All @@ -808,6 +809,18 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
this._updateSessionFromEvent(session, processedEvent);
}

if (isTransaction) {
const spanCountBefore =
(processedEvent.sdkProcessingMetadata && processedEvent.sdkProcessingMetadata.spanCountBeforeProcessing) ||
0;
const spanCountAfter = processedEvent.spans ? processedEvent.spans.length : 0;

const droppedSpanCount = spanCountBefore - spanCountAfter;
if (droppedSpanCount > 0) {
this.recordDroppedEvent('before_send', 'span', droppedSpanCount);
}
}

// None of the Sentry built event processor will update transaction name,
// so if the transaction name has been changed by an event processor, we know
// it has to come from custom event processor added by a user
Expand Down Expand Up @@ -973,6 +986,15 @@ function processBeforeSend(
}

if (beforeSendTransaction) {
if (event.spans) {
// We store the # of spans before processing in SDK metadata,
// so we can compare it afterwards to determine how many spans were dropped
const spanCountBefore = event.spans.length;
event.sdkProcessingMetadata = {
...event.sdkProcessingMetadata,
spanCountBeforeProcessing: spanCountBefore,
};
}
return beforeSendTransaction(event, hint);
}
}
Expand Down
28 changes: 26 additions & 2 deletions 28 packages/core/test/lib/base.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,30 @@ describe('BaseClient', () => {
expect(TestClient.instance!.event!.transaction).toBe('/adopt/dont/shop');
});

test('calls `beforeSendTransaction` and drops spans', () => {
const beforeSendTransaction = jest.fn(event => {
event.spans = [{ span_id: 'span5', trace_id: 'trace1', start_timestamp: 1234 }];
return event;
});
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSendTransaction });
const client = new TestClient(options);

client.captureEvent({
transaction: '/dogs/are/great',
type: 'transaction',
spans: [
{ span_id: 'span1', trace_id: 'trace1', start_timestamp: 1234 },
{ span_id: 'span2', trace_id: 'trace1', start_timestamp: 1234 },
{ span_id: 'span3', trace_id: 'trace1', start_timestamp: 1234 },
],
});

expect(beforeSendTransaction).toHaveBeenCalled();
expect(TestClient.instance!.event!.spans?.length).toBe(1);

expect(client['_outcomes']).toEqual({ 'before_send:span': 2 });
});

test('calls `beforeSendSpan` and uses the modified spans', () => {
expect.assertions(3);

Expand Down Expand Up @@ -1120,8 +1144,6 @@ describe('BaseClient', () => {
});

test('calls `beforeSendSpan` and discards the span', () => {
expect.assertions(2);

const beforeSendSpan = jest.fn(() => null);
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSendSpan });
const client = new TestClient(options);
Expand Down Expand Up @@ -1149,6 +1171,7 @@ describe('BaseClient', () => {
expect(beforeSendSpan).toHaveBeenCalledTimes(2);
const capturedEvent = TestClient.instance!.event!;
expect(capturedEvent.spans).toHaveLength(0);
expect(client['_outcomes']).toEqual({ 'before_send:span': 2 });
});

test('calls `beforeSend` and logs info about invalid return value', () => {
Expand Down Expand Up @@ -2017,6 +2040,7 @@ describe('BaseClient', () => {

describe('hook removal with `on`', () => {
it('should return a cleanup function that, when executed, unregisters a hook', async () => {
jest.useFakeTimers();
expect.assertions(8);

const client = new TestClient(
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.