-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (44 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const assert = require('assert');
const Plugin = require('hato/plugins/base');
const { constants: { Scopes: { API } } } = require('hato');
module.exports = class extends Plugin {
constructor(options = {}) {
assert(options.Sentry, '[hato:sentry] `Sentry` option property must be defined');
super('sentry');
this.Sentry = options.Sentry;
}
init() {
this.scopes[API] = this.reportError();
}
reportError() {
const plugin = this;
return (constructor) => class extends constructor {
consume(queue, fn, options) {
return super.consume(queue, fn, options)
.on('error', (err, msg) => {
plugin.Sentry.withScope((scope) => {
scope.setLevel('error');
try {
plugin.addData(scope, msg);
} finally {
plugin.Sentry.captureException(err);
}
});
});
}
};
}
addData(scope, msg) {
const {
fields,
properties
} = msg;
scope.setTag('exchange', fields.exchange);
scope.setTag('routing_key', fields.routingKey);
scope.setTag('redelivered', fields.redelivered);
scope.setTag('consumer_tag', fields.consumerTag);
const content = Buffer.isBuffer(msg.content) ?
msg.content.toString() : msg.content;
scope.setContext('message', { fields, content, properties });
}
};