@redthreadlabs/tracelog 1.4.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 (127) hide show
  1. package/LICENSE +26 -0
  2. package/README.md +126 -0
  3. package/index.d.ts +464 -0
  4. package/index.js +11 -0
  5. package/lib/InflightEventSet.js +53 -0
  6. package/lib/activation-method.js +97 -0
  7. package/lib/agent.js +1226 -0
  8. package/lib/apm-client/apm-client.js +107 -0
  9. package/lib/apm-client/channel-writer.js +334 -0
  10. package/lib/apm-client/jsonl-file-client.js +241 -0
  11. package/lib/apm-client/ndjson.js +20 -0
  12. package/lib/apm-client/noop-apm-client.js +79 -0
  13. package/lib/apm-client/s3-uploader.js +308 -0
  14. package/lib/apm-client/truncate.js +507 -0
  15. package/lib/async-hooks-polyfill.js +58 -0
  16. package/lib/cloud-metadata/aws.js +175 -0
  17. package/lib/cloud-metadata/azure.js +123 -0
  18. package/lib/cloud-metadata/callback-coordination.js +159 -0
  19. package/lib/cloud-metadata/gcp.js +133 -0
  20. package/lib/cloud-metadata/index.js +175 -0
  21. package/lib/config/config.js +431 -0
  22. package/lib/config/normalizers.js +649 -0
  23. package/lib/config/schema.js +946 -0
  24. package/lib/constants.js +35 -0
  25. package/lib/errors.js +303 -0
  26. package/lib/filters/sanitize-field-names.js +69 -0
  27. package/lib/http-request.js +249 -0
  28. package/lib/instrumentation/context.js +56 -0
  29. package/lib/instrumentation/dropped-spans-stats.js +112 -0
  30. package/lib/instrumentation/elasticsearch-shared.js +63 -0
  31. package/lib/instrumentation/express-utils.js +91 -0
  32. package/lib/instrumentation/generic-span.js +322 -0
  33. package/lib/instrumentation/http-shared.js +424 -0
  34. package/lib/instrumentation/ids.js +39 -0
  35. package/lib/instrumentation/index.js +1078 -0
  36. package/lib/instrumentation/modules/@apollo/server.js +39 -0
  37. package/lib/instrumentation/modules/@aws-sdk/client-dynamodb.js +143 -0
  38. package/lib/instrumentation/modules/@aws-sdk/client-s3.js +230 -0
  39. package/lib/instrumentation/modules/@aws-sdk/client-sns.js +197 -0
  40. package/lib/instrumentation/modules/@aws-sdk/client-sqs.js +336 -0
  41. package/lib/instrumentation/modules/@elastic/elasticsearch.js +343 -0
  42. package/lib/instrumentation/modules/@hapi/hapi.js +221 -0
  43. package/lib/instrumentation/modules/@redis/client/dist/lib/client/commands-queue.js +178 -0
  44. package/lib/instrumentation/modules/@redis/client/dist/lib/client/index.js +49 -0
  45. package/lib/instrumentation/modules/@smithy/smithy-client.js +198 -0
  46. package/lib/instrumentation/modules/apollo-server-core.js +49 -0
  47. package/lib/instrumentation/modules/aws-sdk/dynamodb.js +155 -0
  48. package/lib/instrumentation/modules/aws-sdk/s3.js +184 -0
  49. package/lib/instrumentation/modules/aws-sdk/sns.js +232 -0
  50. package/lib/instrumentation/modules/aws-sdk/sqs.js +361 -0
  51. package/lib/instrumentation/modules/aws-sdk.js +76 -0
  52. package/lib/instrumentation/modules/bluebird.js +93 -0
  53. package/lib/instrumentation/modules/cassandra-driver.js +280 -0
  54. package/lib/instrumentation/modules/elasticsearch.js +200 -0
  55. package/lib/instrumentation/modules/express-graphql.js +66 -0
  56. package/lib/instrumentation/modules/express-queue.js +28 -0
  57. package/lib/instrumentation/modules/express.js +162 -0
  58. package/lib/instrumentation/modules/fastify.js +179 -0
  59. package/lib/instrumentation/modules/finalhandler.js +41 -0
  60. package/lib/instrumentation/modules/generic-pool.js +85 -0
  61. package/lib/instrumentation/modules/graphql.js +256 -0
  62. package/lib/instrumentation/modules/handlebars.js +33 -0
  63. package/lib/instrumentation/modules/http.js +112 -0
  64. package/lib/instrumentation/modules/http2.js +320 -0
  65. package/lib/instrumentation/modules/https.js +68 -0
  66. package/lib/instrumentation/modules/ioredis.js +94 -0
  67. package/lib/instrumentation/modules/jade.js +29 -0
  68. package/lib/instrumentation/modules/kafkajs.js +476 -0
  69. package/lib/instrumentation/modules/knex.js +91 -0
  70. package/lib/instrumentation/modules/koa-router.js +74 -0
  71. package/lib/instrumentation/modules/koa.js +15 -0
  72. package/lib/instrumentation/modules/memcached.js +100 -0
  73. package/lib/instrumentation/modules/mimic-response.js +45 -0
  74. package/lib/instrumentation/modules/mongodb/lib/cmap/connection_pool.js +40 -0
  75. package/lib/instrumentation/modules/mongodb-core.js +206 -0
  76. package/lib/instrumentation/modules/mongodb.js +259 -0
  77. package/lib/instrumentation/modules/mysql.js +200 -0
  78. package/lib/instrumentation/modules/mysql2.js +140 -0
  79. package/lib/instrumentation/modules/pg.js +148 -0
  80. package/lib/instrumentation/modules/pug.js +29 -0
  81. package/lib/instrumentation/modules/redis.js +176 -0
  82. package/lib/instrumentation/modules/restify.js +52 -0
  83. package/lib/instrumentation/modules/tedious.js +159 -0
  84. package/lib/instrumentation/modules/undici.js +270 -0
  85. package/lib/instrumentation/modules/ws.js +59 -0
  86. package/lib/instrumentation/noop-transaction.js +81 -0
  87. package/lib/instrumentation/run-context/AbstractRunContextManager.js +215 -0
  88. package/lib/instrumentation/run-context/AsyncHooksRunContextManager.js +106 -0
  89. package/lib/instrumentation/run-context/AsyncLocalStorageRunContextManager.js +73 -0
  90. package/lib/instrumentation/run-context/BasicRunContextManager.js +82 -0
  91. package/lib/instrumentation/run-context/RunContext.js +151 -0
  92. package/lib/instrumentation/run-context/index.js +23 -0
  93. package/lib/instrumentation/shimmer.js +123 -0
  94. package/lib/instrumentation/span-compression.js +239 -0
  95. package/lib/instrumentation/span.js +621 -0
  96. package/lib/instrumentation/template-shared.js +43 -0
  97. package/lib/instrumentation/timer.js +84 -0
  98. package/lib/instrumentation/transaction.js +571 -0
  99. package/lib/load-source-map.js +100 -0
  100. package/lib/logging.js +212 -0
  101. package/lib/metrics/index.js +92 -0
  102. package/lib/metrics/platforms/generic/index.js +40 -0
  103. package/lib/metrics/platforms/generic/process-cpu.js +22 -0
  104. package/lib/metrics/platforms/generic/process-top.js +157 -0
  105. package/lib/metrics/platforms/generic/stats.js +34 -0
  106. package/lib/metrics/platforms/generic/system-cpu.js +51 -0
  107. package/lib/metrics/platforms/linux/index.js +19 -0
  108. package/lib/metrics/platforms/linux/stats.js +213 -0
  109. package/lib/metrics/queue.js +90 -0
  110. package/lib/metrics/registry.js +52 -0
  111. package/lib/metrics/reporter.js +119 -0
  112. package/lib/metrics/runtime.js +77 -0
  113. package/lib/middleware/connect.js +16 -0
  114. package/lib/parsers.js +225 -0
  115. package/lib/propwrap.js +147 -0
  116. package/lib/stacktraces.js +537 -0
  117. package/lib/symbols.js +15 -0
  118. package/lib/tracecontext/index.js +115 -0
  119. package/lib/tracecontext/traceparent.js +185 -0
  120. package/lib/tracecontext/tracestate.js +388 -0
  121. package/lib/wildcard-matcher.js +52 -0
  122. package/loader.mjs +7 -0
  123. package/package.json +98 -0
  124. package/start.d.ts +8 -0
  125. package/start.js +29 -0
  126. package/types/aws-lambda.d.ts +98 -0
  127. package/types/connect.d.ts +23 -0
