Skip to content

Commit f3b2e7d

Browse files
authored
ref: Streamline SDK metadata handling (#10251)
There have been tries to do this before, but let's see how things stand today...
1 parent b916548 commit f3b2e7d

File tree

25 files changed

+65
-262
lines changed

25 files changed

+65
-262
lines changed

packages/angular-ivy/src/sdk.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { VERSION } from '@angular/core';
22
import type { BrowserOptions } from '@sentry/browser';
3-
import { getDefaultIntegrations } from '@sentry/browser';
4-
import { SDK_VERSION, init as browserInit, setContext } from '@sentry/browser';
5-
import type { SdkMetadata } from '@sentry/types';
3+
import { getDefaultIntegrations, init as browserInit, setContext } from '@sentry/browser';
4+
import { applySdkMetadata } from '@sentry/core';
65
import { logger } from '@sentry/utils';
76

87
import { IS_DEBUG_BUILD } from './flags';
@@ -12,7 +11,6 @@ import { IS_DEBUG_BUILD } from './flags';
1211
*/
1312
export function init(options: BrowserOptions): void {
1413
const opts = {
15-
_metadata: {} as SdkMetadata,
1614
// Filter out TryCatch integration as it interferes with our Angular `ErrorHandler`:
1715
// TryCatch would catch certain errors before they reach the `ErrorHandler` and thus provide a
1816
// lower fidelity error than what `SentryErrorHandler` (see errorhandler.ts) would provide.
@@ -25,16 +23,7 @@ export function init(options: BrowserOptions): void {
2523
...options,
2624
};
2725

28-
opts._metadata.sdk = opts._metadata.sdk || {
29-
name: 'sentry.javascript.angular-ivy',
30-
packages: [
31-
{
32-
name: 'npm:@sentry/angular-ivy',
33-
version: SDK_VERSION,
34-
},
35-
],
36-
version: SDK_VERSION,
37-
};
26+
applySdkMetadata(opts, 'angular-ivy');
3827

3928
checkAndSetAngularVersion();
4029
browserInit(opts);

packages/angular/src/sdk.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { VERSION } from '@angular/core';
22
import type { BrowserOptions } from '@sentry/browser';
3-
import { getDefaultIntegrations } from '@sentry/browser';
4-
import { SDK_VERSION, init as browserInit, setContext } from '@sentry/browser';
5-
import type { SdkMetadata } from '@sentry/types';
3+
import { getDefaultIntegrations, init as browserInit, setContext } from '@sentry/browser';
4+
import { applySdkMetadata } from '@sentry/core';
65
import { logger } from '@sentry/utils';
76

87
import { IS_DEBUG_BUILD } from './flags';
@@ -12,7 +11,6 @@ import { IS_DEBUG_BUILD } from './flags';
1211
*/
1312
export function init(options: BrowserOptions): void {
1413
const opts = {
15-
_metadata: {} as SdkMetadata,
1614
// Filter out TryCatch integration as it interferes with our Angular `ErrorHandler`:
1715
// TryCatch would catch certain errors before they reach the `ErrorHandler` and thus provide a
1816
// lower fidelity error than what `SentryErrorHandler` (see errorhandler.ts) would provide.
@@ -25,16 +23,7 @@ export function init(options: BrowserOptions): void {
2523
...options,
2624
};
2725

28-
opts._metadata.sdk = opts._metadata.sdk || {
29-
name: 'sentry.javascript.angular',
30-
packages: [
31-
{
32-
name: 'npm:@sentry/angular',
33-
version: SDK_VERSION,
34-
},
35-
],
36-
version: SDK_VERSION,
37-
};
26+
applySdkMetadata(opts, 'angular');
3827

3928
checkAndSetAngularVersion();
4029
browserInit(opts);

packages/astro/src/client/sdk.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import {
55
init as initBrowserSdk,
66
setTag,
77
} from '@sentry/browser';
8-
import { hasTracingEnabled } from '@sentry/core';
8+
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
99
import type { Integration } from '@sentry/types';
1010

11-
import { applySdkMetadata } from '../common/metadata';
12-
1311
// Treeshakable guard to remove all code related to tracing
1412
declare const __SENTRY_TRACING__: boolean;
1513

@@ -24,7 +22,7 @@ export function init(options: BrowserOptions): void {
2422
...options,
2523
};
2624

27-
applySdkMetadata(opts, ['astro', 'browser']);
25+
applySdkMetadata(opts, 'astro', ['astro', 'browser']);
2826

2927
initBrowserSdk(opts);
3028

packages/astro/src/server/sdk.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
import { applySdkMetadata } from '@sentry/core';
12
import type { NodeOptions } from '@sentry/node';
23
import { init as initNodeSdk, setTag } from '@sentry/node';
34

4-
import { applySdkMetadata } from '../common/metadata';
5-
65
/**
76
*
87
* @param options
98
*/
109
export function init(options: NodeOptions): void {
11-
applySdkMetadata(options, ['astro', 'node']);
10+
applySdkMetadata(options, 'astro', ['astro', 'node']);
1211

1312
initNodeSdk(options);
1413

packages/browser/src/client.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Scope } from '@sentry/core';
2-
import { BaseClient, SDK_VERSION } from '@sentry/core';
2+
import { applySdkMetadata } from '@sentry/core';
3+
import { BaseClient } from '@sentry/core';
34
import type {
45
BrowserClientProfilingOptions,
56
BrowserClientReplayOptions,
@@ -50,18 +51,7 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
5051
*/
5152
public constructor(options: BrowserClientOptions) {
5253
const sdkSource = WINDOW.SENTRY_SDK_SOURCE || getSDKSource();
53-
54-
options._metadata = options._metadata || {};
55-
options._metadata.sdk = options._metadata.sdk || {
56-
name: 'sentry.javascript.browser',
57-
packages: [
58-
{
59-
name: `${sdkSource}:@sentry/browser`,
60-
version: SDK_VERSION,
61-
},
62-
],
63-
version: SDK_VERSION,
64-
};
54+
applySdkMetadata(options, 'browser', ['browser'], sdkSource);
6555

6656
super(options);
6757

packages/bun/src/client.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as os from 'os';
22
import type { ServerRuntimeClientOptions } from '@sentry/core';
3-
import { SDK_VERSION, ServerRuntimeClient } from '@sentry/core';
3+
import { applySdkMetadata } from '@sentry/core';
4+
import { ServerRuntimeClient } from '@sentry/core';
45

56
import type { BunClientOptions } from './types';
67

@@ -16,17 +17,7 @@ export class BunClient extends ServerRuntimeClient<BunClientOptions> {
1617
* @param options Configuration options for this SDK.
1718
*/
1819
public constructor(options: BunClientOptions) {
19-
options._metadata = options._metadata || {};
20-
options._metadata.sdk = options._metadata.sdk || {
21-
name: 'sentry.javascript.bun',
22-
packages: [
23-
{
24-
name: 'npm:@sentry/bun',
25-
version: SDK_VERSION,
26-
},
27-
],
28-
version: SDK_VERSION,
29-
};
20+
applySdkMetadata(options, 'bun');
3021

3122
const clientOptions: ServerRuntimeClientOptions = {
3223
...options,

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export {
8686
spanIsSampled,
8787
} from './utils/spanUtils';
8888
export { getRootSpan } from './utils/getRootSpan';
89+
export { applySdkMetadata } from './utils/sdkMetadata';
8990
export { DEFAULT_ENVIRONMENT } from './constants';
9091
/* eslint-disable deprecation/deprecation */
9192
export { ModuleMetadata } from './integrations/metadata';
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { SDK_VERSION } from '@sentry/core';
2-
import type { Options, SdkInfo } from '@sentry/types';
3-
4-
const PACKAGE_NAME_PREFIX = 'npm:@sentry/';
1+
import type { Options } from '@sentry/types';
2+
import { SDK_VERSION } from '../version';
53

64
/**
75
* A builder for the SDK metadata in the options for the SDK initialization.
@@ -16,16 +14,19 @@ const PACKAGE_NAME_PREFIX = 'npm:@sentry/';
1614
* @param options SDK options object that gets mutated
1715
* @param names list of package names
1816
*/
19-
export function applySdkMetadata(options: Options, names: string[]): void {
20-
options._metadata = options._metadata || {};
21-
options._metadata.sdk =
22-
options._metadata.sdk ||
23-
({
24-
name: 'sentry.javascript.astro',
17+
export function applySdkMetadata(options: Options, name: string, names = [name], source = 'npm'): void {
18+
const metadata = options._metadata || {};
19+
20+
if (!metadata.sdk) {
21+
metadata.sdk = {
22+
name: `sentry.javascript.${name}`,
2523
packages: names.map(name => ({
26-
name: `${PACKAGE_NAME_PREFIX}${name}`,
24+
name: `${source}:@sentry/${name}`,
2725
version: SDK_VERSION,
2826
})),
2927
version: SDK_VERSION,
30-
} as SdkInfo);
28+
};
29+
}
30+
31+
options._metadata = metadata;
3132
}

packages/ember/addon/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getOwnConfig, isDevelopingApp, macroCondition } from '@embroider/macros
55
import { startSpan } from '@sentry/browser';
66
import type { BrowserOptions } from '@sentry/browser';
77
import * as Sentry from '@sentry/browser';
8-
import { SDK_VERSION } from '@sentry/browser';
8+
import { applySdkMetadata } from '@sentry/core';
99
import { GLOBAL_OBJ } from '@sentry/utils';
1010
import Ember from 'ember';
1111

@@ -33,17 +33,7 @@ export function InitSentryForEmber(_runtimeConfig?: BrowserOptions): void {
3333
Object.assign(environmentConfig.sentry, _runtimeConfig || {});
3434
const initConfig = Object.assign({}, environmentConfig.sentry);
3535

36-
initConfig._metadata = initConfig._metadata || {};
37-
initConfig._metadata.sdk = {
38-
name: 'sentry.javascript.ember',
39-
packages: [
40-
{
41-
name: 'npm:@sentry/ember',
42-
version: SDK_VERSION,
43-
},
44-
],
45-
version: SDK_VERSION,
46-
};
36+
applySdkMetadata(initConfig, 'ember');
4737

4838
// Persist Sentry init options so they are identical when performance initializers call init again.
4939
const sentryInitConfig = _getSentryInitConfig();

packages/gatsby/src/sdk.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { SDK_VERSION, init as reactInit } from '@sentry/react';
1+
import { applySdkMetadata } from '@sentry/core';
2+
import { init as reactInit } from '@sentry/react';
23

34
import { getIntegrationsFromOptions } from './utils/integrations';
45
import type { GatsbyOptions } from './utils/types';
@@ -7,18 +8,7 @@ import type { GatsbyOptions } from './utils/types';
78
* Inits the Sentry Gatsby SDK.
89
*/
910
export function init(options: GatsbyOptions): void {
10-
options._metadata = options._metadata || {};
11-
options._metadata.sdk = options._metadata.sdk || {
12-
name: 'sentry.javascript.gatsby',
13-
packages: [
14-
{
15-
name: 'npm:@sentry/gatsby',
16-
version: SDK_VERSION,
17-
},
18-
],
19-
version: SDK_VERSION,
20-
};
21-
11+
applySdkMetadata(options, 'gatsby');
2212
const integrations = getIntegrationsFromOptions(options);
2313
reactInit({
2414
...options,

packages/nextjs/src/client/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hasTracingEnabled } from '@sentry/core';
1+
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
22
import type { BrowserOptions } from '@sentry/react';
33
import {
44
Integrations as OriginalIntegrations,
@@ -10,7 +10,6 @@ import type { EventProcessor, Integration } from '@sentry/types';
1010

1111
import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor';
1212
import { getVercelEnv } from '../common/getVercelEnv';
13-
import { buildMetadata } from '../common/metadata';
1413
import { BrowserTracing } from './browserTracingIntegration';
1514
import { rewriteFramesIntegration } from './rewriteFramesIntegration';
1615
import { applyTunnelRouteOption } from './tunnelRoute';
@@ -50,7 +49,7 @@ export function init(options: BrowserOptions): void {
5049
fixBrowserTracingIntegration(opts);
5150

5251
applyTunnelRouteOption(opts);
53-
buildMetadata(opts, ['nextjs', 'react']);
52+
applySdkMetadata(opts, 'nextjs', ['nextjs', 'react']);
5453

5554
reactInit(opts);
5655

packages/nextjs/src/common/metadata.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/nextjs/src/edge/index.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { SDK_VERSION, addTracingExtensions } from '@sentry/core';
2-
import type { SdkMetadata } from '@sentry/types';
1+
import { addTracingExtensions, applySdkMetadata } from '@sentry/core';
32
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
43
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
54

@@ -21,21 +20,11 @@ export function init(options: VercelEdgeOptions = {}): void {
2120
const customDefaultIntegrations = [...getDefaultIntegrations(options), rewriteFramesIntegration()];
2221

2322
const opts = {
24-
_metadata: {} as SdkMetadata,
2523
defaultIntegrations: customDefaultIntegrations,
2624
...options,
2725
};
2826

29-
opts._metadata.sdk = opts._metadata.sdk || {
30-
name: 'sentry.javascript.nextjs',
31-
packages: [
32-
{
33-
name: 'npm:@sentry/nextjs',
34-
version: SDK_VERSION,
35-
},
36-
],
37-
version: SDK_VERSION,
38-
};
27+
applySdkMetadata(opts, 'nextjs');
3928

4029
vercelEdgeInit(opts);
4130
}

packages/nextjs/src/server/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addTracingExtensions, getClient } from '@sentry/core';
1+
import { addTracingExtensions, applySdkMetadata, getClient } from '@sentry/core';
22
import type { NodeOptions } from '@sentry/node';
33
import {
44
Integrations as OriginalIntegrations,
@@ -12,7 +12,6 @@ import { logger } from '@sentry/utils';
1212
import { DEBUG_BUILD } from '../common/debug-build';
1313
import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor';
1414
import { getVercelEnv } from '../common/getVercelEnv';
15-
import { buildMetadata } from '../common/metadata';
1615
import { isBuild } from '../common/utils/isBuild';
1716
import { Http } from './httpIntegration';
1817
import { OnUncaughtException } from './onUncaughtExceptionIntegration';
@@ -109,7 +108,7 @@ export function init(options: NodeOptions): void {
109108
return;
110109
}
111110

112-
buildMetadata(opts, ['nextjs', 'node']);
111+
applySdkMetadata(opts, 'nextjs', ['nextjs', 'node']);
113112

114113
nodeInit(opts);
115114

packages/node-experimental/src/sdk/client.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NodeClient, SDK_VERSION } from '@sentry/node';
33
import type { Tracer } from '@opentelemetry/api';
44
import { trace } from '@opentelemetry/api';
55
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
6+
import { applySdkMetadata } from '@sentry/core';
67
import type { CaptureContext, Event, EventHint } from '@sentry/types';
78
import { Scope, getIsolationScope } from './scope';
89

@@ -12,17 +13,7 @@ export class NodeExperimentalClient extends NodeClient {
1213
private _tracer: Tracer | undefined;
1314

1415
public constructor(options: ConstructorParameters<typeof NodeClient>[0]) {
15-
options._metadata = options._metadata || {};
16-
options._metadata.sdk = options._metadata.sdk || {
17-
name: 'sentry.javascript.node-experimental',
18-
packages: [
19-
{
20-
name: 'npm:@sentry/node-experimental',
21-
version: SDK_VERSION,
22-
},
23-
],
24-
version: SDK_VERSION,
25-
};
16+
applySdkMetadata(options, 'node-experimental');
2617

2718
super(options);
2819
}

0 commit comments

Comments
 (0)