@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.
- package/LICENSE +26 -0
- package/NOTICE.md +442 -0
- package/README.md +48 -0
- package/changes.json +78 -0
- package/index.d.ts +398 -0
- package/index.js +11 -0
- package/lib/InflightEventSet.js +53 -0
- package/lib/activation-method.js +119 -0
- package/lib/agent.js +941 -0
- package/lib/apm-client/apm-client.js +313 -0
- package/lib/apm-client/http-apm-client/CHANGELOG.md +271 -0
- package/lib/apm-client/http-apm-client/README.md +485 -0
- package/lib/apm-client/http-apm-client/central-config.js +41 -0
- package/lib/apm-client/http-apm-client/container-info.js +111 -0
- package/lib/apm-client/http-apm-client/detect-hostname.js +96 -0
- package/lib/apm-client/http-apm-client/index.js +1975 -0
- package/lib/apm-client/http-apm-client/logging.js +31 -0
- package/lib/apm-client/http-apm-client/ndjson.js +20 -0
- package/lib/apm-client/http-apm-client/truncate.js +434 -0
- package/lib/apm-client/noop-apm-client.js +73 -0
- package/lib/async-hooks-polyfill.js +58 -0
- package/lib/cloud-metadata/aws.js +175 -0
- package/lib/cloud-metadata/azure.js +123 -0
- package/lib/cloud-metadata/callback-coordination.js +159 -0
- package/lib/cloud-metadata/gcp.js +133 -0
- package/lib/cloud-metadata/index.js +175 -0
- package/lib/config/config.js +458 -0
- package/lib/config/normalizers.js +701 -0
- package/lib/config/schema.js +1007 -0
- package/lib/constants.js +35 -0
- package/lib/errors.js +303 -0
- package/lib/filters/sanitize-field-names.js +69 -0
- package/lib/http-request.js +249 -0
- package/lib/instrumentation/azure-functions.js +519 -0
- package/lib/instrumentation/context.js +56 -0
- package/lib/instrumentation/dropped-spans-stats.js +112 -0
- package/lib/instrumentation/elasticsearch-shared.js +63 -0
- package/lib/instrumentation/express-utils.js +91 -0
- package/lib/instrumentation/generic-span.js +322 -0
- package/lib/instrumentation/http-shared.js +424 -0
- package/lib/instrumentation/ids.js +39 -0
- package/lib/instrumentation/index.js +1127 -0
- package/lib/instrumentation/modules/@apollo/server.js +30 -0
- package/lib/instrumentation/modules/@aws-sdk/client-dynamodb.js +143 -0
- package/lib/instrumentation/modules/@aws-sdk/client-s3.js +230 -0
- package/lib/instrumentation/modules/@aws-sdk/client-sns.js +197 -0
- package/lib/instrumentation/modules/@aws-sdk/client-sqs.js +336 -0
- package/lib/instrumentation/modules/@elastic/elasticsearch.js +343 -0
- package/lib/instrumentation/modules/@hapi/hapi.js +221 -0
- package/lib/instrumentation/modules/@opentelemetry/api.js +86 -0
- package/lib/instrumentation/modules/@opentelemetry/sdk-metrics.js +79 -0
- package/lib/instrumentation/modules/@redis/client/dist/lib/client/commands-queue.js +178 -0
- package/lib/instrumentation/modules/@redis/client/dist/lib/client/index.js +49 -0
- package/lib/instrumentation/modules/@smithy/smithy-client.js +198 -0
- package/lib/instrumentation/modules/_lambda-handler.js +40 -0
- package/lib/instrumentation/modules/apollo-server-core.js +49 -0
- package/lib/instrumentation/modules/aws-sdk/dynamodb.js +155 -0
- package/lib/instrumentation/modules/aws-sdk/s3.js +184 -0
- package/lib/instrumentation/modules/aws-sdk/sns.js +232 -0
- package/lib/instrumentation/modules/aws-sdk/sqs.js +361 -0
- package/lib/instrumentation/modules/aws-sdk.js +76 -0
- package/lib/instrumentation/modules/bluebird.js +93 -0
- package/lib/instrumentation/modules/cassandra-driver.js +280 -0
- package/lib/instrumentation/modules/elasticsearch.js +191 -0
- package/lib/instrumentation/modules/express-graphql.js +66 -0
- package/lib/instrumentation/modules/express-queue.js +28 -0
- package/lib/instrumentation/modules/express.js +162 -0
- package/lib/instrumentation/modules/fastify.js +172 -0
- package/lib/instrumentation/modules/finalhandler.js +41 -0
- package/lib/instrumentation/modules/generic-pool.js +85 -0
- package/lib/instrumentation/modules/graphql.js +256 -0
- package/lib/instrumentation/modules/handlebars.js +22 -0
- package/lib/instrumentation/modules/http.js +112 -0
- package/lib/instrumentation/modules/http2.js +320 -0
- package/lib/instrumentation/modules/https.js +68 -0
- package/lib/instrumentation/modules/ioredis.js +94 -0
- package/lib/instrumentation/modules/jade.js +18 -0
- package/lib/instrumentation/modules/kafkajs.js +476 -0
- package/lib/instrumentation/modules/knex.js +91 -0
- package/lib/instrumentation/modules/koa-router.js +74 -0
- package/lib/instrumentation/modules/koa.js +15 -0
- package/lib/instrumentation/modules/memcached.js +99 -0
- package/lib/instrumentation/modules/mimic-response.js +45 -0
- package/lib/instrumentation/modules/mongodb/lib/cmap/connection_pool.js +40 -0
- package/lib/instrumentation/modules/mongodb-core.js +206 -0
- package/lib/instrumentation/modules/mongodb.js +259 -0
- package/lib/instrumentation/modules/mysql.js +200 -0
- package/lib/instrumentation/modules/mysql2.js +140 -0
- package/lib/instrumentation/modules/pg.js +148 -0
- package/lib/instrumentation/modules/pug.js +18 -0
- package/lib/instrumentation/modules/redis.js +176 -0
- package/lib/instrumentation/modules/restify.js +52 -0
- package/lib/instrumentation/modules/tedious.js +159 -0
- package/lib/instrumentation/modules/undici.js +270 -0
- package/lib/instrumentation/modules/ws.js +59 -0
- package/lib/instrumentation/noop-transaction.js +81 -0
- package/lib/instrumentation/run-context/AbstractRunContextManager.js +215 -0
- package/lib/instrumentation/run-context/AsyncHooksRunContextManager.js +106 -0
- package/lib/instrumentation/run-context/AsyncLocalStorageRunContextManager.js +73 -0
- package/lib/instrumentation/run-context/BasicRunContextManager.js +82 -0
- package/lib/instrumentation/run-context/RunContext.js +151 -0
- package/lib/instrumentation/run-context/index.js +23 -0
- package/lib/instrumentation/shimmer.js +123 -0
- package/lib/instrumentation/span-compression.js +239 -0
- package/lib/instrumentation/span.js +621 -0
- package/lib/instrumentation/template-shared.js +43 -0
- package/lib/instrumentation/timer.js +84 -0
- package/lib/instrumentation/transaction.js +571 -0
- package/lib/lambda.js +992 -0
- package/lib/load-source-map.js +100 -0
- package/lib/logging.js +212 -0
- package/lib/metrics/index.js +92 -0
- package/lib/metrics/platforms/generic/index.js +40 -0
- package/lib/metrics/platforms/generic/process-cpu.js +22 -0
- package/lib/metrics/platforms/generic/process-top.js +157 -0
- package/lib/metrics/platforms/generic/stats.js +34 -0
- package/lib/metrics/platforms/generic/system-cpu.js +51 -0
- package/lib/metrics/platforms/linux/index.js +19 -0
- package/lib/metrics/platforms/linux/stats.js +213 -0
- package/lib/metrics/queue.js +90 -0
- package/lib/metrics/registry.js +52 -0
- package/lib/metrics/reporter.js +119 -0
- package/lib/metrics/runtime.js +77 -0
- package/lib/middleware/connect.js +16 -0
- package/lib/opentelemetry-bridge/OTelBridgeNonRecordingSpan.js +150 -0
- package/lib/opentelemetry-bridge/OTelBridgeRunContext.js +124 -0
- package/lib/opentelemetry-bridge/OTelContextManager.js +82 -0
- package/lib/opentelemetry-bridge/OTelSpan.js +344 -0
- package/lib/opentelemetry-bridge/OTelTracer.js +201 -0
- package/lib/opentelemetry-bridge/OTelTracerProvider.js +25 -0
- package/lib/opentelemetry-bridge/README.md +244 -0
- package/lib/opentelemetry-bridge/index.js +15 -0
- package/lib/opentelemetry-bridge/oblog.js +23 -0
- package/lib/opentelemetry-bridge/opentelemetry-core-mini/README.md +3 -0
- package/lib/opentelemetry-bridge/opentelemetry-core-mini/internal/validators.js +52 -0
- package/lib/opentelemetry-bridge/opentelemetry-core-mini/trace/TraceState.js +109 -0
- package/lib/opentelemetry-bridge/otelutils.js +99 -0
- package/lib/opentelemetry-bridge/setup.js +76 -0
- package/lib/opentelemetry-metrics/ElasticApmMetricExporter.js +285 -0
- package/lib/opentelemetry-metrics/index.js +50 -0
- package/lib/parsers.js +225 -0
- package/lib/propwrap.js +147 -0
- package/lib/stacktraces.js +537 -0
- package/lib/symbols.js +15 -0
- package/lib/tracecontext/index.js +118 -0
- package/lib/tracecontext/traceparent.js +185 -0
- package/lib/tracecontext/tracestate.js +388 -0
- package/lib/wildcard-matcher.js +52 -0
- package/loader.mjs +7 -0
- package/package.json +299 -0
- package/start.d.ts +8 -0
- package/start.js +29 -0
- package/types/aws-lambda.d.ts +98 -0
- 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,22 @@
|
|
|
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 shimmer = require('../shimmer');
|
|
10
|
+
var templateShared = require('../template-shared');
|
|
11
|
+
|
|
12
|
+
module.exports = function (handlebars, agent, { enabled }) {
|
|
13
|
+
if (!enabled) return handlebars;
|
|
14
|
+
agent.logger.debug('shimming handlebars.compile');
|
|
15
|
+
shimmer.wrap(
|
|
16
|
+
handlebars,
|
|
17
|
+
'compile',
|
|
18
|
+
templateShared.wrapCompile(agent, 'handlebars'),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
return handlebars;
|
|
22
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -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
|
+
};
|