@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,256 @@
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
+ var clone = require('shallow-clone-shim');
11
+
12
+ var getPathFromRequest = require('../express-utils').getPathFromRequest;
13
+
14
+ module.exports = function (graphql, agent, { version, enabled }) {
15
+ if (!enabled) return graphql;
16
+ if (
17
+ !semver.satisfies(version, '>=0.7.0 <17') ||
18
+ !graphql.Kind ||
19
+ typeof graphql.Source !== 'function' ||
20
+ typeof graphql.parse !== 'function' ||
21
+ typeof graphql.validate !== 'function' ||
22
+ typeof graphql.execute !== 'function'
23
+ ) {
24
+ agent.logger.debug(
25
+ 'graphql version %s not supported - aborting...',
26
+ version,
27
+ );
28
+ return graphql;
29
+ }
30
+
31
+ // Over the many versions the `graphql()` and `execute()` functions have
32
+ // changed in what arguments they accept:
33
+ // - Initially they only supported positional arguments:
34
+ // function graphql(schema, requestString, rootValue, contextValue, variableValues, operationName)
35
+ // - Starting in v0.10.0 (in #356), they started accepting a second form where
36
+ // all fields were passed in an object as the first argument:
37
+ // function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver)
38
+ // and `arguments.length === 1` was used as the test to determine which.
39
+ // - Starting in v16 (in #2904), they dropped positional arguments:
40
+ // function graphql(args)
41
+ const onlySupportsPositionalArgs = semver.lt(version, '0.10.0');
42
+ const onlySupportsSingleArg = semver.gte(version, '16.0.0');
43
+
44
+ const ins = agent._instrumentation;
45
+
46
+ return clone({}, graphql, {
47
+ graphql(descriptor) {
48
+ const getter = descriptor.get;
49
+ if (getter) {
50
+ descriptor.get = function get() {
51
+ return wrapGraphql(getter());
52
+ };
53
+ }
54
+ return descriptor;
55
+ },
56
+ execute(descriptor) {
57
+ const getter = descriptor.get;
58
+ if (getter) {
59
+ descriptor.get = function get() {
60
+ return wrapExecute(getter());
61
+ };
62
+ }
63
+ return descriptor;
64
+ },
65
+ });
66
+
67
+ function wrapGraphql(orig) {
68
+ return function wrappedGraphql(args) {
69
+ agent.logger.debug('intercepted call to graphql.graphql');
70
+ const span = ins.createSpan(
71
+ 'GraphQL: Unknown Query',
72
+ 'db',
73
+ 'graphql',
74
+ 'execute',
75
+ );
76
+ if (!span) {
77
+ return orig.apply(this, arguments);
78
+ }
79
+
80
+ let schema;
81
+ let source;
82
+ let operationName;
83
+ const singleArgForm =
84
+ onlySupportsSingleArg ||
85
+ (!onlySupportsPositionalArgs && arguments.length === 1);
86
+ if (singleArgForm) {
87
+ schema = args.schema;
88
+ source = args.source;
89
+ operationName = args.operationName;
90
+ } else {
91
+ schema = arguments[0];
92
+ source = arguments[1];
93
+ operationName = arguments[5];
94
+ }
95
+
96
+ const sourceObj =
97
+ typeof source === 'string'
98
+ ? new graphql.Source(source || '', 'GraphQL request')
99
+ : source;
100
+ if (sourceObj) {
101
+ var documentAST;
102
+
103
+ try {
104
+ documentAST = graphql.parse(sourceObj);
105
+ } catch (syntaxError) {
106
+ agent.logger.debug(
107
+ 'graphql.parse(source) failed - skipping graphql query extraction',
108
+ );
109
+ }
110
+
111
+ if (documentAST) {
112
+ var validationErrors = graphql.validate(schema, documentAST);
113
+ if (validationErrors && validationErrors.length === 0) {
114
+ var queries = extractDetails(documentAST, operationName).queries;
115
+ if (queries.length > 0)
116
+ span.name = 'GraphQL: ' + queries.join(', ');
117
+ }
118
+ }
119
+ } else {
120
+ agent.logger.debug(
121
+ 'graphql.Source(query) failed - skipping graphql query extraction',
122
+ );
123
+ }
124
+
125
+ const spanRunContext = ins.currRunContext().enterSpan(span);
126
+ const p = ins.withRunContext(spanRunContext, orig, this, ...arguments);
127
+ p.then(function () {
128
+ span.end();
129
+ });
130
+ return p;
131
+ };
132
+ }
133
+
134
+ function wrapExecute(orig) {
135
+ return function wrappedExecute(args) {
136
+ agent.logger.debug('intercepted call to graphql.execute');
137
+ const span = ins.createSpan(
138
+ 'GraphQL: Unknown Query',
139
+ 'db',
140
+ 'graphql',
141
+ 'execute',
142
+ );
143
+ if (!span) {
144
+ agent.logger.debug(
145
+ 'no active transaction found - skipping graphql tracing',
146
+ );
147
+ return orig.apply(this, arguments);
148
+ }
149
+
150
+ let document;
151
+ let operationName;
152
+ const singleArgForm =
153
+ onlySupportsSingleArg ||
154
+ (!onlySupportsPositionalArgs && arguments.length === 1);
155
+ if (singleArgForm) {
156
+ document = args.document;
157
+ operationName = args.operationName;
158
+ } else {
159
+ document = arguments[1];
160
+ operationName = arguments[5];
161
+ }
162
+
163
+ var details = extractDetails(document, operationName);
164
+ var queries = details.queries;
165
+ operationName =
166
+ operationName ||
167
+ (details.operation &&
168
+ details.operation.name &&
169
+ details.operation.name.value);
170
+ if (queries.length > 0) {
171
+ span.name =
172
+ 'GraphQL: ' +
173
+ (operationName ? operationName + ' ' : '') +
174
+ queries.join(', ');
175
+ }
176
+
177
+ // `_graphqlRoute` is a boolean, set in instrumentations of other modules
178
+ // that specify 'graphql' in peerDependencies (e.g. '@apollo/server') to
179
+ // indicate that this transaction is for a GraphQL request.
180
+ const trans = span.transaction;
181
+ if (trans._graphqlRoute) {
182
+ var name =
183
+ queries.length > 0 ? queries.join(', ') : 'Unknown GraphQL query';
184
+ if (trans.req) var path = getPathFromRequest(trans.req, true);
185
+ var defaultName = name;
186
+ defaultName = path ? defaultName + ' (' + path + ')' : defaultName;
187
+ defaultName = operationName
188
+ ? operationName + ' ' + defaultName
189
+ : defaultName;
190
+ trans.setDefaultName(defaultName);
191
+ trans.type = 'graphql';
192
+ }
193
+
194
+ const spanRunContext = ins.currRunContext().enterSpan(span);
195
+ const p = ins.withRunContext(spanRunContext, orig, this, ...arguments);
196
+ if (typeof p.then === 'function') {
197
+ p.then(function () {
198
+ span.end();
199
+ });
200
+ } else {
201
+ span.end();
202
+ }
203
+ return p;
204
+ };
205
+ }
206
+
207
+ function extractDetails(document, operationName) {
208
+ var queries = [];
209
+ var operation;
210
+
211
+ if (document && Array.isArray(document.definitions)) {
212
+ document.definitions.some(function (definition) {
213
+ if (
214
+ !definition ||
215
+ definition.kind !== graphql.Kind.OPERATION_DEFINITION
216
+ )
217
+ return false;
218
+ if (!operationName && operation) return false;
219
+ if (
220
+ !operationName ||
221
+ (definition.name && definition.name.value === operationName)
222
+ ) {
223
+ operation = definition;
224
+ return true;
225
+ }
226
+ return false;
227
+ });
228
+
229
+ var selections =
230
+ operation &&
231
+ operation.selectionSet &&
232
+ operation.selectionSet.selections;
233
+ if (selections && Array.isArray(selections)) {
234
+ for (const selection of selections) {
235
+ const kind = selection.name && selection.name.kind;
236
+ if (kind === graphql.Kind.NAME) {
237
+ const queryName = selection.name.value;
238
+ if (queryName) queries.push(queryName);
239
+ }
240
+ }
241
+
242
+ queries = queries.sort(function (a, b) {
243
+ if (a > b) return 1;
244
+ else if (a < b) return -1;
245
+ return 0;
246
+ });
247
+ }
248
+ } else {
249
+ agent.logger.debug(
250
+ 'unexpected document format - skipping graphql query extraction',
251
+ );
252
+ }
253
+
254
+ return { queries, operation };
255
+ }
256
+ };
@@ -0,0 +1,33 @@
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 (handlebars, agent, { version, enabled }) {
15
+ if (!enabled) return handlebars;
16
+
17
+ if (!semver.satisfies(version, '>=1 <5')) {
18
+ agent.logger.debug(
19
+ 'cannot instrument handlebars version %s, skipping handlebars instrumentation',
20
+ version,
21
+ );
22
+ return handlebars;
23
+ }
24
+
25
+ agent.logger.debug('shimming handlebars.compile');
26
+ shimmer.wrap(
27
+ handlebars,
28
+ 'compile',
29
+ templateShared.wrapCompile(agent, 'handlebars'),
30
+ );
31
+
32
+ return handlebars;
33
+ };
@@ -0,0 +1,112 @@
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 httpShared = require('../http-shared');
10
+ var shimmer = require('../shimmer');
11
+
12
+ function getSafeHeaders(res) {
13
+ return res.getHeaders ? res.getHeaders() : res._headers;
14
+ }
15
+
16
+ module.exports = function (modExports, agent, { enabled, isImportMod }) {
17
+ if (agent._conf.instrumentIncomingHTTPRequests) {
18
+ agent.logger.debug('shimming http.Server.prototype.emit function');
19
+ shimmer.wrap(
20
+ modExports && modExports.Server && modExports.Server.prototype,
21
+ 'emit',
22
+ httpShared.instrumentRequest(agent, 'http'),
23
+ );
24
+
25
+ agent.logger.debug(
26
+ 'shimming http.ServerResponse.prototype.writeHead function',
27
+ );
28
+ shimmer.wrap(
29
+ modExports &&
30
+ modExports.ServerResponse &&
31
+ modExports.ServerResponse.prototype,
32
+ 'writeHead',
33
+ wrapWriteHead,
34
+ );
35
+ }
36
+
37
+ if (!enabled) {
38
+ return modExports;
39
+ }
40
+
41
+ agent.logger.debug('shimming http.request function');
42
+ let wrapped = shimmer.wrap(
43
+ modExports,
44
+ 'request',
45
+ httpShared.traceOutgoingRequest(agent, 'http', 'request'),
46
+ );
47
+ if (isImportMod && wrapped) {
48
+ // With IITM and `import` the above shimmer.wrap handles this usage:
49
+ // import { request } from 'http'
50
+ // To handle this usage:
51
+ // import http from 'http'
52
+ // we need to wrap `modExports.default.request` as well.
53
+ //
54
+ // Note that IITM gives us a Proxy *without* a getter, so
55
+ // `modExports.request !== wrapped`, even though we just set it.
56
+ modExports.default.request = wrapped;
57
+ }
58
+
59
+ agent.logger.debug('shimming http.get function');
60
+ wrapped = shimmer.wrap(
61
+ modExports,
62
+ 'get',
63
+ httpShared.traceOutgoingRequest(agent, 'http', 'get'),
64
+ );
65
+ if (isImportMod && wrapped) {
66
+ modExports.default.get = wrapped;
67
+ }
68
+
69
+ return modExports;
70
+
71
+ function wrapWriteHead(original) {
72
+ return function wrappedWriteHead() {
73
+ var headers =
74
+ arguments.length === 1
75
+ ? getSafeHeaders(this) // might be because of implicit headers.
76
+ : arguments[arguments.length - 1];
77
+
78
+ var result = original.apply(this, arguments);
79
+
80
+ var trans = httpShared.transactionForResponse.get(this);
81
+ if (trans) {
82
+ httpShared.transactionForResponse.delete(this);
83
+
84
+ // It shouldn't be possible for the statusCode to be falsy, but just in
85
+ // case we're in a bad state we should avoid throwing
86
+ trans.result = 'HTTP ' + (this.statusCode || '').toString()[0] + 'xx';
87
+ trans._setOutcomeFromHttpStatusCode(this.statusCode);
88
+
89
+ // End transacton early in case of SSE
90
+ if (headers && typeof headers === 'object' && !Array.isArray(headers)) {
91
+ Object.keys(headers).some(function (key) {
92
+ if (key.toLowerCase() !== 'content-type') return false;
93
+ if (
94
+ String(headers[key])
95
+ .toLowerCase()
96
+ .indexOf('text/event-stream') !== 0
97
+ )
98
+ return false;
99
+ agent.logger.debug(
100
+ 'detected SSE response - ending transaction %o',
101
+ { id: trans.id },
102
+ );
103
+ agent.endTransaction();
104
+ return true;
105
+ });
106
+ }
107
+ }
108
+
109
+ return result;
110
+ };
111
+ }
112
+ };