Skip to content
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

fix(core): Don't return trace data in getTraceData and getTraceMetaTags if SDK is disabled #13760

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -5,7 +5,7 @@ describe('getTraceMetaTags', () => {
cleanupChildProcesses();
});

test('injects sentry tracing <meta> tags', async () => {
test('injects <meta> tags with trace from incoming headers', async () => {
const traceId = 'cd7ee7a6fe3ebe7ab9c3271559bc203c';
const parentSpanId = '100ff0980e7a4ead';

Expand All @@ -22,4 +22,53 @@ describe('getTraceMetaTags', () => {
expect(html).toMatch(/<meta name="sentry-trace" content="cd7ee7a6fe3ebe7ab9c3271559bc203c-[a-z0-9]{16}-1"\/>/);
expect(html).toContain('<meta name="baggage" content="sentry-environment=production"/>');
});

test('injects <meta> tags with new trace if no incoming headers', async () => {
const runner = createRunner(__dirname, 'server.js').start();

const response = await runner.makeRequest('get', '/test');

// @ts-ignore - response is defined, types just don't reflect it
const html = response?.response as unknown as string;

const traceId = html.match(/<meta name="sentry-trace" content="([a-z0-9]{32})-[a-z0-9]{16}-1"\/>/)?.[1];
expect(traceId).not.toBeUndefined();

expect(html).toContain('<meta name="baggage"');
expect(html).toContain(`sentry-trace_id=${traceId}`);
});

test('injects <meta> tags with negative sampling decision if tracesSampleRate is 0', async () => {
const runner = createRunner(__dirname, 'server-tracesSampleRate-zero.js').start();

const response = await runner.makeRequest('get', '/test');

// @ts-ignore - response is defined, types just don't reflect it
const html = response?.response as unknown as string;

const traceId = html.match(/<meta name="sentry-trace" content="([a-z0-9]{32})-[a-z0-9]{16}-0"\/>/)?.[1];
expect(traceId).not.toBeUndefined();

expect(html).toContain('<meta name="baggage"');
expect(html).toContain(`sentry-trace_id=${traceId}`);
expect(html).toContain('sentry-sampled=false');
});

test("doesn't inject sentry tracing <meta> tags if SDK is disabled", async () => {
const traceId = 'cd7ee7a6fe3ebe7ab9c3271559bc203c';
const parentSpanId = '100ff0980e7a4ead';

const runner = createRunner(__dirname, 'server-sdk-disabled.js').start();

const response = await runner.makeRequest('get', '/test', {
'sentry-trace': `${traceId}-${parentSpanId}-1`,
baggage: 'sentry-environment=production',
});

// @ts-ignore - response is defined, types just don't reflect it
const html = response?.response as unknown as string;

expect(html).not.toContain('"sentry-trace"');
expect(html).not.toContain('"baggage"');
});
});
5 changes: 5 additions & 0 deletions packages/core/src/utils/traceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { getAsyncContextStrategy } from '../asyncContext';
import { getMainCarrier } from '../carrier';
import { getClient, getCurrentScope } from '../currentScopes';
import { isEnabled } from '../exports';
import { getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan } from '../tracing';
import { getActiveSpan, getRootSpan, spanToTraceHeader } from './spanUtils';

Expand All @@ -23,6 +24,10 @@ import { getActiveSpan, getRootSpan, spanToTraceHeader } from './spanUtils';
* or meta tag name.
*/
export function getTraceData(): SerializedTraceData {
if (!isEnabled()) {
return {};
}

const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
if (acs.getTraceData) {
Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/lib/utils/traceData.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SentrySpan, getTraceData } from '../../../src/';
import * as SentryCoreCurrentScopes from '../../../src/currentScopes';
import * as SentryCoreExports from '../../../src/exports';
import * as SentryCoreTracing from '../../../src/tracing';
import * as SentryCoreSpanUtils from '../../../src/utils/spanUtils';

Expand All @@ -22,6 +23,14 @@ const mockedScope = {
} as any;

describe('getTraceData', () => {
beforeEach(() => {
jest.spyOn(SentryCoreExports, 'isEnabled').mockReturnValue(true);
});

afterEach(() => {
jest.clearAllMocks();
});

it('returns the tracing data from the span, if a span is available', () => {
{
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
Expand Down Expand Up @@ -139,6 +148,14 @@ describe('getTraceData', () => {

expect(traceData).toEqual({});
});

it('returns an empty object if the SDK is disabled', () => {
jest.spyOn(SentryCoreExports, 'isEnabled').mockReturnValueOnce(false);

const traceData = getTraceData();

expect(traceData).toEqual({});
});
});

describe('isValidBaggageString', () => {
Expand Down
Loading