Skip to content

Commit 79d0efc

Browse files
authored
move sdk info setting to client in node and browser, add tests (#3279)
1 parent d7798eb commit 79d0efc

File tree

6 files changed

+141
-28
lines changed

6 files changed

+141
-28
lines changed

packages/browser/src/client.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseClient, Scope } from '@sentry/core';
1+
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
22
import { Event, EventHint } from '@sentry/types';
33
import { getGlobalObject, logger } from '@sentry/utils';
44

@@ -19,6 +19,18 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
1919
* @param options Configuration options for this SDK.
2020
*/
2121
public constructor(options: BrowserOptions = {}) {
22+
options._metadata = options._metadata || {};
23+
options._metadata.sdk = options._metadata.sdk || {
24+
name: 'sentry.javascript.browser',
25+
packages: [
26+
{
27+
name: 'npm:@sentry/browser',
28+
version: SDK_VERSION,
29+
},
30+
],
31+
version: SDK_VERSION,
32+
};
33+
2234
super(BrowserBackend, options);
2335
}
2436

packages/browser/src/sdk.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations, SDK_VERSION } from '@sentry/core';
1+
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
22
import { addInstrumentationHandler, getGlobalObject, logger, SyncPromise } from '@sentry/utils';
33

44
import { BrowserOptions } from './backend';
@@ -88,18 +88,6 @@ export function init(options: BrowserOptions = {}): void {
8888
options.autoSessionTracking = true;
8989
}
9090

91-
options._metadata = options._metadata || {};
92-
options._metadata.sdk = {
93-
name: 'sentry.javascript.browser',
94-
packages: [
95-
{
96-
name: 'npm:@sentry/browser',
97-
version: SDK_VERSION,
98-
},
99-
],
100-
version: SDK_VERSION,
101-
};
102-
10391
initAndBind(BrowserClient, options);
10492

10593
if (options.autoSessionTracking) {

packages/browser/test/unit/index.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SDK_VERSION } from '@sentry/core';
12
import { expect } from 'chai';
23
import { SinonSpy, spy } from 'sinon';
34

@@ -177,11 +178,67 @@ describe('SentryBrowser initialization', () => {
177178
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).to.equal('foobar');
178179
delete global.SENTRY_RELEASE;
179180
});
181+
180182
it('should have initialization proceed as normal if window.SENTRY_RELEASE is not set', () => {
181183
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
182184
init({ dsn });
183185
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).to.be.undefined;
184186
});
187+
188+
describe('SDK metadata', () => {
189+
it('should set SDK data when Sentry.init() is called', () => {
190+
init({ dsn });
191+
192+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
193+
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
194+
195+
expect(sdkData.name).to.equal('sentry.javascript.browser');
196+
expect(sdkData.packages[0].name).to.equal('npm:@sentry/browser');
197+
expect(sdkData.packages[0].version).to.equal(SDK_VERSION);
198+
expect(sdkData.version).to.equal(SDK_VERSION);
199+
});
200+
201+
it('should set SDK data when instantiating a client directly', () => {
202+
const client = new BrowserClient({ dsn });
203+
204+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
205+
const sdkData = (client as any)._backend._transport._api.metadata?.sdk;
206+
207+
expect(sdkData.name).to.equal('sentry.javascript.browser');
208+
expect(sdkData.packages[0].name).to.equal('npm:@sentry/browser');
209+
expect(sdkData.packages[0].version).to.equal(SDK_VERSION);
210+
expect(sdkData.version).to.equal(SDK_VERSION);
211+
});
212+
213+
// wrapper packages (like @sentry/angular and @sentry/react) set their SDK data in their `init` methods, which are
214+
// called before the client is instantiated, and we don't want to clobber that data
215+
it("shouldn't overwrite SDK data that's already there", () => {
216+
init({
217+
dsn,
218+
// this would normally be set by the wrapper SDK in init()
219+
_metadata: {
220+
sdk: {
221+
name: 'sentry.javascript.angular',
222+
packages: [
223+
{
224+
name: 'npm:@sentry/angular',
225+
version: SDK_VERSION,
226+
},
227+
],
228+
version: SDK_VERSION,
229+
},
230+
},
231+
});
232+
233+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
234+
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
235+
236+
expect(sdkData.name).to.equal('sentry.javascript.angular');
237+
expect(sdkData.packages[0].name).to.equal('npm:@sentry/angular');
238+
expect(sdkData.packages[0].version).to.equal(SDK_VERSION);
239+
expect(sdkData.version).to.equal(SDK_VERSION);
240+
});
241+
});
185242
});
186243

