@strapi/plugin-sentry 4.0.0-next.0 → 4.0.0-next.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -7
- package/package.json +7 -4
- package/{config/functions → server}/bootstrap.js +4 -2
- package/server/config.js +10 -0
- package/server/middlewares/index.js +7 -0
- package/server/middlewares/sentry/index.js +36 -0
- package/{services → server/services}/__tests__/sentry.test.js +12 -18
- package/server/services/index.js +7 -0
- package/{services/sentry.js → server/services/sentry/index.js} +5 -9
- package/strapi-server.js +13 -0
- package/config/routes.json +0 -3
- package/config/settings.json +0 -5
- package/controllers/sentry.js +0 -9
- package/middlewares/sentry/defaults.json +0 -5
- package/middlewares/sentry/index.js +0 -33
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ module.exports = ({ env }) => ({
|
|
|
49
49
|
You can access a Sentry service throughout your app.
|
|
50
50
|
|
|
51
51
|
```js
|
|
52
|
-
const sentryService = strapi.
|
|
52
|
+
const sentryService = strapi.plugin('sentry').service('sentry');
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
This service exposes the following methods:
|
|
@@ -65,13 +65,19 @@ try {
|
|
|
65
65
|
// Your code here
|
|
66
66
|
} catch (error) {
|
|
67
67
|
// Either send a simple error
|
|
68
|
-
strapi
|
|
68
|
+
strapi
|
|
69
|
+
.plugin('sentry')
|
|
70
|
+
.service('sentry')
|
|
71
|
+
.sendError(error);
|
|
69
72
|
|
|
70
73
|
// Or send an error with a customized Sentry scope
|
|
71
|
-
strapi
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
strapi
|
|
75
|
+
.plugin('sentry')
|
|
76
|
+
.service('sentry')
|
|
77
|
+
.sendError(error, (scope, sentryInstance) => {
|
|
78
|
+
// Customize the scope here
|
|
79
|
+
scope.setTag('my_custom_tag', 'Tag value');
|
|
80
|
+
});
|
|
75
81
|
throw error;
|
|
76
82
|
}
|
|
77
83
|
```
|
|
@@ -83,7 +89,10 @@ Use it if you need direct access to the Sentry instance, which should already al
|
|
|
83
89
|
**Example**
|
|
84
90
|
|
|
85
91
|
```js
|
|
86
|
-
const sentryInstance = strapi
|
|
92
|
+
const sentryInstance = strapi
|
|
93
|
+
.plugin('sentry')
|
|
94
|
+
.service('sentry')
|
|
95
|
+
.getInstance();
|
|
87
96
|
```
|
|
88
97
|
|
|
89
98
|
## Disabling
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/plugin-sentry",
|
|
3
|
-
"version": "4.0.0-next.
|
|
3
|
+
"version": "4.0.0-next.12",
|
|
4
4
|
"description": "Send Strapi error events to Sentry",
|
|
5
5
|
"strapi": {
|
|
6
|
-
"name": "
|
|
6
|
+
"name": "sentry",
|
|
7
|
+
"displayName": "Sentry",
|
|
7
8
|
"icon": "plug",
|
|
8
|
-
"description": "sentry.plugin.description"
|
|
9
|
+
"description": "sentry.plugin.description",
|
|
10
|
+
"kind": "plugin"
|
|
9
11
|
},
|
|
10
12
|
"dependencies": {
|
|
11
13
|
"@sentry/node": "6.7.1"
|
|
@@ -26,5 +28,6 @@
|
|
|
26
28
|
"node": ">=12.x.x <=16.x.x",
|
|
27
29
|
"npm": ">=6.0.0"
|
|
28
30
|
},
|
|
29
|
-
"license": "SEE LICENSE IN LICENSE"
|
|
31
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
32
|
+
"gitHead": "67af23d827fd48e7782e509a78d0ba66e67e2215"
|
|
30
33
|
}
|
package/server/config.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
defaults: { sentry: { enabled: true } },
|
|
5
|
+
load: {
|
|
6
|
+
beforeInitialize() {
|
|
7
|
+
strapi.config.middleware.load.after.unshift('sentry');
|
|
8
|
+
},
|
|
9
|
+
initialize() {
|
|
10
|
+
const sentry = strapi.plugin('sentry').service('sentry');
|
|
11
|
+
sentry.init();
|
|
12
|
+
|
|
13
|
+
strapi.server.use(async (ctx, next) => {
|
|
14
|
+
try {
|
|
15
|
+
await next();
|
|
16
|
+
} catch (error) {
|
|
17
|
+
sentry.sendError(error, (scope, sentryInstance) => {
|
|
18
|
+
scope.addEventProcessor(event => {
|
|
19
|
+
// Parse Koa context to add error metadata
|
|
20
|
+
return sentryInstance.Handlers.parseRequest(event, ctx.request, {
|
|
21
|
+
// Don't parse the transaction name, we'll do it manually
|
|
22
|
+
transaction: false,
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
// Manually add transaction name
|
|
26
|
+
scope.setTag('transaction', `${ctx.method} ${ctx.request.url}`);
|
|
27
|
+
// Manually add Strapi version
|
|
28
|
+
scope.setTag('strapi_version', strapi.config.info.strapi);
|
|
29
|
+
scope.setTag('method', ctx.method);
|
|
30
|
+
});
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
};
|
|
@@ -19,25 +19,21 @@ jest.mock('@sentry/node', () => {
|
|
|
19
19
|
};
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
const defaultConfig = require('../../config
|
|
22
|
+
const sentryServiceLoader = require('../sentry');
|
|
23
|
+
const defaultConfig = require('../../config').default;
|
|
24
24
|
|
|
25
25
|
describe('Sentry service', () => {
|
|
26
26
|
beforeEach(() => {
|
|
27
27
|
// Reset Strapi state
|
|
28
28
|
global.strapi = {
|
|
29
|
-
config: {
|
|
30
|
-
|
|
31
|
-
sentry: {
|
|
32
|
-
config: defaultConfig,
|
|
33
|
-
},
|
|
29
|
+
config: {
|
|
30
|
+
get: () => defaultConfig,
|
|
34
31
|
},
|
|
35
32
|
log: {
|
|
36
33
|
warn: jest.fn(),
|
|
37
34
|
info: jest.fn(),
|
|
38
35
|
},
|
|
39
36
|
};
|
|
40
|
-
sentryService = require('../sentry');
|
|
41
37
|
});
|
|
42
38
|
|
|
43
39
|
afterEach(() => {
|
|
@@ -46,6 +42,7 @@ describe('Sentry service', () => {
|
|
|
46
42
|
});
|
|
47
43
|
|
|
48
44
|
it('disables Sentry when no DSN is provided', () => {
|
|
45
|
+
const sentryService = sentryServiceLoader({ strapi });
|
|
49
46
|
sentryService.init();
|
|
50
47
|
expect(strapi.log.info).toHaveBeenCalledWith(expect.stringMatching(/disabled/i));
|
|
51
48
|
|
|
@@ -54,9 +51,8 @@ describe('Sentry service', () => {
|
|
|
54
51
|
});
|
|
55
52
|
|
|
56
53
|
it('disables Sentry when an invalid DSN is provided', () => {
|
|
57
|
-
global.strapi.
|
|
58
|
-
|
|
59
|
-
};
|
|
54
|
+
global.strapi.config.get = () => ({ dsn: INVALID_DSN });
|
|
55
|
+
const sentryService = sentryServiceLoader({ strapi });
|
|
60
56
|
sentryService.init();
|
|
61
57
|
expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/could not set up sentry/i));
|
|
62
58
|
|
|
@@ -65,14 +61,14 @@ describe('Sentry service', () => {
|
|
|
65
61
|
});
|
|
66
62
|
|
|
67
63
|
it("doesn't send events before init", () => {
|
|
64
|
+
const sentryService = sentryServiceLoader({ strapi });
|
|
68
65
|
sentryService.sendError(Error());
|
|
69
66
|
expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/cannot send event/i));
|
|
70
67
|
});
|
|
71
68
|
|
|
72
69
|
it('initializes and sends errors', () => {
|
|
73
|
-
global.strapi.
|
|
74
|
-
|
|
75
|
-
};
|
|
70
|
+
global.strapi.config.get = () => ({ dsn: VALID_DSN, sendMetadata: true });
|
|
71
|
+
const sentryService = sentryServiceLoader({ strapi });
|
|
76
72
|
sentryService.init();
|
|
77
73
|
|
|
78
74
|
// Saves the instance correctly
|
|
@@ -92,10 +88,8 @@ describe('Sentry service', () => {
|
|
|
92
88
|
|
|
93
89
|
it('does not not send metadata when the option is disabled', () => {
|
|
94
90
|
// Init with metadata option disabled
|
|
95
|
-
global.strapi.
|
|
96
|
-
|
|
97
|
-
sendMetadata: false,
|
|
98
|
-
};
|
|
91
|
+
global.strapi.config.get = () => ({ dsn: VALID_DSN, sendMetadata: false });
|
|
92
|
+
const sentryService = sentryServiceLoader({ strapi });
|
|
99
93
|
sentryService.init();
|
|
100
94
|
|
|
101
95
|
// Send error
|
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
// FIXME
|
|
3
3
|
/* eslint-disable import/extensions */
|
|
4
4
|
const Sentry = require('@sentry/node');
|
|
5
|
-
const defaultSettings = require('../config/settings.json');
|
|
6
5
|
|
|
7
|
-
const createSentryService =
|
|
6
|
+
const createSentryService = strapi => {
|
|
8
7
|
let isReady = false;
|
|
9
8
|
let instance = null;
|
|
10
9
|
let settings = {};
|
|
@@ -15,22 +14,19 @@ const createSentryService = () => {
|
|
|
15
14
|
*/
|
|
16
15
|
init() {
|
|
17
16
|
// Make sure there isn't a Sentry instance already running
|
|
18
|
-
if (instance
|
|
17
|
+
if (instance !== null) {
|
|
19
18
|
return this;
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
// Retrieve user settings and merge them with the default ones
|
|
23
|
-
settings =
|
|
24
|
-
...defaultSettings,
|
|
25
|
-
...strapi.plugins.sentry.config,
|
|
26
|
-
};
|
|
22
|
+
settings = strapi.config.get('plugin.sentry');
|
|
27
23
|
|
|
28
24
|
try {
|
|
29
25
|
// Don't init Sentry if no DSN was provided
|
|
30
26
|
if (settings.dsn) {
|
|
31
27
|
Sentry.init({
|
|
32
28
|
dsn: settings.dsn,
|
|
33
|
-
environment: strapi.config.environment,
|
|
29
|
+
environment: strapi.config.get('environment'),
|
|
34
30
|
...settings.init,
|
|
35
31
|
});
|
|
36
32
|
// Store the successfully initialized Sentry instance
|
|
@@ -85,4 +81,4 @@ const createSentryService = () => {
|
|
|
85
81
|
};
|
|
86
82
|
};
|
|
87
83
|
|
|
88
|
-
module.exports = createSentryService();
|
|
84
|
+
module.exports = ({ strapi }) => createSentryService(strapi);
|
package/strapi-server.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const bootstrap = require('./server/bootstrap');
|
|
4
|
+
const services = require('./server/services');
|
|
5
|
+
const middlewares = require('./server/middlewares');
|
|
6
|
+
const config = require('./server/config');
|
|
7
|
+
|
|
8
|
+
module.exports = () => ({
|
|
9
|
+
bootstrap,
|
|
10
|
+
config,
|
|
11
|
+
middlewares,
|
|
12
|
+
services,
|
|
13
|
+
});
|
package/config/routes.json
DELETED
package/config/settings.json
DELETED
package/controllers/sentry.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports = strapi => ({
|
|
4
|
-
beforeInitialize() {
|
|
5
|
-
strapi.config.middleware.load.after.unshift('sentry');
|
|
6
|
-
},
|
|
7
|
-
initialize() {
|
|
8
|
-
const { sentry } = strapi.plugins.sentry.services;
|
|
9
|
-
sentry.init();
|
|
10
|
-
|
|
11
|
-
strapi.app.use(async (ctx, next) => {
|
|
12
|
-
try {
|
|
13
|
-
await next();
|
|
14
|
-
} catch (error) {
|
|
15
|
-
sentry.sendError(error, (scope, sentryInstance) => {
|
|
16
|
-
scope.addEventProcessor(event => {
|
|
17
|
-
// Parse Koa context to add error metadata
|
|
18
|
-
return sentryInstance.Handlers.parseRequest(event, ctx.request, {
|
|
19
|
-
// Don't parse the transaction name, we'll do it manually
|
|
20
|
-
transaction: false,
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
// Manually add transaction name
|
|
24
|
-
scope.setTag('transaction', `${ctx.method} ${ctx.request.url}`);
|
|
25
|
-
// Manually add Strapi version
|
|
26
|
-
scope.setTag('strapi_version', strapi.config.info.strapi);
|
|
27
|
-
scope.setTag('method', ctx.method);
|
|
28
|
-
});
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
},
|
|
33
|
-
});
|