@depup/elastic-apm-node 4.15.0-depup.0

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.
Files changed (154) hide show
  1. package/LICENSE +26 -0
  2. package/NOTICE.md +442 -0
  3. package/README.md +48 -0
  4. package/changes.json +78 -0
  5. package/index.d.ts +398 -0
  6. package/index.js +11 -0
  7. package/lib/InflightEventSet.js +53 -0
  8. package/lib/activation-method.js +119 -0
  9. package/lib/agent.js +941 -0
  10. package/lib/apm-client/apm-client.js +313 -0
  11. package/lib/apm-client/http-apm-client/CHANGELOG.md +271 -0
  12. package/lib/apm-client/http-apm-client/README.md +485 -0
  13. package/lib/apm-client/http-apm-client/central-config.js +41 -0
  14. package/lib/apm-client/http-apm-client/container-info.js +111 -0
  15. package/lib/apm-client/http-apm-client/detect-hostname.js +96 -0
  16. package/lib/apm-client/http-apm-client/index.js +1975 -0
  17. package/lib/apm-client/http-apm-client/logging.js +31 -0
  18. package/lib/apm-client/http-apm-client/ndjson.js +20 -0
  19. package/lib/apm-client/http-apm-client/truncate.js +434 -0
  20. package/lib/apm-client/noop-apm-client.js +73 -0
  21. package/lib/async-hooks-polyfill.js +58 -0
  22. package/lib/cloud-metadata/aws.js +175 -0
  23. package/lib/cloud-metadata/azure.js +123 -0
  24. package/lib/cloud-metadata/callback-coordination.js +159 -0
  25. package/lib/cloud-metadata/gcp.js +133 -0
  26. package/lib/cloud-metadata/index.js +175 -0
  27. package/lib/config/config.js +458 -0
  28. package/lib/config/normalizers.js +701 -0
  29. package/lib/config/schema.js +1007 -0
  30. package/lib/constants.js +35 -0
  31. package/lib/errors.js +303 -0
  32. package/lib/filters/sanitize-field-names.js +69 -0
  33. package/lib/http-request.js +249 -0
  34. package/lib/instrumentation/azure-functions.js +519 -0
  35. package/lib/instrumentation/context.js +56 -0
  36. package/lib/instrumentation/dropped-spans-stats.js +112 -0
  37. package/lib/instrumentation/elasticsearch-shared.js +63 -0
  38. package/lib/instrumentation/express-utils.js +91 -0
  39. package/lib/instrumentation/generic-span.js +322 -0
  40. package/lib/instrumentation/http-shared.js +424 -0
  41. package/lib/instrumentation/ids.js +39 -0
  42. package/lib/instrumentation/index.js +1127 -0
  43. package/lib/instrumentation/modules/@apollo/server.js +30 -0
  44. package/lib/instrumentation/modules/@aws-sdk/client-dynamodb.js +143 -0
  45. package/lib/instrumentation/modules/@aws-sdk/client-s3.js +230 -0
  46. package/lib/instrumentation/modules/@aws-sdk/client-sns.js +197 -0
  47. package/lib/instrumentation/modules/@aws-sdk/client-sqs.js +336 -0
  48. package/lib/instrumentation/modules/@elastic/elasticsearch.js +343 -0
  49. package/lib/instrumentation/modules/@hapi/hapi.js +221 -0
  50. package/lib/instrumentation/modules/@opentelemetry/api.js +86 -0
  51. package/lib/instrumentation/modules/@opentelemetry/sdk-metrics.js +79 -0
  52. package/lib/instrumentation/modules/@redis/client/dist/lib/client/commands-queue.js +178 -0
  53. package/lib/instrumentation/modules/@redis/client/dist/lib/client/index.js +49 -0
  54. package/lib/instrumentation/modules/@smithy/smithy-client.js +198 -0
  55. package/lib/instrumentation/modules/_lambda-handler.js +40 -0
  56. package/lib/instrumentation/modules/apollo-server-core.js +49 -0
  57. package/lib/instrumentation/modules/aws-sdk/dynamodb.js +155 -0
  58. package/lib/instrumentation/modules/aws-sdk/s3.js +184 -0
  59. package/lib/instrumentation/modules/aws-sdk/sns.js +232 -0
  60. package/lib/instrumentation/modules/aws-sdk/sqs.js +361 -0
  61. package/lib/instrumentation/modules/aws-sdk.js +76 -0
  62. package/lib/instrumentation/modules/bluebird.js +93 -0
  63. package/lib/instrumentation/modules/cassandra-driver.js +280 -0
  64. package/lib/instrumentation/modules/elasticsearch.js +191 -0
  65. package/lib/instrumentation/modules/express-graphql.js +66 -0
  66. package/lib/instrumentation/modules/express-queue.js +28 -0
  67. package/lib/instrumentation/modules/express.js +162 -0
  68. package/lib/instrumentation/modules/fastify.js +172 -0
  69. package/lib/instrumentation/modules/finalhandler.js +41 -0
  70. package/lib/instrumentation/modules/generic-pool.js +85 -0
  71. package/lib/instrumentation/modules/graphql.js +256 -0
  72. package/lib/instrumentation/modules/handlebars.js +22 -0
  73. package/lib/instrumentation/modules/http.js +112 -0
  74. package/lib/instrumentation/modules/http2.js +320 -0
  75. package/lib/instrumentation/modules/https.js +68 -0
  76. package/lib/instrumentation/modules/ioredis.js +94 -0
  77. package/lib/instrumentation/modules/jade.js +18 -0
  78. package/lib/instrumentation/modules/kafkajs.js +476 -0
  79. package/lib/instrumentation/modules/knex.js +91 -0
  80. package/lib/instrumentation/modules/koa-router.js +74 -0
  81. package/lib/instrumentation/modules/koa.js +15 -0
  82. package/lib/instrumentation/modules/memcached.js +99 -0
  83. package/lib/instrumentation/modules/mimic-response.js +45 -0
  84. package/lib/instrumentation/modules/mongodb/lib/cmap/connection_pool.js +40 -0
  85. package/lib/instrumentation/modules/mongodb-core.js +206 -0
  86. package/lib/instrumentation/modules/mongodb.js +259 -0
  87. package/lib/instrumentation/modules/mysql.js +200 -0
  88. package/lib/instrumentation/modules/mysql2.js +140 -0
  89. package/lib/instrumentation/modules/pg.js +148 -0
  90. package/lib/instrumentation/modules/pug.js +18 -0
  91. package/lib/instrumentation/modules/redis.js +176 -0
  92. package/lib/instrumentation/modules/restify.js +52 -0
  93. package/lib/instrumentation/modules/tedious.js +159 -0
  94. package/lib/instrumentation/modules/undici.js +270 -0
  95. package/lib/instrumentation/modules/ws.js +59 -0
  96. package/lib/instrumentation/noop-transaction.js +81 -0
  97. package/lib/instrumentation/run-context/AbstractRunContextManager.js +215 -0
  98. package/lib/instrumentation/run-context/AsyncHooksRunContextManager.js +106 -0
  99. package/lib/instrumentation/run-context/AsyncLocalStorageRunContextManager.js +73 -0
  100. package/lib/instrumentation/run-context/BasicRunContextManager.js +82 -0
  101. package/lib/instrumentation/run-context/RunContext.js +151 -0
  102. package/lib/instrumentation/run-context/index.js +23 -0
  103. package/lib/instrumentation/shimmer.js +123 -0
  104. package/lib/instrumentation/span-compression.js +239 -0
  105. package/lib/instrumentation/span.js +621 -0
  106. package/lib/instrumentation/template-shared.js +43 -0
  107. package/lib/instrumentation/timer.js +84 -0
  108. package/lib/instrumentation/transaction.js +571 -0
  109. package/lib/lambda.js +992 -0
  110. package/lib/load-source-map.js +100 -0
  111. package/lib/logging.js +212 -0
  112. package/lib/metrics/index.js +92 -0
  113. package/lib/metrics/platforms/generic/index.js +40 -0
  114. package/lib/metrics/platforms/generic/process-cpu.js +22 -0
  115. package/lib/metrics/platforms/generic/process-top.js +157 -0
  116. package/lib/metrics/platforms/generic/stats.js +34 -0
  117. package/lib/metrics/platforms/generic/system-cpu.js +51 -0
  118. package/lib/metrics/platforms/linux/index.js +19 -0
  119. package/lib/metrics/platforms/linux/stats.js +213 -0
  120. package/lib/metrics/queue.js +90 -0
  121. package/lib/metrics/registry.js +52 -0
  122. package/lib/metrics/reporter.js +119 -0
  123. package/lib/metrics/runtime.js +77 -0
  124. package/lib/middleware/connect.js +16 -0
  125. package/lib/opentelemetry-bridge/OTelBridgeNonRecordingSpan.js +150 -0
  126. package/lib/opentelemetry-bridge/OTelBridgeRunContext.js +124 -0
  127. package/lib/opentelemetry-bridge/OTelContextManager.js +82 -0
  128. package/lib/opentelemetry-bridge/OTelSpan.js +344 -0
  129. package/lib/opentelemetry-bridge/OTelTracer.js +201 -0
  130. package/lib/opentelemetry-bridge/OTelTracerProvider.js +25 -0
  131. package/lib/opentelemetry-bridge/README.md +244 -0
  132. package/lib/opentelemetry-bridge/index.js +15 -0
  133. package/lib/opentelemetry-bridge/oblog.js +23 -0
  134. package/lib/opentelemetry-bridge/opentelemetry-core-mini/README.md +3 -0
  135. package/lib/opentelemetry-bridge/opentelemetry-core-mini/internal/validators.js +52 -0
  136. package/lib/opentelemetry-bridge/opentelemetry-core-mini/trace/TraceState.js +109 -0
  137. package/lib/opentelemetry-bridge/otelutils.js +99 -0
  138. package/lib/opentelemetry-bridge/setup.js +76 -0
  139. package/lib/opentelemetry-metrics/ElasticApmMetricExporter.js +285 -0
  140. package/lib/opentelemetry-metrics/index.js +50 -0
  141. package/lib/parsers.js +225 -0
  142. package/lib/propwrap.js +147 -0
  143. package/lib/stacktraces.js +537 -0
  144. package/lib/symbols.js +15 -0
  145. package/lib/tracecontext/index.js +118 -0
  146. package/lib/tracecontext/traceparent.js +185 -0
  147. package/lib/tracecontext/tracestate.js +388 -0
  148. package/lib/wildcard-matcher.js +52 -0
  149. package/loader.mjs +7 -0
  150. package/package.json +299 -0
  151. package/start.d.ts +8 -0
  152. package/start.js +29 -0
  153. package/types/aws-lambda.d.ts +98 -0
  154. package/types/connect.d.ts +23 -0
