Skip to content

Commit 73d5389

Browse files
committed
Another test and fix applyLocalSampleRateToDsc
1 parent fbf6af1 commit 73d5389

File tree

5 files changed

+114
-3
lines changed

5 files changed

+114
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const errorBtn = document.getElementById('errorBtn');
2+
errorBtn.addEventListener('click', () => {
3+
throw new Error(`Sentry Test Error ${Math.random()}`);
4+
});
5+
6+
const fetchBtn = document.getElementById('fetchBtn');
7+
fetchBtn.addEventListener('click', async () => {
8+
await fetch('http://sentry-test-site.example');
9+
});
10+
11+
const xhrBtn = document.getElementById('xhrBtn');
12+
xhrBtn.addEventListener('click', () => {
13+
const xhr = new XMLHttpRequest();
14+
xhr.open('GET', 'http://sentry-test-site.example');
15+
xhr.send();
16+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<button id="errorBtn">Throw Error</button>
8+
<button id="fetchBtn">Fetch Request</button>
9+
<button id="xhrBtn">XHR Request</button>
10+
</body>
11+
</html>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../utils/fixtures';
3+
import type { EventAndTraceHeader } from '../../../../utils/helpers';
4+
import {
5+
eventAndTraceHeaderRequestParser,
6+
getFirstSentryEnvelopeRequest,
7+
shouldSkipTracingTest,
8+
} from '../../../../utils/helpers';
9+
10+
const META_TAG_TRACE_ID = '12345678901234567890123456789012';
11+
const META_TAG_PARENT_SPAN_ID = '1234567890123456';
12+
const META_TAG_BAGGAGE =
13+
'sentry-trace_id=12345678901234567890123456789012,sentry-sample_rate=0.2,sentry-sampled=true,sentry-transaction=my-transaction,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod,sentry-sample_rand=0.42';
14+
15+
sentryTest(
16+
'create a new trace for a navigation after the server timing headers',
17+
async ({ getLocalTestUrl, page, enableConsole }) => {
18+
if (shouldSkipTracingTest()) {
19+
sentryTest.skip();
20+
}
21+
22+
enableConsole();
23+
24+
const url = await getLocalTestUrl({
25+
testDir: __dirname,
26+
responseHeaders: {
27+
'Server-Timing': `sentry-trace;desc=${META_TAG_TRACE_ID}-${META_TAG_PARENT_SPAN_ID}, baggage;desc="${META_TAG_BAGGAGE}"`,
28+
},
29+
});
30+
31+
const [pageloadEvent, pageloadTraceHeader] = await getFirstSentryEnvelopeRequest<EventAndTraceHeader>(
32+
page,
33+
url,
34+
eventAndTraceHeaderRequestParser,
35+
);
36+
const [navigationEvent, navigationTraceHeader] = await getFirstSentryEnvelopeRequest<EventAndTraceHeader>(
37+
page,
38+
`${url}#foo`,
39+
eventAndTraceHeaderRequestParser,
40+
);
41+
42+
const pageloadTraceContext = pageloadEvent.contexts?.trace;
43+
const navigationTraceContext = navigationEvent.contexts?.trace;
44+
45+
expect(pageloadEvent.type).toEqual('transaction');
46+
expect(pageloadTraceContext).toMatchObject({
47+
op: 'pageload',
48+
trace_id: META_TAG_TRACE_ID,
49+
parent_span_id: META_TAG_PARENT_SPAN_ID,
50+
span_id: expect.stringMatching(/^[\da-f]{16}$/),
51+
});
52+
53+
expect(pageloadTraceHeader).toEqual({
54+
environment: 'prod',
55+
release: '1.0.0',
56+
sample_rate: '0.2',
57+
sampled: 'true',
58+
transaction: 'my-transaction',
59+
public_key: 'public',
60+
trace_id: META_TAG_TRACE_ID,
61+
sample_rand: '0.42',
62+
});
63+
64+
expect(navigationEvent.type).toEqual('transaction');
65+
expect(navigationTraceContext).toMatchObject({
66+
op: 'navigation',
67+
trace_id: expect.stringMatching(/^[\da-f]{32}$/),
68+
span_id: expect.stringMatching(/^[\da-f]{16}$/),
69+
});
70+
// navigation span is head of trace, so there's no parent span:
71+
expect(navigationTraceContext?.trace_id).not.toHaveProperty('parent_span_id');
72+
73+
expect(navigationTraceHeader).toEqual({
74+
environment: 'production',
75+
public_key: 'public',
76+
sample_rate: '1',
77+
sampled: 'true',
78+
trace_id: navigationTraceContext?.trace_id,
79+
sample_rand: expect.any(String),
80+
});
81+
82+
expect(pageloadTraceContext?.trace_id).not.toEqual(navigationTraceContext?.trace_id);
83+
expect(pageloadTraceHeader?.sample_rand).not.toEqual(navigationTraceHeader?.sample_rand);
84+
},
85+
);

dev-packages/browser-integration-tests/suites/tracing/trace-lifetime/tracing-without-performance-headers/template.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@
55
</head>
66
<body>
77
<button id="errorBtn">Throw Error</button>
8-
<button id="fetchBtn">Fetch Request</button>
9-
<button id="xhrBtn">XHR Request</button>
108
</body>
119
</html>

packages/core/src/tracing/dynamicSamplingContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<
9393
rootSpanAttributes[SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE];
9494

9595
function applyLocalSampleRateToDsc(dsc: Partial<DynamicSamplingContext>): Partial<DynamicSamplingContext> {
96-
if (typeof rootSpanSampleRate === 'number' || typeof rootSpanSampleRate === 'string') {
96+
// Only apply local sample rate if the DSC doesn't already have one
97+
if (!dsc.sample_rate && (typeof rootSpanSampleRate === 'number' || typeof rootSpanSampleRate === 'string')) {
9798
dsc.sample_rate = `${rootSpanSampleRate}`;
9899
}
99100
return dsc;

0 commit comments

Comments
 (0)