Skip to content

chore(nextjs): Merge master into nextjs performance feature branch #3536

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
May 12, 2021
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
18 changes: 4 additions & 14 deletions packages/tracing/src/browser/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getCurrentHub } from '@sentry/hub';
import { addInstrumentationHandler, isInstanceOf, isMatchingPattern } from '@sentry/utils';

import { Span } from '../span';
Expand Down Expand Up @@ -145,13 +144,7 @@ export function fetchCallback(
shouldCreateSpan: (url: string) => boolean,
spans: Record<string, Span>,
): void {
const currentClientOptions = getCurrentHub()
.getClient()
?.getOptions();
if (
!(currentClientOptions && hasTracingEnabled(currentClientOptions)) ||
!(handlerData.fetchData && shouldCreateSpan(handlerData.fetchData.url))
) {
if (!hasTracingEnabled() || !(handlerData.fetchData && shouldCreateSpan(handlerData.fetchData.url))) {
return;
}

Expand Down Expand Up @@ -219,13 +212,10 @@ export function xhrCallback(
shouldCreateSpan: (url: string) => boolean,
spans: Record<string, Span>,
): void {
const currentClientOptions = getCurrentHub()
.getClient()
?.getOptions();
if (
!(currentClientOptions && hasTracingEnabled(currentClientOptions)) ||
!(handlerData.xhr && handlerData.xhr.__sentry_xhr__ && shouldCreateSpan(handlerData.xhr.__sentry_xhr__.url)) ||
handlerData.xhr.__sentry_own_request__
!hasTracingEnabled() ||
handlerData.xhr?.__sentry_own_request__ ||
!(handlerData.xhr?.__sentry_xhr__ && shouldCreateSpan(handlerData.xhr.__sentry_xhr__.url))
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tracing/src/hubextensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function traceHeaders(this: Hub): { [key: string]: string } {
*/
function sample<T extends Transaction>(transaction: T, options: Options, samplingContext: SamplingContext): T {
// nothing to do if tracing is not enabled
if (!hasTracingEnabled(options)) {
if (!hasTracingEnabled()) {
transaction.sampled = false;
return transaction;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/tracing/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ export const TRACEPARENT_REGEXP = new RegExp(
*
* Tracing is enabled when at least one of `tracesSampleRate` and `tracesSampler` is defined in the SDK config.
*/
export function hasTracingEnabled(options: Options): boolean {
export function hasTracingEnabled(
options: Options | undefined = getCurrentHub()
.getClient()
?.getOptions(),
): boolean {
if (!options) {
return false;
}
return 'tracesSampleRate' in options || 'tracesSampler' in options;
}

Expand Down
41 changes: 37 additions & 4 deletions packages/tracing/test/hub.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { BrowserClient } from '@sentry/browser';
import { Hub } from '@sentry/hub';
import * as hubModule from '@sentry/hub';
import { Hub, makeMain } from '@sentry/hub';
import { TransactionSamplingMethod } from '@sentry/types';
import * as utilsModule from '@sentry/utils'; // for mocking
import { logger } from '@sentry/utils';
Expand Down Expand Up @@ -32,6 +31,7 @@ describe('Hub', () => {
describe('getTransaction()', () => {
it('should find a transaction which has been set on the scope if sampled = true', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });
transaction.sampled = true;

Expand All @@ -44,6 +44,7 @@ describe('Hub', () => {

it('should find a transaction which has been set on the scope if sampled = false', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark', sampled: false });

hub.configureScope(scope => {
Expand All @@ -55,6 +56,7 @@ describe('Hub', () => {

it("should not find an open transaction if it's not on the scope", () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(hub.getScope()?.getTransaction()).toBeUndefined();
Expand All @@ -66,6 +68,8 @@ describe('Hub', () => {
it('should add transaction context data to default sample context', () => {
const tracesSampler = jest.fn();
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);

const transactionContext = {
name: 'dogpark',
parentSpanId: '12312012',
Expand All @@ -80,6 +84,7 @@ describe('Hub', () => {
it("should add parent's sampling decision to default sample context", () => {
const tracesSampler = jest.fn();
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
const parentSamplingDecsion = false;

hub.startTransaction({
Expand All @@ -98,20 +103,23 @@ describe('Hub', () => {
it('should set sampled = false when tracing is disabled', () => {
// neither tracesSampleRate nor tracesSampler is defined -> tracing disabled
const hub = new Hub(new BrowserClient({}));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });

expect(transaction.sampled).toBe(false);
});

it('should set sampled = false if tracesSampleRate is 0', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });

expect(transaction.sampled).toBe(false);
});

it('should set sampled = true if tracesSampleRate is 1', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });

expect(transaction.sampled).toBe(true);
Expand All @@ -120,6 +128,7 @@ describe('Hub', () => {
it("should call tracesSampler if it's defined", () => {
const tracesSampler = jest.fn();
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(tracesSampler).toHaveBeenCalled();
Expand All @@ -128,6 +137,7 @@ describe('Hub', () => {
it('should set sampled = false if tracesSampler returns 0', () => {
const tracesSampler = jest.fn().mockReturnValue(0);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });

expect(tracesSampler).toHaveBeenCalled();
Expand All @@ -137,6 +147,7 @@ describe('Hub', () => {
it('should set sampled = true if tracesSampler returns 1', () => {
const tracesSampler = jest.fn().mockReturnValue(1);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });

expect(tracesSampler).toHaveBeenCalled();
Expand All @@ -147,6 +158,7 @@ describe('Hub', () => {
// so that the decision otherwise would be false
const tracesSampler = jest.fn().mockReturnValue(0);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark', sampled: true });

expect(transaction.sampled).toBe(true);
Expand All @@ -156,6 +168,7 @@ describe('Hub', () => {
// so that the decision otherwise would be true
const tracesSampler = jest.fn().mockReturnValue(1);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark', sampled: false });

expect(transaction.sampled).toBe(false);
Expand All @@ -165,6 +178,7 @@ describe('Hub', () => {
// make the two options do opposite things to prove precedence
const tracesSampler = jest.fn().mockReturnValue(true);
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0, tracesSampler }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });

expect(tracesSampler).toHaveBeenCalled();
Expand All @@ -174,6 +188,7 @@ describe('Hub', () => {
it('should tolerate tracesSampler returning a boolean', () => {
const tracesSampler = jest.fn().mockReturnValue(true);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });

expect(tracesSampler).toHaveBeenCalled();
Expand All @@ -183,6 +198,7 @@ describe('Hub', () => {
it('should record sampling method when sampling decision is explicitly set', () => {
const tracesSampler = jest.fn().mockReturnValue(0.1121);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark', sampled: true });

expect(Transaction.prototype.setMetadata).toHaveBeenCalledWith({
Expand All @@ -193,6 +209,7 @@ describe('Hub', () => {
it('should record sampling method and rate when sampling decision comes from tracesSampler', () => {
const tracesSampler = jest.fn().mockReturnValue(0.1121);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(Transaction.prototype.setMetadata).toHaveBeenCalledWith({
Expand All @@ -202,6 +219,7 @@ describe('Hub', () => {

it('should record sampling method when sampling decision is inherited', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0.1121 }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark', parentSampled: true });

expect(Transaction.prototype.setMetadata).toHaveBeenCalledWith({
Expand All @@ -211,6 +229,7 @@ describe('Hub', () => {

it('should record sampling method and rate when sampling decision comes from traceSampleRate', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0.1121 }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(Transaction.prototype.setMetadata).toHaveBeenCalledWith({
Expand All @@ -222,13 +241,15 @@ describe('Hub', () => {
describe('isValidSampleRate()', () => {
it("should reject tracesSampleRates which aren't numbers or booleans", () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 'dogs!' as any }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Sample rate must be a boolean or a number'));
});

it('should reject tracesSampleRates which are NaN', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 'dogs!' as any }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Sample rate must be a boolean or a number'));
Expand All @@ -237,6 +258,7 @@ describe('Hub', () => {
// the rate might be a boolean, but for our purposes, false is equivalent to 0 and true is equivalent to 1
it('should reject tracesSampleRates less than 0', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: -26 }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Sample rate must be between 0 and 1'));
Expand All @@ -245,6 +267,7 @@ describe('Hub', () => {
// the rate might be a boolean, but for our purposes, false is equivalent to 0 and true is equivalent to 1
it('should reject tracesSampleRates greater than 1', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 26 }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Sample rate must be between 0 and 1'));
Expand All @@ -253,6 +276,7 @@ describe('Hub', () => {
it("should reject tracesSampler return values which aren't numbers or booleans", () => {
const tracesSampler = jest.fn().mockReturnValue('dogs!');
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Sample rate must be a boolean or a number'));
Expand All @@ -261,6 +285,7 @@ describe('Hub', () => {
it('should reject tracesSampler return values which are NaN', () => {
const tracesSampler = jest.fn().mockReturnValue(NaN);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Sample rate must be a boolean or a number'));
Expand All @@ -270,6 +295,7 @@ describe('Hub', () => {
it('should reject tracesSampler return values less than 0', () => {
const tracesSampler = jest.fn().mockReturnValue(-12);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Sample rate must be between 0 and 1'));
Expand All @@ -279,6 +305,7 @@ describe('Hub', () => {
it('should reject tracesSampler return values greater than 1', () => {
const tracesSampler = jest.fn().mockReturnValue(31);
const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);
hub.startTransaction({ name: 'dogpark' });

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Sample rate must be between 0 and 1'));
Expand All @@ -290,6 +317,7 @@ describe('Hub', () => {
jest.spyOn(client, 'captureEvent');

const hub = new Hub(client);
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });

jest.spyOn(transaction, 'finish');
Expand All @@ -303,6 +331,7 @@ describe('Hub', () => {
describe('sampling inheritance', () => {
it('should propagate sampling decision to child spans', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: Math.random() }));
makeMain(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });
const child = transaction.startChild({ op: 'ball.chase' });

Expand All @@ -320,7 +349,7 @@ describe('Hub', () => {
integrations: [new BrowserTracing()],
}),
);
jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub);
makeMain(hub);

const transaction = hub.startTransaction({ name: 'dogpark' });
hub.configureScope(scope => {
Expand Down Expand Up @@ -362,7 +391,7 @@ describe('Hub', () => {
integrations: [new BrowserTracing()],
}),
);
jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub);
makeMain(hub);

const transaction = hub.startTransaction({ name: 'dogpark', sampled: false });
hub.configureScope(scope => {
Expand Down Expand Up @@ -407,6 +436,7 @@ describe('Hub', () => {
// tracesSampleRate
mathRandom.mockReturnValueOnce(1);
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0.5 }));
makeMain(hub);
const parentSamplingDecsion = true;

const transaction = hub.startTransaction({
Expand All @@ -422,6 +452,7 @@ describe('Hub', () => {
// tracesSampleRate = 1 means every transaction should end up with sampled = true, so make parent's decision the
// opposite to prove that inheritance takes precedence over tracesSampleRate
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
makeMain(hub);
const parentSamplingDecsion = false;

const transaction = hub.startTransaction({
Expand All @@ -440,6 +471,7 @@ describe('Hub', () => {
const parentSamplingDecsion = false;

const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);

const transaction = hub.startTransaction({
name: 'dogpark',
Expand All @@ -457,6 +489,7 @@ describe('Hub', () => {
const parentSamplingDecsion = true;

const hub = new Hub(new BrowserClient({ tracesSampler }));
makeMain(hub);

const transaction = hub.startTransaction({
name: 'dogpark',
Expand Down
3 changes: 2 additions & 1 deletion packages/tracing/test/span.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BrowserClient } from '@sentry/browser';
import { Hub, Scope } from '@sentry/hub';
import { Hub, makeMain, Scope } from '@sentry/hub';

import { Span, SpanStatus, Transaction } from '../src';
import { TRACEPARENT_REGEXP } from '../src/utils';
Expand All @@ -10,6 +10,7 @@ describe('Span', () => {
beforeEach(() => {
const myScope = new Scope();
hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }), myScope);
makeMain(hub);
});

describe('new Span', () => {
Expand Down