forked from Meteor-Community-Packages/meteor-elastic-apm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteor-elastic-apm.js
75 lines (63 loc) · 2.32 KB
/
meteor-elastic-apm.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* eslint-disable no-undef */
const shimmer = require('shimmer');
const Fibers = require('fibers');
const Agent = require('elastic-apm-node');
const { Session, Subscription, MongoCursor } = require('./meteorx');
const instrumentMethods = require('./instrumenting/methods');
const instrumentHttpIn = require('./instrumenting/http-in');
const instrumentHttpOut = require('./instrumenting/http-out');
const instrumentSession = require('./instrumenting/session');
const instrumentSubscription = require('./instrumenting/subscription');
const instrumentAsync = require('./instrumenting/async');
const instrumentDB = require('./instrumenting/db');
const startMetrics = require('./metrics');
const hackDB = require('./hacks');
const [framework, version] = Meteor.release.split('@');
Agent.setFramework({
name: framework,
version,
override: true
});
shimmer.wrap(Agent, 'start', function(startAgent) {
return function(...args) {
const config = args[0] || {};
if (config.active !== false) {
try {
const disabled = new Set(config.disableMeteorInstrumentations);
// Must be called before any other route is registered on WebApp.
instrumentHttpIn(Agent, WebApp);
instrumentHttpOut(Agent);
Meteor.startup(() => {
try {
startAgent.apply(Agent, args);
Object.entries({
methods: () => instrumentMethods(Agent, Meteor),
session: () => instrumentSession(Agent, Session),
subscription: () => instrumentSubscription(Agent, Subscription),
async: () => instrumentAsync(Agent, Fibers),
db: () => {
hackDB();
instrumentDB(Agent, Meteor, MongoCursor);
},
metrics: () => startMetrics(Agent)
}).forEach(([name, fn]) => {
if (!disabled.has(name)) {
fn();
}
});
Agent.logger.info('meteor-elastic-apm completed instrumenting');
} catch (e) {
Agent.logger.error('Could not start meteor-elastic-apm');
throw e;
}
});
} catch (e) {
Agent.logger.error('Could not start meteor-elastic-apm');
throw e;
}
} else {
Agent.logger.warn('meteor-elastic-apm is not active');
}
};
});
module.exports = Agent;