You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/platforms/javascript/common/configuration/integrations/custom.mdx
+89-16Lines changed: 89 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,8 +14,9 @@ A custom integration can be created and added to the SDK as follows:
14
14
functionmyAwesomeIntegration() {
15
15
return {
16
16
name:"MyAwesomeIntegration",
17
-
setupOnce() {
17
+
setup(client) {
18
18
// Do something when the SDK is initialized
19
+
// The client that is being setup is passed to the hook
19
20
},
20
21
};
21
22
}
@@ -26,22 +27,94 @@ Sentry.init({
26
27
});
27
28
```
28
29
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:
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.
42
36
43
-
Sentry.init({
44
-
// ...
45
-
integrations: [myAwesomeIntegration()],
46
-
});
37
+
```javascript
38
+
constintegration= {
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
+
constintegration= {
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
+
returnevent;
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
+
constintegration= {
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
+
constintegration= {
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
+
constintegration= {
113
+
name:"MyAwesomeIntegration",
114
+
afterAllSetup(client) {
115
+
// We can be sure that all other integrations
116
+
// have run their `setup` and `setupOnce` hooks now
0 commit comments