1 | 'use strict'
|
2 | var debug = require('./utils/debug')()
|
3 | var Agent = require('./agent')
|
4 | var Instrumentation = require('./instrumentations')
|
5 | var ConfigReader = require('./utils/configReader')
|
6 | var format = require('util').format
|
7 |
|
8 | var INSTRUMENTED_LIBS = {
|
9 | 'http': './trace-instrumentation-http',
|
10 | 'https': './trace-instrumentation-https',
|
11 | 'mongoose': './trace-instrumentation-mongoose',
|
12 | 'mongodb': './trace-instrumentation-mongodb',
|
13 | 'redis': './trace-instrumentation-redis',
|
14 | 'ioredis': './trace-instrumentation-ioredis',
|
15 | 'mysql': './trace-instrumentation-mysql',
|
16 | 'koa': './trace-instrumentation-koa',
|
17 | 'express': './trace-instrumentation-express',
|
18 | 'pg': './trace-instrumentation-pg',
|
19 | 'amqplib': './trace-instrumentation-amqplib'
|
20 | }
|
21 |
|
22 | var traceNoop = {
|
23 | report: function () {},
|
24 | reportError: function () {},
|
25 | getTransactionId: function () {},
|
26 | sendMemorySnapshot: function () {},
|
27 | recordMetric: function () {},
|
28 | incrementMetric: function () {},
|
29 | stop: function (cb) {
|
30 | if (typeof cb === 'function') {
|
31 | cb()
|
32 | }
|
33 | }
|
34 | }
|
35 |
|
36 | function Trace (config) {
|
37 | debug.info('Trace',
|
38 | format('constructing with configuration %s', config))
|
39 | this._agent = Agent.create({
|
40 | config: config
|
41 | })
|
42 |
|
43 | this._instrumentation = Instrumentation.create({
|
44 | agent: this._agent,
|
45 | instrumentations: INSTRUMENTED_LIBS,
|
46 | config: config
|
47 | })
|
48 |
|
49 | this._agent.start()
|
50 | }
|
51 |
|
52 | Trace.prototype.report = function (name) {
|
53 | var data = Array.prototype.slice.call(arguments, 1)
|
54 | if (typeof name !== 'string' || name === '') {
|
55 | throw new Error('First parameter invalid, should be a string')
|
56 | }
|
57 | this._agent.tracer.collector.userSentEvent(name, data)
|
58 | }
|
59 |
|
60 | Trace.prototype.reportError = function (errorName, error) {
|
61 | if (typeof errorName !== 'string' || errorName === '') {
|
62 | throw new Error('First parameter invalid, should be a string')
|
63 | }
|
64 | this._agent.tracer.collector.userSentError(errorName, error)
|
65 | }
|
66 |
|
67 | Trace.prototype.getTransactionId = function () {
|
68 | var transactionId = this._agent.tracer.collector.getTransactionId()
|
69 | return transactionId
|
70 | }
|
71 |
|
72 | Trace.prototype.sendMemorySnapshot = function () {
|
73 | this._agent.memoryProfiler.sendSnapshot()
|
74 | }
|
75 |
|
76 | Trace.prototype.incrementMetric = function (name, amount) {
|
77 | this._agent.customMetrics.increment(name, amount)
|
78 | }
|
79 |
|
80 | Trace.prototype.recordMetric = function (name, value) {
|
81 | this._agent.customMetrics.record(name, value)
|
82 | }
|
83 |
|
84 | Trace.prototype.stop = function (cb) {
|
85 | this._agent.stop(cb)
|
86 | }
|
87 |
|
88 | module.exports.Trace = Trace
|
89 | module.exports.noop = traceNoop
|
90 |
|
91 | module.exports.create = function () {
|
92 | if (process.env.NODE_ENV === 'test') {
|
93 | debug.error('create',
|
94 | "Trace disabled. Reason: NODE_ENV is set to 'test'")
|
95 | return module.exports.noop
|
96 | }
|
97 |
|
98 |
|
99 | if (require.cache.__NR_cache) {
|
100 | debug.error('create',
|
101 | 'Trace disabled. Reason: bailing out because New Relic is loaded')
|
102 | return module.exports.noop
|
103 | }
|
104 |
|
105 | try {
|
106 | var configReader = ConfigReader.create()
|
107 | var config = configReader.getConfig()
|
108 |
|
109 | return new module.exports.Trace(config)
|
110 | } catch (ex) {
|
111 | debug.error('create',
|
112 | format('Trace disabled. Reason: %s', ex.stack))
|
113 | return module.exports.noop
|
114 | }
|
115 | }
|