187244
describe('wrap()', () => {

packages/node/src/client.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseClient, Scope } from '@sentry/core';
1+
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
22
import { Event, EventHint } from '@sentry/types';
33

44
import { NodeBackend, NodeOptions } from './backend';
@@ -15,6 +15,18 @@ export class NodeClient extends BaseClient<NodeBackend, NodeOptions> {
1515
* @param options Configuration options for this SDK.
1616
*/
1717
public constructor(options: NodeOptions) {
18+
options._metadata = options._metadata || {};
19+
options._metadata.sdk = options._metadata.sdk || {
20+
name: 'sentry.javascript.node',
21+
packages: [
22+
{
23+
name: 'npm:@sentry/node',
24+
version: SDK_VERSION,
25+
},
26+
],
27+
version: SDK_VERSION,
28+
};
29+
1830
super(NodeBackend, options);
1931
}
2032

packages/node/src/sdk.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations, SDK_VERSION } from '@sentry/core';
1+
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
22
import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
33
import { getGlobalObject } from '@sentry/utils';
44
import * as domain from 'domain';
@@ -108,18 +108,6 @@ export function init(options: NodeOptions = {}): void {
108108
options.environment = process.env.SENTRY_ENVIRONMENT;
109109
}
110110

111-
options._metadata = options._metadata || {};
112-
options._metadata.sdk = {
113-
name: 'sentry.javascript.node',
114-
packages: [
115-
{
116-
name: 'npm:@sentry/node',
117-
version: SDK_VERSION,
118-
},
119-
],
120-
version: SDK_VERSION,
121-
};
122-
123111
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
124112
if ((domain as any).active) {
125113
setHubOnCarrier(getMainCarrier(), getCurrentHub());

packages/node/test/index.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SDK_VERSION } from '@sentry/core';
12
import * as domain from 'domain';
23

34
import {
@@ -282,4 +283,59 @@ describe('SentryNode initialization', () => {
282283
init({ dsn });
283284
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined();
284285
});
286+
287+
describe('SDK metadata', () => {
288+
it('should set SDK data when Sentry.init() is called', () => {
289+
init({ dsn });
290+
291+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
292+
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
293+
294+
expect(sdkData.name).toEqual('sentry.javascript.node');
295+
expect(sdkData.packages[0].name).toEqual('npm:@sentry/node');
296+
expect(sdkData.packages[0].version).toEqual(SDK_VERSION);
297+
expect(sdkData.version).toEqual(SDK_VERSION);
298+
});
299+
300+
it('should set SDK data when instantiating a client directly', () => {
301+
const client = new NodeClient({ dsn });
302+
303+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
304+
const sdkData = (client as any)._backend._transport._api.metadata?.sdk;
305+
306+
expect(sdkData.name).toEqual('sentry.javascript.node');
307+
expect(sdkData.packages[0].name).toEqual('npm:@sentry/node');
308+
expect(sdkData.packages[0].version).toEqual(SDK_VERSION);
309+
expect(sdkData.version).toEqual(SDK_VERSION);
310+
});
311+
312+
// wrapper packages (like @sentry/serverless) set their SDK data in their `init` methods, which are
313+
// called before the client is instantiated, and we don't want to clobber that data
314+
it("shouldn't overwrite SDK data that's already there", () => {
315+
init({
316+
dsn,
317+
// this would normally be set by the wrapper SDK in init()
318+
_metadata: {
319+
sdk: {
320+
name: 'sentry.javascript.serverless',
321+
packages: [
322+
{
323+
name: 'npm:@sentry/serverless',
324+
version: SDK_VERSION,
325+
},
326+
],
327+
version: SDK_VERSION,
328+
},
329+
},
330+
});
331+
332+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
333+
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
334+
335+
expect(sdkData.name).toEqual('sentry.javascript.serverless');
336+
expect(sdkData.packages[0].name).toEqual('npm:@sentry/serverless');
337+
expect(sdkData.packages[0].version).toEqual(SDK_VERSION);
338+
expect(sdkData.version).toEqual(SDK_VERSION);
339+
});
340+
});
285341
});

0 commit comments

Comments
 (0)