Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Jun 21, 2024
2 parents 49b0ce0 + 250b6d3 commit c68c653
Show file tree
Hide file tree
Showing 59 changed files with 317 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
window.sentryOnLoad = function () {
Sentry.init({});

window.__sentryLoaded = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Sentry.forceLoad();

Sentry.captureException('Test exception');
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
window.Sentry = {_customThingOnSentry: 'customThingOnSentry' };
</script>
</head>
<body></body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../utils/fixtures';
import { envelopeRequestParser, waitForErrorRequestOnUrl } from '../../../../utils/helpers';

sentryTest('keeps data on window.Sentry intact', async ({ getLocalTestUrl, page }) => {
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestUrl({ testDir: __dirname });
const req = await waitForErrorRequestOnUrl(page, url);

const eventData = envelopeRequestParser(req);

expect(eventData.message).toBe('Test exception');

const customThingy = await page.evaluate('window.Sentry._customThingOnSentry');
expect(customThingy).toBe('customThingOnSentry');
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import * as assert from 'assert/strict';

const packageJson = require('./package.json');
Expand All @@ -20,4 +21,11 @@ assert.match(buildStdout, /(λ|ƒ) \/server-component/);
assert.match(buildStdout, /(λ|ƒ) \/server-component\/parameter\/\[\.\.\.parameters\]/);
assert.match(buildStdout, /(λ|ƒ) \/server-component\/parameter\/\[parameter\]/);

// Read the contents of the directory
const files = fs.readdirSync(path.join(process.cwd(), '.next', 'server'));
const mapFiles = files.filter(file => path.extname(file) === '.map');
if (mapFiles.length > 0) {
throw new Error('.map files found even though `sourcemaps.deleteSourcemapsAfterUpload` option is set!');
}

export {};
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ const nextConfig = {
};

module.exports = withSentryConfig(nextConfig, {
silent: true,
debug: true,
sourcemaps: {
deleteSourcemapsAfterUpload: true,
},
});
3 changes: 3 additions & 0 deletions dev-packages/rollup-utils/bundleHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export function makeBaseBundleConfig(options) {
output: {
format: 'iife',
name: 'Sentry',
intro: () => {
return 'exports = window.Sentry || {};';
},
},
context: 'window',
plugins: [rrwebBuildPlugin, markAsBrowserBuildPlugin],
Expand Down
1 change: 1 addition & 0 deletions docs/commit-issue-pr-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and committed as such onto `develop`.
- Make sure to rebase the branch on `develop` before squashing it
- Make sure to update the commit message of the squashed branch to follow the commit guidelines - including the PR
number
- If you are a Sentry employee, assign yourself to the PR

Please note that we cannot _enforce_ Squash Merge due to the usage of Gitflow (see below). Github remembers the last
used merge method, so you'll need to make sure to double check that you are using "Squash and Merge" correctly.
Expand Down
6 changes: 3 additions & 3 deletions packages/angular/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
functionToStringIntegration,
inboundFiltersIntegration,
} from '@sentry/core';
import type { Integration } from '@sentry/types';
import type { Client, Integration } from '@sentry/types';
import { logger } from '@sentry/utils';

import { IS_DEBUG_BUILD } from './flags';
Expand Down Expand Up @@ -44,7 +44,7 @@ export function getDefaultIntegrations(): Integration[] {
/**
* Inits the Angular SDK
*/
export function init(options: BrowserOptions): void {
export function init(options: BrowserOptions): Client | undefined {
const opts = {
defaultIntegrations: getDefaultIntegrations(),
...options,
Expand All @@ -53,7 +53,7 @@ export function init(options: BrowserOptions): void {
applySdkMetadata(opts, 'angular');

checkAndSetAngularVersion();
browserInit(opts);
return browserInit(opts);
}

function checkAndSetAngularVersion(): void {
Expand Down
4 changes: 4 additions & 0 deletions packages/angular/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ describe('init', () => {

expect(angularDefaultIntegrations).toEqual(browserDefaultIntegrationsWithoutBrowserApiErrors);
});

it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@sentry/node": "8.11.0",
"@sentry/types": "8.11.0",
"@sentry/utils": "8.11.0",
"@sentry/vite-plugin": "^2.18.0"
"@sentry/vite-plugin": "^2.19.0"
},
"devDependencies": {
"astro": "^3.5.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/client/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
setTag,
} from '@sentry/browser';
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
import type { Integration } from '@sentry/types';
import type { Client, Integration } from '@sentry/types';

// Tree-shakable guard to remove all code related to tracing
declare const __SENTRY_TRACING__: boolean;
Expand All @@ -16,17 +16,19 @@ declare const __SENTRY_TRACING__: boolean;
*
* @param options Configuration options for the SDK.
*/
export function init(options: BrowserOptions): void {
export function init(options: BrowserOptions): Client | undefined {
const opts = {
defaultIntegrations: getDefaultIntegrations(options),
...options,
};

applySdkMetadata(opts, 'astro', ['astro', 'browser']);

initBrowserSdk(opts);
const client = initBrowserSdk(opts);

setTag('runtime', 'browser');

return client;
}

function getDefaultIntegrations(options: BrowserOptions): Integration[] | undefined {
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { applySdkMetadata } from '@sentry/core';
import type { NodeOptions } from '@sentry/node';
import type { NodeClient, NodeOptions } from '@sentry/node';
import { init as initNodeSdk, setTag } from '@sentry/node';

/**
*
* @param options
*/
export function init(options: NodeOptions): void {
export function init(options: NodeOptions): NodeClient | undefined {
const opts = {
...options,
};

applySdkMetadata(opts, 'astro', ['astro', 'node']);

initNodeSdk(opts);
const client = initNodeSdk(opts);

setTag('runtime', 'node');

return client;
}
4 changes: 4 additions & 0 deletions packages/astro/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,9 @@ describe('Sentry client SDK', () => {
expect(getActiveSpan()).toBeUndefined();
});
});

it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
});
});
4 changes: 4 additions & 0 deletions packages/astro/test/server/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ describe('Sentry server SDK', () => {

expect(SentryNode.getIsolationScope().getScopeData().tags).toEqual({ runtime: 'node' });
});

it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
});
});
6 changes: 3 additions & 3 deletions packages/aws-serverless/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import { hostname } from 'os';
import { basename, resolve } from 'path';
import { types } from 'util';
import type { NodeOptions } from '@sentry/node';
import type { NodeClient, NodeOptions } from '@sentry/node';
import {
SDK_VERSION,
captureException,
Expand Down Expand Up @@ -74,7 +74,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
*
* @param options Configuration options for the SDK, @see {@link AWSLambdaOptions}.
*/
export function init(options: NodeOptions = {}): void {
export function init(options: NodeOptions = {}): NodeClient | undefined {
const opts = {
_metadata: {} as SdkMetadata,
defaultIntegrations: getDefaultIntegrations(options),
Expand All @@ -93,7 +93,7 @@ export function init(options: NodeOptions = {}): void {
version: SDK_VERSION,
};

initWithoutDefaultIntegrations(opts);
return initWithoutDefaultIntegrations(opts);
}

/** */
Expand Down
8 changes: 5 additions & 3 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
lastEventId,
startSession,
} from '@sentry/core';
import type { DsnLike, Integration, Options, UserFeedback } from '@sentry/types';
import type { Client, DsnLike, Integration, Options, UserFeedback } from '@sentry/types';
import { consoleSandbox, logger, stackParserFromStackParserOptions, supportsFetch } from '@sentry/utils';

import { addHistoryInstrumentationHandler } from '@sentry-internal/browser-utils';
Expand Down Expand Up @@ -139,7 +139,7 @@ declare const __SENTRY_RELEASE__: string | undefined;
*
* @see {@link BrowserOptions} for documentation on configuration options.
*/
export function init(browserOptions: BrowserOptions = {}): void {
export function init(browserOptions: BrowserOptions = {}): Client | undefined {
const options = applyDefaultOptions(browserOptions);

if (shouldShowBrowserExtensionError()) {
Expand All @@ -166,11 +166,13 @@ export function init(browserOptions: BrowserOptions = {}): void {
transport: options.transport || makeFetchTransport,
};

initAndBind(BrowserClient, clientOptions);
const client = initAndBind(BrowserClient, clientOptions);

if (options.autoSessionTracking) {
startSessionTracking();
}

return client;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/browser/test/unit/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,25 @@ describe('init', () => {

consoleErrorSpy.mockRestore();
});

it("doesn't return a client on initialization error", () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

Object.defineProperty(WINDOW, 'chrome', {
value: { runtime: { id: 'mock-extension-id' } },
writable: true,
});

const client = init(options);

expect(client).toBeUndefined();

consoleErrorSpy.mockRestore();
});
});

it('returns a client from init', () => {
const client = init();
expect(client).not.toBeUndefined();
});
});
5 changes: 3 additions & 2 deletions packages/bun/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
linkedErrorsIntegration,
requestDataIntegration,
} from '@sentry/core';
import type { NodeClient } from '@sentry/node';
import {
consoleIntegration,
contextLinesIntegration,
Expand Down Expand Up @@ -91,13 +92,13 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
*
* @see {@link BunOptions} for documentation on configuration options.
*/
export function init(options: BunOptions = {}): void {
export function init(options: BunOptions = {}): NodeClient | undefined {
options.clientClass = BunClient;
options.transport = options.transport || makeFetchTransport;

if (options.defaultIntegrations === undefined) {
options.defaultIntegrations = getDefaultIntegrations(options);
}

initNode(options);
return initNode(options);
}
4 changes: 4 additions & 0 deletions packages/bun/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ test("calling init shouldn't fail", () => {
});
expect(true).toBe(true);
});

