-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add type & utility for function-based integrations #9818
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
Conversation
size-limit report 📦
|
*/ | ||
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void { | ||
// noop | ||
const inboundFiltersIntegration = makeIntegrationFn('InboundFilters', (options: Partial<InboundFiltersOptions>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just converted a single integration for now to show this in "reality", but I can also extract this out into a follow up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just keep it here, the change is pretty small.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I'm missing something here but I'm thinking about the defineConfig
patterns from Vite, Astro, Nuxt, etc and I'm wondering if we could simplify this helper function a little in a similar manner; or actually even more than that:
const inboundFilters =
(options: Partial<InboundFiltersOptions>) => {
// do whatever you need to do here e.g. assign default values
return {
name: 'InboundFilters',
processEvent(event, _hint, client) {
const clientOptions = client.getOptions();
const mergedOptions = _mergeOptions(options, clientOptions);
return _shouldDropEvent(event, mergedOptions) ? null : event;
}
}
});
I get that this simplification doesn't allow for name checks before initalization but I'm wondering if we even need that. Usually, instrumentation should happen in setup(once)
anyway.
export { | ||
getIntegrationsToSetup, | ||
addIntegration, | ||
// eslint-disable-next-line deprecation/deprecation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are using deprecated functions, I think we should disable this rule instead of overriding it everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here was to make it easier to spot that this should be removed in v8 🤔 no strong feelings, I just wanted to make it clear that this is a temporary thing that will be removed in v8 again - generally, we should avoid using deprecated stuff ourselves, so I think the rule is good.
@@ -84,8 +64,7 @@ export function _mergeOptions( | |||
}; | |||
} | |||
|
|||
/** JSDoc */ | |||
export function _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean { | |||
function _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this function start with underscore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they are "private" - we are not super consistent there, sometimes we prefix them and sometimes not. but specifically here I did not change that, just removed the export
as we don't actually use it anywhere!
Hmm, can we get rid of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, can we get rid of the id
I think we can because id
is basically equivalent to name
everywhere.
*/ | ||
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void { | ||
// noop | ||
const inboundFiltersIntegration = makeIntegrationFn('InboundFilters', (options: Partial<InboundFiltersOptions>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just keep it here, the change is pretty small.
Hmm, if we get rid of the Function: function makeIntegrationFn<Fn extends IntegrationFn>(fn: Fn): Fn {
return fn;
} But using it allows to omit all types: const inboundFiltersIntegration = makeIntegrationFn((options: Partial<InboundFiltersOptions>) => {
return {
name: INTEGRATION_NAME,
// look mom, no types!
processEvent(event, _hint, client) {
const clientOptions = client.getOptions();
const mergedOptions = _mergeOptions(options, clientOptions);
return _shouldDropEvent(event, mergedOptions) ? null : event;
},
};
}); is this worth it?? |
packages/types/src/integration.ts
Outdated
export type IntegrationFn<Fn extends (...rest: any[]) => IntegrationFnResult> = { id: string } & Fn; | ||
|
||
export interface IntegrationFnResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these are necessary or a good idea. I would prefer if we just have an Integration
interface. These types seem overkill and don't add much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is that for now these are not the same, as setupOnce
is optional here. In v8 we can get rid of this and have just a single Integration
interface again!
packages/core/src/integration.ts
Outdated
* This will ensure to add the given name both to the function definition (as id), | ||
* as well as to the integration return value. | ||
*/ | ||
export function makeIntegrationFn< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is imo not really doing anything. I think we (and eventually our users) are just better off defining integrations as follows.
export function MyCustomIntegration(): Integration {};
Easy, simple, well-typed. The rollup and Vite ecosystem is proof that this pattern works and is well understood.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will remove this!
This reverts commit 31887622b297c094e37ccfa00b2a8708bd0d73c7.
4db0b7a
to
2a64201
Compare
2a64201
to
87446e9
Compare
Updated this to remove the |
This PR adds new types for function-based integrations, that eventually (in v8) should fully replace the class-based functions.
This also introduces a small helper function to make writing such integrations easier (as we need to set an id/name etc. on the integration). With this, you can write an integration like this:
And you get a fully typed integration ready to go!
For backwards compatibility, and so that we can actually start converting integrations in v7 already, this PR also adds a small utility
convertIntegrationFnToClass()
to convert such an integration to the "current" integration class syntax.So we can actually already start porting integrations over like this:
Then, in v8 we only have to remove all the
convertIntegrationFnToClass
calls, export the integration functions directly, and update the overall integration types which can be passed toinit()
etc.