Skip to content

feat(browser): Track if cdn or npm bundle #6976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import type {
Severity,
SeverityLevel,
} from '@sentry/types';
import { createClientReportEnvelope, dsnToString, logger, serializeEnvelope } from '@sentry/utils';
import { createClientReportEnvelope, dsnToString, getSDKSource, logger, serializeEnvelope } from '@sentry/utils';

import { eventFromException, eventFromMessage } from './eventbuilder';
import { WINDOW } from './helpers';
import type { Breadcrumbs } from './integrations';
import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs';
import type { BrowserTransportOptions } from './transports/types';

/**
* Configuration options for the Sentry Browser SDK.
* @see @sentry/types Options for more information.
Expand All @@ -41,12 +42,14 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
* @param options Configuration options for this SDK.
*/
public constructor(options: BrowserClientOptions) {
const sdkSource = WINDOW.SENTRY_SDK_SOURCE || getSDKSource();

options._metadata = options._metadata || {};
options._metadata.sdk = options._metadata.sdk || {
name: 'sentry.javascript.browser',
packages: [
{
name: 'npm:@sentry/browser',
name: `${sdkSource}:@sentry/browser`,
version: SDK_VERSION,
},
],
Expand Down
22 changes: 22 additions & 0 deletions packages/browser/test/unit/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getReportDialogEndpoint, SDK_VERSION } from '@sentry/core';
import * as utils from '@sentry/utils';

import type { Event } from '../../src';
import {
Expand Down Expand Up @@ -277,6 +278,27 @@ describe('SentryBrowser initialization', () => {
expect(sdkData?.version).toBe(SDK_VERSION);
});

it('uses SDK source from window for package name', () => {
global.SENTRY_SDK_SOURCE = 'loader';
init({ dsn });

const sdkData = (getCurrentHub().getClient() as any).getOptions()._metadata.sdk;

expect(sdkData?.packages[0].name).toBe('loader:@sentry/browser');
delete global.SENTRY_SDK_SOURCE;
});

it('uses SDK source from global for package name', () => {
const spy = jest.spyOn(utils, 'getSDKSource').mockReturnValue('cdn');
init({ dsn });

const sdkData = (getCurrentHub().getClient() as any).getOptions()._metadata.sdk;

expect(sdkData?.packages[0].name).toBe('cdn:@sentry/browser');
expect(utils.getSDKSource).toBeCalledTimes(1);
spy.mockRestore();
});

it('should set SDK data when instantiating a client directly', () => {
const options = getDefaultBrowserClientOptions({ dsn });
const client = new BrowserClient(options);
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined;

type SdkSource = 'npm' | 'cdn' | 'loader';
export type SdkSource = 'npm' | 'cdn' | 'loader';

/**
* Figures out if we're building a browser bundle.
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import type { Integration } from '@sentry/types';

import type { SdkSource } from './env';

/** Internal global with common properties and Sentry extensions */
export interface InternalGlobal {
navigator?: { userAgent?: string };
Expand All @@ -26,6 +28,7 @@ export interface InternalGlobal {
SENTRY_RELEASE?: {
id?: string;
};
SENTRY_SDK_SOURCE?: SdkSource;
__SENTRY__: {
globalEventProcessors: any;
hub: any;
Expand Down