test('shuold return client from init', () => {
expect(init({})).not.toBeUndefined();
});
4 changes: 2 additions & 2 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function captureException(
/**
* Captures a message event and sends it to Sentry.
*
* @param exception The exception to capture.
* @param message The message to send to Sentry.
* @param captureContext Define the level of the message or pass in additional data to attach to the message.
* @returns the id of the captured message.
*/
Expand All @@ -56,7 +56,7 @@ export function captureMessage(message: string, captureContext?: CaptureContext
/**
* Captures a manually created event and sends it to Sentry.
*
* @param exception The event to send to Sentry.
* @param event The event to send to Sentry.
* @param hint Optional additional data to attach to the Sentry event.
* @returns the id of the captured event.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type ClientClass<F extends Client, O extends ClientOptions> = new (option
export function initAndBind<F extends Client, O extends ClientOptions>(
clientClass: ClientClass<F, O>,
options: O,
): void {
): Client {
if (options.debug === true) {
if (DEBUG_BUILD) {
logger.enable();
Expand All @@ -35,6 +35,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
const client = new clientClass(options);
setCurrentClient(client);
client.init();
return client;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/core/test/lib/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ describe('SDK', () => {
'afterAllSetup2',
]);
});

test('returns client from init', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
const client = initAndBind(TestClient, options);
expect(client).not.toBeUndefined();
});
});
});

Expand Down
Loading

0 comments on commit c68c653

Please sign in to comment.