Skip to content

Commit 5aecd63

Browse files
mydeaandreiborza
authored andcommitted
feat(js): Update docs for custom integrations (#10150)
--------- Co-authored-by: Andrei <[email protected]>
1 parent 2bab1bf commit 5aecd63

File tree

1 file changed

+89
-16
lines changed
  • docs/platforms/javascript/common/configuration/integrations

1 file changed

+89
-16
lines changed

docs/platforms/javascript/common/configuration/integrations/custom.mdx

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ A custom integration can be created and added to the SDK as follows:
1414
function myAwesomeIntegration() {
1515
return {
1616
name: "MyAwesomeIntegration",
17-
setupOnce() {
17+
setup(client) {
1818
// Do something when the SDK is initialized
19+
// The client that is being setup is passed to the hook
1920
},
2021
};
2122
}
@@ -26,22 +27,94 @@ Sentry.init({
2627
});
2728
```
2829

29-
The JavaScript Core Package (`@sentry/core`) also exports a helper function called `defineIntegration` to help with type-safety:
30+
All hooks on an integration are optional. The only required field is the `name`. You can use one or multiple of the following hooks in a custom integration:
3031

31-
```typescript
32-
import { defineIntegration } from "@sentry/core";
32+
### `setup`
3333

34-
const myAwesomeIntegration = defineIntegration(() => {
35-
return {
36-
name: "MyAwesomeIntegration",
37-
setupOnce() {
38-
// Do something when the SDK is initialized
39-
},
40-
};
41-
});
34+
The `setup` hook is called when the SDK is initialized. It receives the client instance as an argument.
35+
You should use this if you want to run some code on initialization.
4236

43-
Sentry.init({
44-
// ...
45-
integrations: [myAwesomeIntegration()],
46-
});
37+
```javascript
38+
const integration = {
39+
name: "MyAwesomeIntegration",
40+
setup(client) {
41+
setupCustomSentryListener(client);
42+
},
43+
};
44+
```
45+
46+
### `processEvent`
47+
48+
This hook can be used to modify events before they are sent to Sentry. It receives the event as an argument and should return the modified event. The hook also receives a hint object that may hold additional event metadata, and the client that is sending the event. You can also return `null` to drop the event from being sent.
49+
50+
```javascript
51+
const integration = {
52+
name: "MyAwesomeIntegration",
53+
processEvent(event, hint, client) {
54+
event.extra = {
55+
...event.extra,
56+
myCustomTag: "value",
57+
};
58+
// Return the modified event,
59+
// or return `null` to drop the event
60+
return event;
61+
},
62+
};
63+
```
64+
65+
You can also return a promise that resolves with an event or `null`. However, this is not recommended and should be avoided wherever possible, because it can delay event sending.
66+
67+
### `preprocessEvent`
68+
69+
This hook is similar to `processEvent`, but it is called before the event is passed to any other `processEvent` hook. It can be used in places where the order of processing is important.
70+
71+
In most cases, you should just use `processEvent`. Only use `preprocessEvent` if you need to ensure that your hook is called before any other `processEvent` hook.
72+
73+
Similar to `processEvent`, this hooks receives the event, hint, and client as arguments. However, this hook does not allow to return a modified event or `null` to drop the event - instead, you can only mutate the passed in event in this hook:
74+
75+
```javascript
76+
const integration = {
77+
name: "MyAwesomeIntegration",
78+
preprocessEvent(event, hint, client) {
79+
event.extra = {
80+
...event.extra,
81+
myCustomTag: "value",
82+
};
83+
// Nothing to return, just mutate the event
84+
},
85+
};
86+
```
87+
88+
### `setupOnce`
89+
90+
This hook is similar to `setup`, but it is only run once, even if the SDK is re-initialized. It does not receive any arguments.
91+
You should probably not use this, but use `setup` instead. The only reason to use `setupOnce` is e.g. when you may be calling `Sentry.init()` multiple times and you want to ensure a certain piece of code is only run once.
92+
93+
```javascript
94+
const integration = {
95+
name: "MyAwesomeIntegration",
96+
setupOnce() {
97+
wrapLibrary();
98+
},
99+
};
100+
```
101+
102+
### `afterAllSetup`
103+
104+
This hook is triggered after `setupOnce()` and `setup()` have been called for all integrations.
105+
You can use it if it is important that all other integrations have been run before.
106+
107+
In most cases, you should not need to use this hook, and should use `setup` instead.
108+
109+
The hook receives the `client` that is being set up as the first argument.
110+
111+
```javascript
112+
const integration = {
113+
name: "MyAwesomeIntegration",
114+
afterAllSetup(client) {
115+
// We can be sure that all other integrations
116+
// have run their `setup` and `setupOnce` hooks now
117+
startSomeThing(client);
118+
},
119+
};
47120
```

0 commit comments

Comments
 (0)