@@ -0,0 +1,320 @@
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 eos = require('end-of-stream');
10
+
11
+ var shimmer = require('../shimmer');
12
+ var symbols = require('../../symbols');
13
+ var { parseUrl } = require('../../parsers');
14
+ var { getHTTPDestination } = require('../context');
15
+
16
+ // Return true iff this is an HTTP 1.0 or 1.1 request.
17
+ //
18
+ // @param {http2.Http2ServerRequest} req
19
+ function reqIsHTTP1(req) {
20
+ return (
21
+ req &&
22
+ typeof req.httpVersion === 'string' &&
23
+ req.httpVersion.startsWith('1.')
24
+ );
25
+ }
26
+
27
+ module.exports = function (http2, agent, { enabled }) {
28
+ if (agent._conf.instrumentIncomingHTTPRequests) {
29
+ agent.logger.debug('shimming http2.createServer function');
30
+ shimmer.wrap(http2, 'createServer', wrapCreateServer);
31
+ shimmer.wrap(http2, 'createSecureServer', wrapCreateServer);
32
+ }
33
+
34
+ if (!enabled) return http2;
35
+ var ins = agent._instrumentation;
36
+ agent.logger.debug('shimming http2.connect function');
37
+ shimmer.wrap(http2, 'connect', wrapConnect);
38
+
39
+ return http2;
40
+
41
+ // The `createServer` function will unpatch itself after patching
42
+ // the first server prototype it patches.
43
+ function wrapCreateServer(original) {
44
+ return function wrappedCreateServer(options, handler) {
45
+ var server = original.apply(this, arguments);
46
+ shimmer.wrap(server.constructor.prototype, 'emit', wrapEmit);
47
+ wrappedCreateServer[symbols.unwrap]();
48
+ return server;
49
+ };
50
+ }
51
+
52
+ function wrapEmit(original) {
53
+ var patched = false;
54
+ return function wrappedEmit(event, stream, headers) {
55
+ if (event === 'stream') {
56
+ if (!patched) {
57
+ patched = true;
58
+ var proto = stream.constructor.prototype;
59
+ shimmer.wrap(proto, 'pushStream', wrapPushStream);
60
+ shimmer.wrap(proto, 'respondWithFile', wrapRespondWith);
61
+ shimmer.wrap(proto, 'respondWithFD', wrapRespondWith);
62
+ shimmer.wrap(proto, 'respond', wrapHeaders);
63
+ shimmer.wrap(proto, 'end', wrapEnd);
64
+ }
65
+
66
+ agent.logger.debug(
67
+ 'intercepted stream event call to http2.Server.prototype.emit',
68
+ );
69
+
70
+ const traceparent =
71
+ headers.traceparent || headers['elastic-apm-traceparent'];
72
+ const tracestate = headers.tracestate;
73
+ const trans = agent.startTransaction(null, 'request', {
74
+ childOf: traceparent,
75
+ tracestate,
76
+ });
77
+ // `trans.req` and `trans.res` are fake representations of Node.js's
78
+ // core `http.IncomingMessage` and `http.ServerResponse` objects,
79
+ // sufficient for `parsers.getContextFromRequest()` and
80
+ // `parsers.getContextFromResponse()`, respectively.
81
+ // `remoteAddress` is fetched now, rather than at stream end, because
82
+ // this `stream.session.socket` is a proxy object that can throw
83
+ // `ERR_HTTP2_SOCKET_UNBOUND` if the Http2Session has been destroyed.
84
+ trans.req = {
85
+ headers,
86
+ socket: {
87
+ remoteAddress: stream.session.socket.remoteAddress,
88
+ },
89
+ method: headers[':method'],
90
+ url: headers[':path'],
91
+ httpVersion: '2.0',
92
+ };
93
+ trans.res = {
94
+ statusCode: 200,
95
+ headersSent: false,
96
+ finished: false,
97
+ headers: null,
98
+ };
99
+ ins.bindEmitter(stream);
100
+
101
+ eos(stream, function () {
102
+ trans.end();
103
+ });
104
+ } else if (event === 'request' && reqIsHTTP1(stream)) {
105
+ // http2.createSecureServer() supports a `allowHTTP1: true` option.
106
+ // When true, an incoming client request that supports HTTP/1.x but not
107
+ // HTTP/2 will be allowed. It will result in a 'request' event being
108
+ // emitted. We wrap that here.
109
+ //
110
+ // Note that, a HTTP/2 request results in a 'stream' event (wrapped
111
+ // above) *and* a 'request' event. We do not want to wrap the
112
+ // compatibility 'request' event in this case. Hence the `reqIsHTTP1`
113
+ // guard.
114
+ const req = stream;
115
+ const res = headers;
116
+
117
+ agent.logger.debug(
118
+ 'intercepted request event call to http2.Server.prototype.emit for %s',
119
+ req.url,
120
+ );
121
+
122
+ const traceparent =
123
+ req.headers.traceparent || req.headers['elastic-apm-traceparent'];
124
+ const tracestate = req.headers.tracestate;
125
+ const trans = agent.startTransaction(null, null, {
126
+ childOf: traceparent,
127
+ tracestate,
128
+ });
129
+ trans.type = 'request';
130
+ trans.req = req;
131
+ trans.res = res;
132
+
133
+ ins.bindEmitter(req);
134
+ ins.bindEmitter(res);
135
+
136
+ eos(res, function (err) {
137
+ if (trans.ended) return;
138
+ if (!err) {
139
+ trans.end();
140
+ return;
141
+ }
142
+
143
+ if (agent._conf.errorOnAbortedRequests) {
144
+ var duration = trans._timer.elapsed();
145
+ if (duration > agent._conf.abortedErrorThreshold * 1000) {
146
+ agent.captureError(
147
+ 'Socket closed with active HTTP request (>' +
148
+ agent._conf.abortedErrorThreshold +
149
+ ' sec)',
150
+ {
151
+ request: req,
152
+ extra: { abortTime: duration },
153
+ },
154
+ );
155
+ }
156
+ }
157
+
158
+ // Handle case where res.end is called after an error occurred on the
159
+ // stream (e.g. if the underlying socket was prematurely closed)
160
+ const end = res.end;
161
+ res.end = function () {
162
+ const result = end.apply(this, arguments);
163
+ trans.end();
164
+ return result;
165
+ };
166
+ });
167
+ }
168
+
169
+ return original.apply(this, arguments);
170
+ };
171
+ }
172
+
173
+ function updateHeaders(headers) {
174
+ var trans = agent._instrumentation.currTransaction();
175
+ if (trans && !trans.ended) {
176
+ var status = headers[':status'] || 200;
177
+ trans.result = 'HTTP ' + status.toString()[0] + 'xx';
178
+ trans.res.statusCode = status;
179
+ trans._setOutcomeFromHttpStatusCode(status);
180
+ trans.res.headers = mergeHeaders(trans.res.headers, headers);
181
+ trans.res.headersSent = true;
182
+ }
183
+ }
184
+
185
+ function wrapHeaders(original) {
186
+ return function (headers) {
187
+ updateHeaders(headers);
188
+ return original.apply(this, arguments);
189
+ };
190
+ }
191
+
192
+ function wrapRespondWith(original) {
193
+ return function (_, headers) {
194
+ updateHeaders(headers);
195
+ return original.apply(this, arguments);
196
+ };
197
+ }
198
+
199
+ function wrapEnd(original) {
200
+ return function (headers) {
201
+ var trans = agent._instrumentation.currTransaction();
202
+ // `trans.res` might be removed, because before
203
+ // https://github.com/nodejs/node/pull/20084 (e.g. in node v10.0.0) the
204
+ // 'end' event could be called multiple times for the same Http2Stream,
205
+ // and the `trans.res` ref is removed when the Transaction is ended.
206
+ if (trans && trans.res) {
207
+ trans.res.finished = true;
208
+ }
209
+ return original.apply(this, arguments);
210
+ };
211
+ }
212
+
213
+ function wrapPushStream(original) {
214
+ return function wrappedPushStream(...args) {
215
+ // Note: Break the run context so that the wrapped `stream.respond` et al
216
+ // for this pushStream do not overwrite outer transaction state.
217
+ var callback = args.pop();
218
+ args.push(agent._instrumentation.bindFunctionToEmptyRunContext(callback));
219
+ return original.apply(this, args);
220
+ };
221
+ }
222
+
223
+ function mergeHeaders(source, target) {
224
+ if (source === null) return target;
225
+ var result = Object.assign({}, target);
226
+ var keys = Object.keys(source);
227
+ for (let i = 0; i < keys.length; i++) {
228
+ var key = keys[i];
229
+ if (typeof target[key] === 'undefined') {
230
+ result[key] = source[key];
231
+ } else if (Array.isArray(target[key])) {
232
+ result[key].push(source[key]);
233
+ } else {
234
+ result[key] = [source[key]].concat(target[key]);
235
+ }
236
+ }
237
+ return result;
238
+ }
239
+
240
+ function wrapConnect(orig) {
241
+ return function (host) {
242
+ const ret = orig.apply(this, arguments);
243
+ shimmer.wrap(ret, 'request', (orig) => wrapRequest(orig, host));
244
+ return ret;
245
+ };
246
+ }
247
+
248
+ function wrapRequest(orig, host) {
249
+ return function (headers) {
250
+ agent.logger.debug('intercepted call to http2.request');
251
+ var method = headers[':method'] || 'GET';
252
+ const span = ins.createSpan(null, 'external', 'http', method, {
253
+ exitSpan: true,
254
+ });
255
+
256
+ const parentRunContext = ins.currRunContext();
257
+ var parent =
258
+ span ||
259
+ parentRunContext.currSpan() ||
260
+ parentRunContext.currTransaction();
261
+ if (parent) {
262
+ const newHeaders = Object.assign({}, headers);
263
+ parent.propagateTraceContextHeaders(
264
+ newHeaders,
265
+ function (carrier, name, value) {
266
+ carrier[name] = value;
267
+ },
268
+ );
269
+ arguments[0] = newHeaders;
270
+ }
271
+ if (!span) {
272
+ return orig.apply(this, arguments);
273
+ }
274
+
275
+ const spanRunContext = parentRunContext.enterSpan(span);
276
+ var req = ins.withRunContext(spanRunContext, orig, this, ...arguments);
277
+
278
+ ins.bindEmitterToRunContext(parentRunContext, req);
279
+
280
+ var urlObj = parseUrl(headers[':path']);
281
+ var path = urlObj.pathname;
282
+ var url = host + path;
283
+ span.name = method + ' ' + host;
284
+
285
+ var statusCode;
286
+ req.on('response', (headers) => {
287
+ statusCode = headers[':status'];
288
+ });
289
+
290
+ req.on('end', () => {
291
+ agent.logger.debug('intercepted http2 client end event');
292
+
293
+ span.setHttpContext({
294
+ method,
295
+ status_code: statusCode,
296
+ url,
297
+ });
298
+ span._setOutcomeFromHttpStatusCode(statusCode);
299
+
300
+ // The `getHTTPDestination` function might throw in case an
301
+ // invalid URL is given to the `URL()` function. Until we can
302
+ // be 100% sure this doesn't happen, we better catch it here.
303
+ // For details, see:
304
+ // https://github.com/elastic/apm-agent-nodejs/issues/1769
305
+ try {
306
+ span._setDestinationContext(getHTTPDestination(url));
307
+ } catch (e) {
308
+ agent.logger.error(
309
+ 'Could not set destination context: %s',
310
+ e.message,
311
+ );
312
+ }
313
+
314
+ span.end();
315
+ });
316
+
317
+ return req;
318
+ };
319
+ }
320
+ };
@@ -0,0 +1,68 @@
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 httpShared = require('../http-shared');
12
+ var shimmer = require('../shimmer');
13
+
14
+ module.exports = function (
15
+ modExports,
16
+ agent,
17
+ { version, enabled, isImportMod },
18
+ ) {
19
+ if (agent._conf.instrumentIncomingHTTPRequests) {
20
+ agent.logger.debug('shimming https.Server.prototype.emit function');
21
+ shimmer.wrap(
22
+ modExports && modExports.Server && modExports.Server.prototype,
23
+ 'emit',
24
+ httpShared.instrumentRequest(agent, 'https'),
25
+ );
26
+ }
27
+
28
+ if (!enabled) {
29
+ return modExports;
30
+ }
31
+
32
+ // From Node.js v9.0.0 and onwards, https requests no longer just call the
33
+ // http.request function. So to correctly instrument outgoing HTTPS requests
34
+ // in all supported Node.js versions, we'll only only instrument the
35
+ // https.request function if the Node version is v9.0.0 or above.
36
+ //
37
+ // This change was introduced in:
38
+ // https://github.com/nodejs/node/commit/5118f3146643dc55e7e7bd3082d1de4d0e7d5426
39
+ if (semver.lt(version, '9.0.0')) {
40
+ // We must ensure that the `http` module is instrumented to intercept
41
+ // `http.{request,get}` that `https.{request,get}` are using.
42
+ require('http');
43
+ return modExports;
44
+ }
45
+
46
+ agent.logger.debug('shimming https.request function');
47
+ let wrapped = shimmer.wrap(
48
+ modExports,
49
+ 'request',
50
+ httpShared.traceOutgoingRequest(agent, 'https', 'request'),
51
+ );
52
+ if (isImportMod && wrapped) {
53
+ // Handle `import https from 'https'`. See comment in "http.js".
54
+ modExports.default.request = wrapped;
55
+ }
56
+
57
+ agent.logger.debug('shimming https.get function');
58
+ wrapped = shimmer.wrap(
59
+ modExports,
60
+ 'get',
61
+ httpShared.traceOutgoingRequest(agent, 'https', 'get'),
62
+ );
63
+ if (isImportMod && wrapped) {
64
+ modExports.default.get = wrapped;
65
+ }
66
+
67
+ return modExports;
68
+ };
@@ -0,0 +1,94 @@
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 the 'ioredis' package:
10
+ // https://github.com/luin/ioredis
11
+ // https://github.com/luin/ioredis/blob/master/API.md
12
+
13
+ const semver = require('semver');
14
+
15
+ const constants = require('../../constants');
16
+ const { getDBDestination } = require('../context');
17
+ const shimmer = require('../shimmer');
18
+
19
+ const TYPE = 'db';
20
+ const SUBTYPE = 'redis';
21
+ const ACTION = 'query';
22
+ const hasIoredisSpanSym = Symbol('ElasticAPMHasIoredisSpan');
23
+
24
+ module.exports = function (ioredis, agent, { version, enabled }) {
25
+ if (!enabled) {
26
+ return ioredis;
27
+ }
28
+ if (!semver.satisfies(version, '>=2.0.0 <6.0.0')) {
29
+ agent.logger.debug(
30
+ 'ioredis version %s not supported - aborting...',
31
+ version,
32
+ );
33
+ return ioredis;
34
+ }
35
+
36
+ const ins = agent._instrumentation;
37
+
38
+ agent.logger.debug('shimming ioredis.prototype.sendCommand');
39
+ shimmer.wrap(ioredis.prototype, 'sendCommand', wrapSendCommand);
40
+ return ioredis;
41
+
42
+ function wrapSendCommand(origSendCommand) {
43
+ return function wrappedSendCommand(command) {
44
+ if (!command || !command.name || !command.promise) {
45
+ // Doesn't look like an ioredis.Command, skip instrumenting.
46
+ return origSendCommand.apply(this, arguments);
47
+ }
48
+ if (command[hasIoredisSpanSym]) {
49
+ // Avoid double-instrumenting a command when ioredis *re*-calls
50
+ // sendCommand for queued commands when "ready".
51
+ return origSendCommand.apply(this, arguments);
52
+ }
53
+
54
+ agent.logger.debug(
55
+ { command: command.name },
56
+ 'intercepted call to ioredis.prototype.sendCommand',
57
+ );
58
+ const span = ins.createSpan(
59
+ command.name.toUpperCase(),
60
+ TYPE,
61
+ SUBTYPE,
62
+ ACTION,
63
+ { exitSpan: true },
64
+ );
65
+ if (!span) {
66
+ return origSendCommand.apply(this, arguments);
67
+ }
68
+
69
+ command[hasIoredisSpanSym] = true;
70
+
71
+ const options = this.options || {}; // `this` is the `Redis` client.
72
+ span._setDestinationContext(getDBDestination(options.host, options.port));
73
+ span.setDbContext({ type: 'redis' });
74
+
75
+ const spanRunContext = ins.currRunContext().enterSpan(span);
76
+ command.promise.then(
77
+ () => {
78
+ span.end();
79
+ },
80
+ ins.bindFunctionToRunContext(spanRunContext, (err) => {
81
+ span._setOutcomeFromErrorCapture(constants.OUTCOME_FAILURE);
82
+ agent.captureError(err, { skipOutcome: true });
83
+ span.end();
84
+ }),
85
+ );
86
+ return ins.withRunContext(
87
+ spanRunContext,
88
+ origSendCommand,
89
+ this,
90
+ ...arguments,
91
+ );
92
+ };
93
+ }
94
+ };
@@ -0,0 +1,29 @@
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
+ var templateShared = require('../template-shared');
13
+
14
+ module.exports = function (jade, agent, { version, enabled }) {
15
+ if (!enabled) return jade;
16
+
17
+ if (!semver.satisfies(version, '>=0.5.6 <2')) {
18
+ agent.logger.debug(
19
+ 'cannot instrument jade version %s, skipping jade instrumentation',
20
+ version,
21
+ );
22
+ return jade;
23
+ }
24
+
25
+ agent.logger.debug('shimming jade.compile');
26
+ shimmer.wrap(jade, 'compile', templateShared.wrapCompile(agent, 'jade'));
27
+
28
+ return jade;
29
+ };