Skip to content

ref: Clean up new integrations API and pave migration path #10165

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

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,11 @@ export function convertIntegrationFnToClass<Fn extends IntegrationFn>(
new (...args: Parameters<Fn>): Integration & ReturnType<Fn>;
};
}

/**
* Utility function to aid with creating Sentry SDK integrations.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function defineSentryIntegration<F extends (...args: any[]) => Integration>(integrationFactory: F): F {
return integrationFactory;
}
67 changes: 40 additions & 27 deletions packages/core/src/integrations/functiontostring.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
import type { IntegrationFn, WrappedFunction } from '@sentry/types';
import type { Integration, WrappedFunction } from '@sentry/types';
import { getOriginalFunction } from '@sentry/utils';
import { convertIntegrationFnToClass } from '../integration';
import { defineSentryIntegration } from '../integration';

let originalFunctionToString: () => void;

const INTEGRATION_NAME = 'FunctionToString';

const functionToStringIntegration: IntegrationFn = () => {
return {
name: INTEGRATION_NAME,
setupOnce() {
// eslint-disable-next-line @typescript-eslint/unbound-method
originalFunctionToString = Function.prototype.toString;

// intrinsics (like Function.prototype) might be immutable in some environments
// e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal)
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Function.prototype.toString = function (this: WrappedFunction, ...args: any[]): string {
const context = getOriginalFunction(this) || this;
return originalFunctionToString.apply(context, args);
};
} catch {
// ignore errors here, just don't patch this
}
},
};
};

/** Patch toString calls to return proper name for wrapped functions */
// eslint-disable-next-line deprecation/deprecation
export const FunctionToString = convertIntegrationFnToClass(INTEGRATION_NAME, functionToStringIntegration);
/**
* Patch toString calls to return proper name for wrapped functions.
*/
export const functionToStringIntegration = defineSentryIntegration(() => {
// eslint-disable-next-line deprecation/deprecation
return new FunctionToString();
});

/**
* Patch toString calls to return proper name for wrapped functions.
*/
export class FunctionToString implements Integration {
public static id = INTEGRATION_NAME;

public name: typeof INTEGRATION_NAME;

public constructor() {
this.name = INTEGRATION_NAME;
}

// eslint-disable-next-line jsdoc/require-jsdoc
public setupOnce(): void {
// eslint-disable-next-line @typescript-eslint/unbound-method
originalFunctionToString = Function.prototype.toString;

// intrinsics (like Function.prototype) might be immutable in some environments
// e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal)
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Function.prototype.toString = function (this: WrappedFunction, ...args: any[]): string {
const context = getOriginalFunction(this) || this;
return originalFunctionToString.apply(context, args);
};
} catch {
// ignore errors here, just don't patch this
}
}
}
48 changes: 31 additions & 17 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Event, IntegrationFn, StackFrame } from '@sentry/types';
/* eslint-disable jsdoc/require-jsdoc */
import type { Client, Event, EventHint, Integration, StackFrame } from '@sentry/types';
import { getEventDescription, logger, stringMatchesSomePattern } from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';
import { convertIntegrationFnToClass } from '../integration';
import { defineSentryIntegration } from '../integration';

// "Script error." is hard coded into browsers for errors that it can't read.
// this is the result of a script being pulled in from an external domain and CORS.
Expand Down Expand Up @@ -30,22 +31,35 @@ export interface InboundFiltersOptions {
}

const INTEGRATION_NAME = 'InboundFilters';
const inboundFiltersIntegration: IntegrationFn = (options: Partial<InboundFiltersOptions>) => {
return {
name: INTEGRATION_NAME,
// TODO v8: Remove this
setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
processEvent(event, _hint, client) {
const clientOptions = client.getOptions();
const mergedOptions = _mergeOptions(options, clientOptions);
return _shouldDropEvent(event, mergedOptions) ? null : event;
},
};
};

/** Inbound filters configurable by the user */
// eslint-disable-next-line deprecation/deprecation
export const InboundFilters = convertIntegrationFnToClass(INTEGRATION_NAME, inboundFiltersIntegration);
export const inboundFiltersIntegration = defineSentryIntegration((options?: Partial<InboundFiltersOptions>) => {
// eslint-disable-next-line deprecation/deprecation
return new InboundFilters(options);
});

/**
* Inbound filters configurable by the user.
*/
export class InboundFilters implements Integration {
public static id = INTEGRATION_NAME;

public name: string;

public constructor(private _options?: Partial<InboundFiltersOptions>) {
this.name = INTEGRATION_NAME;
}

// TODO v8: Remove this
public setupOnce(): void {
// noop
}

public preprocessEvent(event: Event, hint: EventHint | undefined, client: Client): Event | null {
const clientOptions = client.getOptions();
const mergedOptions = _mergeOptions(this._options, clientOptions);
return _shouldDropEvent(event, mergedOptions) ? null : event;
}
}

function _mergeOptions(
internalOptions: Partial<InboundFiltersOptions> = {},
Expand Down
2 changes: 2 additions & 0 deletions packages/vercel-edge/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ declare const process: {
const nodeStackParser = createStackParser(nodeStackLineParser());

export const defaultIntegrations = [
// eslint-disable-next-line deprecation/deprecation
new CoreIntegrations.InboundFilters(),
// eslint-disable-next-line deprecation/deprecation
new CoreIntegrations.FunctionToString(),
new CoreIntegrations.LinkedErrors(),
new WinterCGFetch(),
Expand Down