Skip to content

Commit 316ebfe

Browse files
authored
Merge pull request #26834 from getsentry/awad/sdk-config-browser-runtime
feat(core): Report a browser runtime in SDK config
1 parent e7a1a76 commit 316ebfe

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎packages/core/src/integrations/reportSdkConfig.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Integration, IntegrationFn } from '../types/integration';
88
import { debug } from '../utils/debug-logger';
99
import { fnv1a32 } from '../utils/hash';
1010
import { isPlainObject } from '../utils/is';
11+
import { isBrowser } from '../utils/isBrowser';
1112
import { safeUnref } from '../utils/timer';
1213

1314
const _experimentalReportSdkConfigIntegration = (() => ({
@@ -106,7 +107,8 @@ function buildConfigAttributes(client: Client): Record<string, string> {
106107
} = client.getSdkMetadata() || {};
107108
const clientOptions = client.getOptions();
108109
const { environment } = clientOptions;
109-
const runtime = 'runtime' in clientOptions ? clientOptions.runtime : undefined;
110+
// Server clients set their runtime as an option; browser clients have none.
111+
const runtime = 'runtime' in clientOptions ? clientOptions.runtime : isBrowser() ? { name: 'browser' } : undefined;
110112

111113
// Only what the user passed, so SDK defaults never show up as config.
112114
const options: Record<string, unknown> = Object.fromEntries(

‎packages/core/test/lib/integrations/reportSdkConfig.test.ts‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Integration } from '../../../src/types/integration';
55
import { experimentalReportSdkConfigIntegration } from '../../../src/integrations/reportSdkConfig';
66
import type * as metricsApi from '../../../src/metrics/public-api';
77
import { count } from '../../../src/metrics/public-api';
8+
import { isBrowser } from '../../../src/utils/isBrowser';
89
import { captureOptionsSetByUser } from '../../../src/utils/sdkMetadata';
910
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';
1011

@@ -13,20 +14,26 @@ vi.mock('../../../src/metrics/public-api', async importOriginal => ({
1314
count: vi.fn(),
1415
}));
1516

17+
vi.mock('../../../src/utils/isBrowser', () => ({ isBrowser: vi.fn(() => false) }));
18+
1619
const tagIntegration = defineIntegration((_options: { tag?: string; filter?: RegExp } = {}) => ({ name: 'Tag' }));
1720

1821
function reportConfig(options: ReturnType<typeof getDefaultTestClientOptions>): Record<string, unknown> {
19-
let payload: Record<string, unknown> = {};
22+
return JSON.parse(reportAttributes(options)['sentry.sdk_config.payload'] as string);
23+
}
24+
25+
function reportAttributes(options: ReturnType<typeof getDefaultTestClientOptions>): Record<string, unknown> {
26+
let attributes: Record<string, unknown> = {};
2027
vi.mocked(count).mockImplementation((_name, _value, metricOptions) => {
21-
payload = JSON.parse(String(metricOptions?.attributes?.['sentry.sdk_config.payload']));
28+
attributes = metricOptions?.attributes ?? {};
2229
});
2330

2431
const client = new TestClient(options);
2532
setCurrentClient(client);
2633
client.init();
2734
vi.advanceTimersByTime(0);
2835

29-
return payload;
36+
return attributes;
3037
}
3138

3239
describe('experimentalReportSdkConfigIntegration', () => {
@@ -108,6 +115,44 @@ describe('experimentalReportSdkConfigIntegration', () => {
108115
});
109116
});
110117

118+
it('reports a browser runtime when the client sets none', () => {
119+
vi.mocked(isBrowser).mockReturnValueOnce(true);
120+
121+
const payload = reportConfig(
122+
getDefaultTestClientOptions({
123+
dsn: 'https://username@domain/123',
124+
integrations: [experimentalReportSdkConfigIntegration()],
125+
}),
126+
);
127+
128+
expect(payload.runtime).toEqual({ name: 'browser' });
129+
});
130+
131+
it('reports no runtime outside the browser when the client sets none', () => {
132+
const payload = reportConfig(
133+
getDefaultTestClientOptions({
134+
dsn: 'https://username@domain/123',
135+
integrations: [experimentalReportSdkConfigIntegration()],
136+
}),
137+
);
138+
139+
expect(payload).not.toHaveProperty('runtime');
140+
});
141+
142+
it('reports a different hash for a different runtime', () => {
143+
const options = { dsn: 'https://username@domain/123', integrations: [experimentalReportSdkConfigIntegration()] };
144+
// Server clients set the runtime; the test client's options type doesn't declare it.
145+
const nodeRuntime = { runtime: { name: 'node', version: 'v24.22.1' } };
146+
const otherNodeRuntime = { runtime: { name: 'node', version: 'v26.0.0' } };
147+
148+
const node = reportAttributes(getDefaultTestClientOptions({ ...options, ...nodeRuntime }));
149+
const otherNode = reportAttributes(getDefaultTestClientOptions({ ...options, ...otherNodeRuntime }));
150+
vi.mocked(isBrowser).mockReturnValueOnce(true);
151+
const browser = reportAttributes(getDefaultTestClientOptions(options));
152+
153+
expect(new Set([node, otherNode, browser].map(attributes => attributes['sentry.sdk_config.hash'])).size).toBe(3);
154+
});
155+
111156
it('reports the names of the default integrations the user passed', () => {
112157
const userOptions = captureOptionsSetByUser({
113158
defaultIntegrations: [experimentalReportSdkConfigIntegration()],

0 commit comments

Comments
 (0)