Skip to content

Commit 9f1b607

Browse files
committed
fix tests, add tests
1 parent 75134c4 commit 9f1b607

File tree

4 files changed

+101
-16
lines changed

4 files changed

+101
-16
lines changed

packages/astro/src/server/meta.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { getDynamicSamplingContextFromClient, spanToTraceHeader } from '@sentry/core';
2-
import { getDynamicSamplingContextFromSpan } from '@sentry/core/src/tracing/dynamicSamplingContext';
1+
import {
2+
getDynamicSamplingContextFromClient,
3+
getDynamicSamplingContextFromSpan,
4+
spanToTraceHeader,
5+
} from '@sentry/core';
36
import type { Client, Scope, Span } from '@sentry/types';
47
import {
58
TRACEPARENT_REGEXP,

packages/astro/test/server/meta.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const mockedScope = {
2525
describe('getTracingMetaTags', () => {
2626
it('returns the tracing tags from the span, if it is provided', () => {
2727
{
28+
vi.spyOn(SentryCore, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
29+
environment: 'production',
30+
});
31+
2832
const tags = getTracingMetaTags(mockedSpan, mockedScope, mockedClient);
2933

3034
expect(tags).toEqual({

packages/core/src/tracing/dynamicSamplingContext.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Client, DynamicSamplingContext, Scope, Span } from '@sentry/types';
1+
import type { Client, DynamicSamplingContext, Scope, Span, Transaction } from '@sentry/types';
22
import { dropUndefinedKeys } from '@sentry/utils';
33

44
import { DEFAULT_ENVIRONMENT } from '../constants';
@@ -38,7 +38,7 @@ export function getDynamicSamplingContextFromClient(
3838
/**
3939
* A Span with a frozen dynamic sampling context.
4040
*/
41-
type TransactionWithV7FrozenDsc = Span & { _frozenDynamicSamplingContext?: DynamicSamplingContext };
41+
type TransactionWithV7FrozenDsc = Transaction & { _frozenDynamicSamplingContext?: DynamicSamplingContext };
4242

4343
/**
4444
* Creates a dynamic sampling context from a span (and client and scope)
@@ -48,29 +48,30 @@ type TransactionWithV7FrozenDsc = Span & { _frozenDynamicSamplingContext?: Dynam
4848
* @returns a dynamic sampling context
4949
*/
5050
export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<DynamicSamplingContext>> {
51-
// As long as we use `Transaction`s internally, this should be fine.
52-
// TODO: We need to replace this with a `getRootSpan(span)` function though
53-
const txn = span.transaction;
54-
55-
// TODO (v8): Remove v7FrozenDsc as a Transaction will no longer have _frozenDynamicSamplingContext
56-
// For now we need to avoid breaking users who directly created a txn with a DSC, where this field is still set.
57-
// @see Transaction class constructor
58-
const v7FrozenDsc = (txn as TransactionWithV7FrozenDsc)._frozenDynamicSamplingContext;
59-
if (v7FrozenDsc) {
60-
return v7FrozenDsc;
61-
}
62-
6351
const client = getClient();
6452
if (!client) {
6553
return {};
6654
}
6755

6856
// passing emit=false here to only emit later once the DSC is actually populated
6957
const dsc = getDynamicSamplingContextFromClient(span.traceId, client, getCurrentScope(), false);
58+
59+
const txn = span.transaction as TransactionWithV7FrozenDsc | undefined;
7060
if (!txn) {
7161
return dsc;
7262
}
7363

64+
// As long as we use `Transaction`s internally, this should be fine.
65+
// TODO: We need to replace this with a `getRootSpan(span)` function though
66+
67+
// TODO (v8): Remove v7FrozenDsc as a Transaction will no longer have _frozenDynamicSamplingContext
68+
// For now we need to avoid breaking users who directly created a txn with a DSC, where this field is still set.
69+
// @see Transaction class constructor
70+
const v7FrozenDsc = txn && txn._frozenDynamicSamplingContext;
71+
if (v7FrozenDsc) {
72+
return v7FrozenDsc;
73+
}
74+
7475
const maybeSampleRate = txn.metadata.sampleRate;
7576
if (maybeSampleRate !== undefined) {
7677
dsc.sample_rate = `${maybeSampleRate}`;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import type { TransactionSource } from '@sentry/types';
2+
import { Hub, makeMain } from '../../../src';
3+
import { Transaction, getDynamicSamplingContextFromSpan } from '../../../src/tracing';
4+
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
5+
6+
describe('getDynamicSamplingContextFromSpan', () => {
7+
beforeEach(() => {
8+
const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0, release: '1.0.1' });
9+
const client = new TestClient(options);
10+
const hub = new Hub(client);
11+
makeMain(hub);
12+
});
13+
14+
afterEach(() => {
15+
jest.resetAllMocks();
16+
});
17+
18+
test('returns the DSC provided during transaction creation', () => {
19+
const transaction = new Transaction({
20+
name: 'tx',
21+
metadata: { dynamicSamplingContext: { environment: 'myEnv' } },
22+
});
23+
24+
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(transaction);
25+
26+
expect(dynamicSamplingContext).toStrictEqual({ environment: 'myEnv' });
27+
});
28+
29+
test('returns a new DSC, if no DSC was provided during transaction creation', () => {
30+
const transaction = new Transaction({
31+
name: 'tx',
32+
metadata: {
33+
sampleRate: 0.56,
34+
},
35+
});
36+
37+
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(transaction);
38+
39+
expect(dynamicSamplingContext).toStrictEqual({
40+
release: '1.0.1',
41+
environment: 'production',
42+
sample_rate: '0.56',
43+
trace_id: expect.any(String),
44+
transaction: 'tx',
45+
});
46+
});
47+
48+
describe('Including transaction name in DSC', () => {
49+
test('is not included if transaction source is url', () => {
50+
const transaction = new Transaction({
51+
name: 'tx',
52+
metadata: {
53+
source: 'url',
54+
},
55+
});
56+
57+
const dsc = getDynamicSamplingContextFromSpan(transaction);
58+
expect(dsc.transaction).toBeUndefined();
59+
});
60+
61+
test.each([
62+
['is included if transaction source is parameterized route/url', 'route'],
63+
['is included if transaction source is a custom name', 'custom'],
64+
])('%s', (_: string, source) => {
65+
const transaction = new Transaction({
66+
name: 'tx',
67+
metadata: {
68+
...(source && { source: source as TransactionSource }),
69+
},
70+
});
71+
72+
const dsc = getDynamicSamplingContextFromSpan(transaction);
73+
74+
expect(dsc.transaction).toEqual('tx');
75+
});
76+
});
77+
});

0 commit comments

Comments
 (0)