Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Nov 19, 2024
1 parent b3988f5 commit 63ab398
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 26 deletions.
170 changes: 170 additions & 0 deletions packages/core/test/lib/tracing/sentryHeaders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import type { Client } from '@sentry/types';
import { Scope, SentrySpan, getCurrentScope, getGlobalScope, getIsolationScope, setCurrentClient } from '../../../src';
import { freezeDscOnSpan } from '../../../src/tracing/dynamicSamplingContext';
import { getSentryHeaders } from '../../../src/tracing/sentryHeaders';
import type { TestClientOptions } from '../../mocks/client';
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';

const dsn = 'https://123@sentry.io/42';

const SCOPE_TRACE_ID = '12345678901234567890123456789012';
const SCOPE_SPAN_ID = '1234567890123456';

function setupClient(opts?: TestClientOptions): Client {
getCurrentScope().clear();
getIsolationScope().clear();
getGlobalScope().clear();

getCurrentScope().setPropagationContext({
traceId: SCOPE_TRACE_ID,
spanId: SCOPE_SPAN_ID,
});

const options = getDefaultTestClientOptions({
dsn,
...opts,
});
const client = new TestClient(options);
setCurrentClient(client);
client.init();

return client;
}

describe('getSentryHeaders', () => {
beforeEach(() => {});

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

it('works with a minimal client', () => {
const client = setupClient();

const { sentryTrace, baggage } = getSentryHeaders({ client });

expect(sentryTrace).toEqual(`${SCOPE_TRACE_ID}-${SCOPE_SPAN_ID}`);
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${SCOPE_TRACE_ID}`);
});

it('allows to pass a specific scope', () => {
const client = setupClient();

const traceId = '12345678901234567890123456789099';
const spanId = '1234567890123499';
const scope = new Scope();
scope.setPropagationContext({
traceId,
spanId,
});

const { sentryTrace, baggage } = getSentryHeaders({ client, scope });

expect(sentryTrace).toEqual(`${traceId}-${spanId}`);
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
});

it('works with a minimal unsampled span', () => {
const client = setupClient();

const traceId = '12345678901234567890123456789099';
const spanId = '1234567890123499';

const span = new SentrySpan({
traceId,
spanId,
sampled: false,
});

const { sentryTrace, baggage } = getSentryHeaders({ client, span });

expect(sentryTrace).toEqual(`${traceId}-${spanId}-0`);
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
});

it('works with a minimal sampled span', () => {
const client = setupClient();

const traceId = '12345678901234567890123456789099';
const spanId = '1234567890123499';

const span = new SentrySpan({
traceId,
spanId,
sampled: true,
});

const { sentryTrace, baggage } = getSentryHeaders({ client, span });

expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
});

it('works with a SentrySpan with frozen DSC', () => {
const client = setupClient();

const traceId = '12345678901234567890123456789099';
const spanId = '1234567890123499';

const span = new SentrySpan({
traceId,
spanId,
sampled: true,
});

freezeDscOnSpan(span, {
environment: 'test-dev',
public_key: '456',
trace_id: '12345678901234567890123456789088',
});

const { sentryTrace, baggage } = getSentryHeaders({ client, span });

expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
expect(baggage).toEqual(
'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088',
);
});

it('works with an OTEL span with frozen DSC in traceState', () => {
const client = setupClient();

const traceId = '12345678901234567890123456789099';
const spanId = '1234567890123499';

const span = new SentrySpan({
traceId,
spanId,
sampled: true,
});

span.spanContext = () => {
const traceState = {
set: () => traceState,
unset: () => traceState,
get: (key: string) => {
if (key === 'sentry.dsc') {
return 'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088';
}
return undefined;
},
serialize: () => '',
};

return {
traceId,
spanId,
sampled: true,
traceFlags: 1,
traceState,
};
};

const { sentryTrace, baggage } = getSentryHeaders({ client, span });

expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
expect(baggage).toEqual(
'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088',
);
});
});
56 changes: 30 additions & 26 deletions packages/core/test/lib/utils/traceData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ 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 SentryCoreTracingDsc from '../../../src/tracing/dynamicSamplingContext';
import { freezeDscOnSpan } from '../../../src/tracing/dynamicSamplingContext';
import * as SentryCoreSpanUtils from '../../../src/utils/spanUtils';

import { isValidBaggageString } from '../../../src/utils/traceData';

const TRACE_FLAG_SAMPLED = 1;

const mockedSpan = new SentrySpan({
traceId: '12345678901234567890123456789012',
spanId: '1234567890123456',
sampled: true,
});

const mockedClient = {} as any;

const mockedScope = {
getPropagationContext: () => ({
traceId: '123',
spanId: '456',
}),
} as any;

Expand All @@ -33,11 +30,16 @@ describe('getTraceData', () => {

it('returns the tracing data from the span, if a span is available', () => {
{
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
environment: 'production',
const mockedSpan = new SentrySpan({
traceId: '12345678901234567890123456789012',
spanId: '1234567890123456',
sampled: true,
});
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => mockedSpan);
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
freezeDscOnSpan(mockedSpan, { environment: 'production' });

jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => mockedSpan);
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => mockedClient);

const data = getTraceData();

Expand All @@ -49,8 +51,8 @@ describe('getTraceData', () => {
});

it('returns propagationContext DSC data if no span is available', () => {
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => undefined);
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => undefined);
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(
() =>
({
getPropagationContext: () => ({
Expand All @@ -65,7 +67,7 @@ describe('getTraceData', () => {
}),
}) as any,
);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => mockedClient);

const traceData = getTraceData();

Expand All @@ -76,13 +78,13 @@ describe('getTraceData', () => {
});

it('returns only the `sentry-trace` value if no DSC is available', () => {
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
jest.spyOn(SentryCoreTracingDsc, 'getDynamicSamplingContextFromSpan').mockReturnValue({
trace_id: '',
public_key: undefined,
});

// @ts-expect-error - we don't need to provide all the properties
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
isRecording: () => true,
spanContext: () => {
return {
Expand All @@ -93,8 +95,13 @@ describe('getTraceData', () => {
},
}));

jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => {
return {
getOptions: () => ({}),
getDsn: () => ({}),
} as any;
});

const traceData = getTraceData();

Expand All @@ -103,14 +110,14 @@ describe('getTraceData', () => {
});
});

it('returns only the `sentry-trace` tag if no DSC is available without a client', () => {
it('returns empty object without a client', () => {
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
trace_id: '',
public_key: undefined,
});

// @ts-expect-error - we don't need to provide all the properties
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
isRecording: () => true,
spanContext: () => {
return {
Expand All @@ -120,20 +127,17 @@ describe('getTraceData', () => {
};
},
}));
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => undefined);
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => undefined);

const traceData = getTraceData();

expect(traceData).toEqual({
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
});
expect('baggage' in traceData).toBe(false);
expect(traceData).toEqual({});
});

it('returns an empty object if the `sentry-trace` value is invalid', () => {
// @ts-expect-error - we don't need to provide all the properties
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
isRecording: () => true,
spanContext: () => {
return {
Expand Down

0 comments on commit 63ab398

Please sign in to comment.