@@ -0,0 +1,162 @@
1
+ /*
2
+ * Copyright Elasticsearch B.V. and other contributors where applicable.
3
+ * Licensed under the BSD 2-Clause License; you may not use this file except in
4
+ * compliance with the BSD 2-Clause License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ var isError = require('core-util-is').isError;
10
+ var semver = require('semver');
11
+
12
+ var shimmer = require('../shimmer');
13
+ var symbols = require('../../symbols');
14
+
15
+ module.exports = function (express, agent, { version, enabled }) {
16
+ if (!enabled) return express;
17
+
18
+ agent.setFramework({ name: 'express', version, overwrite: false });
19
+
20
+ if (!semver.satisfies(version, '>=4.0.0 <6')) {
21
+ agent.logger.debug('cannot instrument express version %s', version);
22
+ return express;
23
+ }
24
+
25
+ if (
26
+ semver.satisfies(version, '>5') &&
27
+ semver.satisfies(process.version, '<18')
28
+ ) {
29
+ agent.logger.debug(
30
+ 'express version %s not supported for node version %s, skipping express instrumentation',
31
+ version,
32
+ process.version,
33
+ );
34
+ return express;
35
+ }
36
+
37
+ // express 5 moves the router methods onto a prototype
38
+ var routerProto = semver.satisfies(version, '^5')
39
+ ? express.Router && express.Router.prototype
40
+ : express.Router;
41
+
42
+ var layerPatchedSymbol = Symbol('layer-patched');
43
+
44
+ function shouldReport(err) {
45
+ if (!agent._conf.captureExceptions) return false;
46
+ if (typeof err === 'string') return true;
47
+ if (isError(err) && !err[symbols.errorReportedSymbol]) {
48
+ err[symbols.errorReportedSymbol] = true;
49
+ return true;
50
+ }
51
+ return false;
52
+ }
53
+
54
+ function safePush(obj, prop, value) {
55
+ if (!obj[prop]) obj[prop] = [];
56
+ obj[prop].push(value);
57
+ }
58
+
59
+ function patchLayer(layer, layerPath) {
60
+ if (!layer[layerPatchedSymbol]) {
61
+ layer[layerPatchedSymbol] = true;
62
+ agent.logger.debug(
63
+ 'shimming express.Router.Layer.handle function: %s',
64
+ layer.name,
65
+ );
66
+ shimmer.wrap(layer, 'handle', function (orig) {
67
+ let handle;
68
+
69
+ if (orig.length !== 4) {
70
+ handle = function (req, res, next) {
71
+ if (!layer.route && layerPath && typeof next === 'function') {
72
+ safePush(req, symbols.expressMountStack, layerPath);
73
+ arguments[2] = function (nextArg) {
74
+ // https://github.com/expressjs/express/blob/4.18.1/lib/router/route.js#L116-L149
75
+ // The argument to an Express handler's `next()` can be:
76
+ // falsey (call the next handler), 'route' (skip handlers for
77
+ // this route), 'router' (skip handlers for this Router), or
78
+ // any other value is considered an error (it doesn't have to
79
+ // be an instance of Error). For all but the last one, Express
80
+ // will consider other routes, so we want to pop the mount
81
+ // stack.
82
+ if (!nextArg || nextArg === 'route' || nextArg === 'router') {
83
+ req[symbols.expressMountStack].pop();
84
+ }
85
+ return next.apply(this, arguments);
86
+ };
87
+ }
88
+
89
+ return orig.apply(this, arguments);
90
+ };
91
+ } else {
92
+ handle = function (err, req, res, next) {
93
+ if (shouldReport(err)) {
94
+ agent.captureError(err, { request: req });
95
+ }
96
+ return orig.apply(this, arguments);
97
+ };
98
+ }
99
+
100
+ for (const prop in orig) {
101
+ if (Object.prototype.hasOwnProperty.call(orig, prop)) {
102
+ handle[prop] = orig[prop];
103
+ }
104
+ }
105
+
106
+ return handle;
107
+ });
108
+ }
109
+ }
110
+
111
+ agent.logger.debug('shimming express.Router.use function');
112
+
113
+ shimmer.wrap(routerProto, 'route', (orig) => {
114
+ return function route(path) {
115
+ var route = orig.apply(this, arguments);
116
+ var layer = this.stack[this.stack.length - 1];
117
+ patchLayer(layer, path);
118
+ return route;
119
+ };
120
+ });
121
+
122
+ shimmer.wrap(routerProto, 'use', (orig) => {
123
+ return function use(path) {
124
+ var route = orig.apply(this, arguments);
125
+ var layer = this.stack[this.stack.length - 1];
126
+ patchLayer(layer, typeof path === 'string' && path);
127
+ return route;
128
+ };
129
+ });
130
+
131
+ agent.logger.debug('shimming express.static function');
132
+
133
+ shimmer.wrap(express, 'static', function wrapStatic(orig) {
134
+ // By the time of this writing, Express adds a `mime` property to the
135
+ // `static` function that needs to be copied to the wrapped function.
136
+ // Instead of only copying the `mime` function, let's loop over all
137
+ // properties in case new properties are added in later versions of
138
+ // Express.
139
+ for (const prop of Object.keys(orig)) {
140
+ agent.logger.debug('copying property %s from express.static', prop);
141
+ wrappedStatic[prop] = orig[prop];
142
+ }
143
+
144
+ return wrappedStatic;
145
+
146
+ function wrappedStatic() {
147
+ var origServeStatic = orig.apply(this, arguments);
148
+ return function serveStatic(req, res, next) {
149
+ req[symbols.staticFile] = true;
150
+
151
+ return origServeStatic(req, res, nextHook);
152
+
153
+ function nextHook(err) {
154
+ if (!err) req[symbols.staticFile] = false;
155
+ return next.apply(this, arguments);
156
+ }
157
+ };
158
+ }
159
+ });
160
+
161
+ return express;
162
+ };
@@ -0,0 +1,172 @@
1
+ /*
2
+ * Copyright Elasticsearch B.V. and other contributors where applicable.
3
+ * Licensed under the BSD 2-Clause License; you may not use this file except in
4
+ * compliance with the BSD 2-Clause License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ // Instrumentation of fastify.
10
+ // https://www.fastify.io/docs/latest/LTS/
11
+
12
+ const semver = require('semver');
13
+
14
+ // Return the fastify route or request method, without hitting a Fastify
15
+ // deprecation warning.
16
+ // https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP018
17
+ function fastifyRouteMethod(req) {
18
+ if (req.routeOptions) {
19
+ // `.routeOptions` was added in fastify@4.10.0.
20
+ return req.routeOptions.method;
21
+ } else {
22
+ return req.routerMethod || req.raw.method; // Fallback for fastify >3 <3.3.0
23
+ }
24
+ }
25
+
26
+ // Return the fastify route or request URL, without hitting a Fastify
27
+ // deprecation warning.
28
+ // https://fastify.dev/docs/latest/Reference/Warnings/#FSTDEP017
29
+ function fastifyRouteUrl(req, reply) {
30
+ if (req.routeOptions) {
31
+ // `.routeOptions` was added in fastify@4.10.0.
32
+ // Note that `.routeOptions.url` might be undefined for a 404 route.
33
+ return req.routeOptions.url;
34
+ } else {
35
+ return req.routerPath || reply.context.config.url; // Fallback for fastify >3 <3.3.0
36
+ }
37
+ }
38
+
39
+ module.exports = function (
40
+ modExports,
41
+ agent,
42
+ { version, enabled, isImportMod },
43
+ ) {
44
+ if (!enabled) {
45
+ return modExports;
46
+ }
47
+ if (isImportMod && !semver.satisfies(version, '>=3.5.0')) {
48
+ // https://github.com/fastify/fastify/pull/2590
49
+ agent.logger.debug(
50
+ 'ESM instrumentation of fastify requires fastify >=3.5.0, skipping instrumentation',
51
+ );
52
+ return modExports;
53
+ }
54
+
55
+ agent.setFramework({ name: 'fastify', version, overwrite: false });
56
+
57
+ agent.logger.debug('wrapping fastify build function');
58
+ const origFastify = isImportMod ? modExports.default : modExports;
59
+
60
+ var wrapper;
61
+ if (semver.gte(version, '3.0.0')) {
62
+ wrapper = wrappedFastify;
63
+ } else if (semver.gte(version, '2.0.0-rc')) {
64
+ wrapper = wrappedFastify2;
65
+ } else {
66
+ wrapper = wrappedFastify1;
67
+ }
68
+
69
+ // Assign all enumerable properties to the wrapper.
70
+ // - 'fastify' and 'default' are intentionally circular references to
71
+ // support various import/require styles. See:
72
+ // https://github.com/fastify/fastify/blob/v4.17.0/fastify.js#L814-L827
73
+ for (var prop in origFastify) {
74
+ switch (prop) {
75
+ case 'fastify':
76
+ case 'default':
77
+ wrapper[prop] = wrapper;
78
+ break;
79
+ default:
80
+ wrapper[prop] = origFastify[prop];
81
+ break;
82
+ }
83
+ }
84
+
85
+ if (isImportMod) {
86
+ modExports.fastify = wrapper;
87
+ modExports.default = wrapper;
88
+ return modExports;
89
+ } else {
90
+ return wrapper;
91
+ }
92
+
93
+ function wrappedFastify() {
94
+ const _fastify = origFastify.apply(null, arguments);
95
+
96
+ agent.logger.debug('adding onRequest hook to fastify');
97
+ _fastify.addHook('onRequest', (req, reply, next) => {
98
+ const method = fastifyRouteMethod(req);
99
+ const url = fastifyRouteUrl(req, reply);
100
+ const name = method + ' ' + url;
101
+ agent._instrumentation.setDefaultTransactionName(name);
102
+ next();
103
+ });
104
+
105
+ agent.logger.debug('adding preHandler hook to fastify');
106
+ _fastify.addHook('preHandler', (req, reply, next) => {
107
+ // Save the parsed req body to be picked up by getContextFromRequest().
108
+ req.raw.body = req.body;
109
+ next();
110
+ });
111
+
112
+ agent.logger.debug('adding onError hook to fastify');
113
+ _fastify.addHook('onError', (req, reply, err, next) => {
114
+ agent.captureError(err, { request: req.raw });
115
+ next();
116
+ });
117
+
118
+ return _fastify;
119
+ }
120
+
121
+ function wrappedFastify2() {
122
+ const _fastify = origFastify.apply(null, arguments);
123
+
124
+ agent.logger.debug('adding onRequest hook to fastify');
125
+ _fastify.addHook('onRequest', (req, reply, next) => {
126
+ const context = reply.context;
127
+ const name = req.raw.method + ' ' + context.config.url;
128
+ agent._instrumentation.setDefaultTransactionName(name);
129
+ next();
130
+ });
131
+
132
+ agent.logger.debug('adding preHandler hook to fastify');
133
+ _fastify.addHook('preHandler', (req, reply, next) => {
134
+ // Save the parsed req body to be picked up by getContextFromRequest().
135
+ req.raw.body = req.body;
136
+ next();
137
+ });
138
+
139
+ agent.logger.debug('adding onError hook to fastify');
140
+ _fastify.addHook('onError', (req, reply, err, next) => {
141
+ agent.captureError(err, { request: req.raw });
142
+ next();
143
+ });
144
+
145
+ return _fastify;
146
+ }
147
+
148
+ function wrappedFastify1() {
149
+ const _fastify = origFastify.apply(null, arguments);
150
+
151
+ agent.logger.debug('adding onRequest hook to fastify');
152
+ _fastify.addHook('onRequest', (req, reply, next) => {
153
+ const context = reply._context;
154
+ const name = req.method + ' ' + context.config.url;
155
+ agent._instrumentation.setDefaultTransactionName(name);
156
+ next();
157
+ });
158
+
159
+ agent.logger.debug('adding preHandler hook to fastify');
160
+ _fastify.addHook('preHandler', (req, reply, next) => {
161
+ // Save the parsed req body to be picked up by getContextFromRequest().
162
+ req.raw.body = req.body;
163
+ next();
164
+ });
165
+
166
+ agent.logger.warn(
167
+ 'Elastic APM cannot automaticaly capture errors on this verison of Fastify. Upgrade to version 2.0.0 or later.',
168
+ );
169
+
170
+ return _fastify;
171
+ }
172
+ };
@@ -0,0 +1,41 @@
1
+ /*
2
+ * Copyright Elasticsearch B.V. and other contributors where applicable.
3
+ * Licensed under the BSD 2-Clause License; you may not use this file except in
4
+ * compliance with the BSD 2-Clause License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ // Instrumentation of https://www.npmjs.com/package/finalhandler
10
+ // This will `apm.captureError` any error passed to the finalhandler.
11
+ //
12
+ // Both 'express' and 'connect' use finalhandler, so this effectively handles
13
+ // capturing errors from incoming request handlers for Express and Connect apps.
14
+ // The `errorReportedSymbol` symbol is used to coordinate with 'express'
15
+ // instrumentation to avoid double-reporting errors.
16
+
17
+ var isError = require('core-util-is').isError;
18
+
19
+ var symbols = require('../../symbols');
20
+
21
+ function shouldReport(err) {
22
+ if (typeof err === 'string') return true;
23
+ if (isError(err) && !err[symbols.errorReportedSymbol]) {
24
+ err[symbols.errorReportedSymbol] = true;
25
+ return true;
26
+ }
27
+ return false;
28
+ }
29
+
30
+ module.exports = function (finalhandler, agent) {
31
+ return function wrappedFinalhandler(req, res, options) {
32
+ var final = finalhandler.apply(this, arguments);
33
+
34
+ return function (err) {
35
+ if (shouldReport(err)) {
36
+ agent.captureError(err, { request: req });
37
+ }
38
+ return final.apply(this, arguments);
39
+ };
40
+ };
41
+ };
@@ -0,0 +1,85 @@
1
+ /*
2
+ * Copyright Elasticsearch B.V. and other contributors where applicable.
3
+ * Licensed under the BSD 2-Clause License; you may not use this file except in
4
+ * compliance with the BSD 2-Clause License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ var semver = require('semver');
10
+
11
+ var shimmer = require('../shimmer');
12
+
13
+ module.exports = function (generic, agent, { version }) {
14
+ if (semver.satisfies(version, '^2.0.0')) {
15
+ agent.logger.debug('shimming generic-pool.Pool');
16
+ shimmer.wrap(generic, 'Pool', function (orig) {
17
+ return function wrappedPool() {
18
+ var trans = agent._instrumentation.currTransaction();
19
+ var id = trans && trans.id;
20
+ agent.logger.debug('intercepted call to generic-pool.Pool %o', { id });
21
+
22
+ var pool;
23
+ if (this instanceof generic.Pool) {
24
+ var args = [].slice.call(arguments);
25
+ args.unshift(null);
26
+ pool = new (Function.prototype.bind.apply(orig, args))();
27
+ } else {
28
+ pool = orig.apply(this, arguments);
29
+ }
30
+
31
+ shimmer.wrap(pool, 'acquire', function (orig) {
32
+ return function wrappedAcquire() {
33
+ var trans = agent._instrumentation.currTransaction();
34
+ var id = trans && trans.id;
35
+ agent.logger.debug('intercepted call to pool.acquire %o', { id });
36
+
37
+ var cb = arguments[0];
38
+ if (typeof cb === 'function') {
39
+ arguments[0] = agent._instrumentation.bindFunction(cb);
40
+ }
41
+
42
+ return orig.apply(this, arguments);
43
+ };
44
+ });
45
+
46
+ return pool;
47
+ };
48
+ });
49
+ } else if (semver.satisfies(version, '^3.1.0') && generic.PriorityQueue) {
50
+ // A work-around as an alternative patching the returned promise from the
51
+ // acquire function, we instead patch its resolve and reject functions.
52
+ //
53
+ // We can do that because they are exposed to the PriorityQueue when
54
+ // enqueuing a ResourceRequest:
55
+ //
56
+ // https://github.com/coopernurse/node-pool/blob/58c275c5146977192165f679e86950396be1b9f1/lib/Pool.js#L404
57
+ agent.logger.debug('shimming generic-pool.PriorityQueue.prototype.enqueue');
58
+ shimmer.wrap(generic.PriorityQueue.prototype, 'enqueue', function (orig) {
59
+ return function wrappedEnqueue() {
60
+ var trans = agent._instrumentation.currTransaction();
61
+ var id = trans && trans.id;
62
+ agent.logger.debug(
63
+ 'intercepted call to generic-pool.PriorityQueue.prototype.enqueue %o',
64
+ { id },
65
+ );
66
+
67
+ var obj = arguments[0];
68
+ // Expect obj to of type Deferred
69
+ if (obj._resolve && obj._reject) {
70
+ obj._resolve = agent._instrumentation.bindFunction(obj._resolve);
71
+ obj._reject = agent._instrumentation.bindFunction(obj._reject);
72
+ }
73
+
74
+ return orig.apply(this, arguments);
75
+ };
76
+ });
77
+ } else {
78
+ agent.logger.debug(
79
+ 'generic-pool version %s not supported - aborting...',
80
+ version,
81
+ );
82
+ }
83
+
84
+ return generic;
85
+ };