Skip to content

ref: Streamline SDK metadata handling #10251

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 1 commit into from
Jan 19, 2024
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
17 changes: 3 additions & 14 deletions packages/angular-ivy/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { VERSION } from '@angular/core';
import type { BrowserOptions } from '@sentry/browser';
import { getDefaultIntegrations } from '@sentry/browser';
import { SDK_VERSION, init as browserInit, setContext } from '@sentry/browser';
import type { SdkMetadata } from '@sentry/types';
import { getDefaultIntegrations, init as browserInit, setContext } from '@sentry/browser';
import { applySdkMetadata } from '@sentry/core';
import { logger } from '@sentry/utils';

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

opts._metadata.sdk = opts._metadata.sdk || {
name: 'sentry.javascript.angular-ivy',
packages: [
{
name: 'npm:@sentry/angular-ivy',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
applySdkMetadata(opts, 'angular-ivy');

checkAndSetAngularVersion();
browserInit(opts);
Expand Down
17 changes: 3 additions & 14 deletions packages/angular/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { VERSION } from '@angular/core';
import type { BrowserOptions } from '@sentry/browser';
import { getDefaultIntegrations } from '@sentry/browser';
import { SDK_VERSION, init as browserInit, setContext } from '@sentry/browser';
import type { SdkMetadata } from '@sentry/types';
import { getDefaultIntegrations, init as browserInit, setContext } from '@sentry/browser';
import { applySdkMetadata } from '@sentry/core';
import { logger } from '@sentry/utils';

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

opts._metadata.sdk = opts._metadata.sdk || {
name: 'sentry.javascript.angular',
packages: [
{
name: 'npm:@sentry/angular',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
applySdkMetadata(opts, 'angular');

checkAndSetAngularVersion();
browserInit(opts);
Expand Down
6 changes: 2 additions & 4 deletions packages/astro/src/client/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import {
init as initBrowserSdk,
setTag,
} from '@sentry/browser';
import { hasTracingEnabled } from '@sentry/core';
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
import type { Integration } from '@sentry/types';

import { applySdkMetadata } from '../common/metadata';

// Treeshakable guard to remove all code related to tracing
declare const __SENTRY_TRACING__: boolean;

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

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

initBrowserSdk(opts);

Expand Down
5 changes: 2 additions & 3 deletions packages/astro/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { applySdkMetadata } from '@sentry/core';
import type { NodeOptions } from '@sentry/node';
import { init as initNodeSdk, setTag } from '@sentry/node';

import { applySdkMetadata } from '../common/metadata';

/**
*
* @param options
*/
export function init(options: NodeOptions): void {
applySdkMetadata(options, ['astro', 'node']);
applySdkMetadata(options, 'astro', ['astro', 'node']);

initNodeSdk(options);

Expand Down
16 changes: 3 additions & 13 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Scope } from '@sentry/core';
import { BaseClient, SDK_VERSION } from '@sentry/core';
import { applySdkMetadata } from '@sentry/core';
import { BaseClient } from '@sentry/core';
import type {
BrowserClientProfilingOptions,
BrowserClientReplayOptions,
Expand Down Expand Up @@ -50,18 +51,7 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
*/
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: `${sdkSource}:@sentry/browser`,
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
applySdkMetadata(options, 'browser', ['browser'], sdkSource);

super(options);

Expand Down
15 changes: 3 additions & 12 deletions packages/bun/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as os from 'os';
import type { ServerRuntimeClientOptions } from '@sentry/core';
import { SDK_VERSION, ServerRuntimeClient } from '@sentry/core';
import { applySdkMetadata } from '@sentry/core';
import { ServerRuntimeClient } from '@sentry/core';

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

Expand All @@ -16,17 +17,7 @@ export class BunClient extends ServerRuntimeClient<BunClientOptions> {
* @param options Configuration options for this SDK.
*/
public constructor(options: BunClientOptions) {
options._metadata = options._metadata || {};
options._metadata.sdk = options._metadata.sdk || {
name: 'sentry.javascript.bun',
packages: [
{
name: 'npm:@sentry/bun',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
applySdkMetadata(options, 'bun');

const clientOptions: ServerRuntimeClientOptions = {
...options,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export {
spanIsSampled,
} from './utils/spanUtils';
export { getRootSpan } from './utils/getRootSpan';
export { applySdkMetadata } from './utils/sdkMetadata';
export { DEFAULT_ENVIRONMENT } from './constants';
/* eslint-disable deprecation/deprecation */
export { ModuleMetadata } from './integrations/metadata';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { SDK_VERSION } from '@sentry/core';
import type { Options, SdkInfo } from '@sentry/types';

const PACKAGE_NAME_PREFIX = 'npm:@sentry/';
import type { Options } from '@sentry/types';
import { SDK_VERSION } from '../version';

/**
* A builder for the SDK metadata in the options for the SDK initialization.
Expand All @@ -16,16 +14,19 @@ const PACKAGE_NAME_PREFIX = 'npm:@sentry/';
* @param options SDK options object that gets mutated
* @param names list of package names
*/
export function applySdkMetadata(options: Options, names: string[]): void {
options._metadata = options._metadata || {};
options._metadata.sdk =
options._metadata.sdk ||
({
name: 'sentry.javascript.astro',
export function applySdkMetadata(options: Options, name: string, names = [name], source = 'npm'): void {
const metadata = options._metadata || {};

if (!metadata.sdk) {
metadata.sdk = {
name: `sentry.javascript.${name}`,
packages: names.map(name => ({
name: `${PACKAGE_NAME_PREFIX}${name}`,
name: `${source}:@sentry/${name}`,
version: SDK_VERSION,
})),
version: SDK_VERSION,
} as SdkInfo);
};
}

options._metadata = metadata;
}
14 changes: 2 additions & 12 deletions packages/ember/addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getOwnConfig, isDevelopingApp, macroCondition } from '@embroider/macros
import { startSpan } from '@sentry/browser';
import type { BrowserOptions } from '@sentry/browser';
import * as Sentry from '@sentry/browser';
import { SDK_VERSION } from '@sentry/browser';
import { applySdkMetadata } from '@sentry/core';
import { GLOBAL_OBJ } from '@sentry/utils';
import Ember from 'ember';

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

initConfig._metadata = initConfig._metadata || {};
initConfig._metadata.sdk = {
name: 'sentry.javascript.ember',
packages: [
{
name: 'npm:@sentry/ember',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
applySdkMetadata(initConfig, 'ember');

// Persist Sentry init options so they are identical when performance initializers call init again.
const sentryInitConfig = _getSentryInitConfig();
Expand Down
16 changes: 3 additions & 13 deletions packages/gatsby/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SDK_VERSION, init as reactInit } from '@sentry/react';
import { applySdkMetadata } from '@sentry/core';
import { init as reactInit } from '@sentry/react';

import { getIntegrationsFromOptions } from './utils/integrations';
import type { GatsbyOptions } from './utils/types';
Expand All @@ -7,18 +8,7 @@ import type { GatsbyOptions } from './utils/types';
* Inits the Sentry Gatsby SDK.
*/
export function init(options: GatsbyOptions): void {
options._metadata = options._metadata || {};
options._metadata.sdk = options._metadata.sdk || {
name: 'sentry.javascript.gatsby',
packages: [
{
name: 'npm:@sentry/gatsby',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};

applySdkMetadata(options, 'gatsby');
const integrations = getIntegrationsFromOptions(options);
reactInit({
...options,
Expand Down
5 changes: 2 additions & 3 deletions packages/nextjs/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasTracingEnabled } from '@sentry/core';
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
import type { BrowserOptions } from '@sentry/react';
import {
Integrations as OriginalIntegrations,
Expand All @@ -10,7 +10,6 @@ import type { EventProcessor, Integration } from '@sentry/types';

import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor';
import { getVercelEnv } from '../common/getVercelEnv';
import { buildMetadata } from '../common/metadata';
import { BrowserTracing } from './browserTracingIntegration';
import { rewriteFramesIntegration } from './rewriteFramesIntegration';
import { applyTunnelRouteOption } from './tunnelRoute';
Expand Down Expand Up @@ -50,7 +49,7 @@ export function init(options: BrowserOptions): void {
fixBrowserTracingIntegration(opts);

applyTunnelRouteOption(opts);
buildMetadata(opts, ['nextjs', 'react']);
applySdkMetadata(opts, 'nextjs', ['nextjs', 'react']);

reactInit(opts);

Expand Down
23 changes: 0 additions & 23 deletions packages/nextjs/src/common/metadata.ts

This file was deleted.

15 changes: 2 additions & 13 deletions packages/nextjs/src/edge/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SDK_VERSION, addTracingExtensions } from '@sentry/core';
import type { SdkMetadata } from '@sentry/types';
import { addTracingExtensions, applySdkMetadata } from '@sentry/core';
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';

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

const opts = {
_metadata: {} as SdkMetadata,
defaultIntegrations: customDefaultIntegrations,
...options,
};

opts._metadata.sdk = opts._metadata.sdk || {
name: 'sentry.javascript.nextjs',
packages: [
{
name: 'npm:@sentry/nextjs',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
applySdkMetadata(opts, 'nextjs');

vercelEdgeInit(opts);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addTracingExtensions, getClient } from '@sentry/core';
import { addTracingExtensions, applySdkMetadata, getClient } from '@sentry/core';
import type { NodeOptions } from '@sentry/node';
import {
Integrations as OriginalIntegrations,
Expand All @@ -12,7 +12,6 @@ import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../common/debug-build';
import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor';
import { getVercelEnv } from '../common/getVercelEnv';
import { buildMetadata } from '../common/metadata';
import { isBuild } from '../common/utils/isBuild';
import { Http } from './httpIntegration';
import { OnUncaughtException } from './onUncaughtExceptionIntegration';
Expand Down Expand Up @@ -109,7 +108,7 @@ export function init(options: NodeOptions): void {
return;
}

buildMetadata(opts, ['nextjs', 'node']);
applySdkMetadata(opts, 'nextjs', ['nextjs', 'node']);

nodeInit(opts);

Expand Down
13 changes: 2 additions & 11 deletions packages/node-experimental/src/sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NodeClient, SDK_VERSION } from '@sentry/node';
import type { Tracer } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { applySdkMetadata } from '@sentry/core';
import type { CaptureContext, Event, EventHint } from '@sentry/types';
import { Scope, getIsolationScope } from './scope';

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

public constructor(options: ConstructorParameters<typeof NodeClient>[0]) {
options._metadata = options._metadata || {};
options._metadata.sdk = options._metadata.sdk || {
name: 'sentry.javascript.node-experimental',
packages: [
{
name: 'npm:@sentry/node-experimental',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
applySdkMetadata(options, 'node-experimental');

super(options);
}
Expand Down
Loading