Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix(sampleRate): profilesSampleRate and tracesSampleRate are multipli…
Browse files Browse the repository at this point in the history
…ed (#77)

* fix: make sure we multiply sampleRates

* test: add test for profilesSampleRate
  • Loading branch information
JonasBa authored Dec 6, 2022
1 parent 8f8efb9 commit 65f63aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/hubextensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { importCppBindingsModule } from './cpu_profiler';

const profiler = importCppBindingsModule();

function makeTransactionMock(): Transaction {
function makeTransactionMock(options = {}): Transaction {
return {
metadata: {},
tags: {},
sampled: true,
startChild: () => ({ finish: () => void 0 }),
finish() {
return;
Expand All @@ -18,7 +19,8 @@ function makeTransactionMock(): Transaction {
},
setMetadata(metadata: Partial<TransactionMetadata>) {
this.metadata = { ...metadata } as TransactionMetadata;
}
},
...options
} as Transaction;
}

Expand Down Expand Up @@ -101,4 +103,20 @@ describe('hubextensions', () => {
// @ts-expect-error profile is not part of SDK metadata
expect(transaction.metadata?.profile).toBeDefined();
});

it('does not start the profiler if transaction is sampled', () => {
const startProfilingSpy = jest.spyOn(profiler, 'startProfiling');
const stopProfilingSpy = jest.spyOn(profiler, 'stopProfiling');

const hub = makeHubMock({ profilesSampleRate: 1 });
const startTransaction = jest.fn().mockImplementation(() => makeTransactionMock({ sampled: false }));

const maybeStartTransactionWithProfiling = __PRIVATE__wrapStartTransactionWithProfiling(startTransaction);
const transaction = maybeStartTransactionWithProfiling.call(hub, { name: '' }, {});
transaction.finish();

expect(startTransaction).toHaveBeenCalledTimes(1);
expect(startProfilingSpy).not.toHaveBeenCalledTimes(1);
expect(stopProfilingSpy).not.toHaveBeenCalledTimes(1);
});
});
5 changes: 5 additions & 0 deletions src/hubextensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export function __PRIVATE__wrapStartTransactionWithProfiling(startTransaction: S
// calling the profiler methods. Note: we log the original name to the user to avoid confusion.
const uniqueTransactionName = `${transactionContext.name} ${uuid4()}`;

// profilesSampleRate is multiplied with tracesSampleRate to get the final sampling rate.
if (!transaction.sampled) {
return transaction;
}

if (profilesSampleRate === undefined) {
if (isDebugBuild()) {
logger.log(
Expand Down

0 comments on commit 65f63aa

Please sign in to comment.