-
Notifications
You must be signed in to change notification settings - Fork 306
/
client.js
311 lines (249 loc) · 8.18 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
'use strict'
const url = require('url')
const semver = require('semver')
const opentracing = require('opentracing')
const log = require('../../dd-trace/src/log')
const constants = require('../../dd-trace/src/constants')
const tags = require('../../../ext/tags')
const kinds = require('../../../ext/kinds')
const formats = require('../../../ext/formats')
const urlFilter = require('../../dd-trace/src/plugins/util/urlfilter')
const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
const Reference = opentracing.Reference
const HTTP_HEADERS = formats.HTTP_HEADERS
const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
const HTTP_REQUEST_HEADERS = tags.HTTP_REQUEST_HEADERS
const HTTP_RESPONSE_HEADERS = tags.HTTP_RESPONSE_HEADERS
const SPAN_KIND = tags.SPAN_KIND
const CLIENT = kinds.CLIENT
const REFERENCE_CHILD_OF = opentracing.REFERENCE_CHILD_OF
const REFERENCE_NOOP = constants.REFERENCE_NOOP
function patch (http, methodName, tracer, config) {
config = normalizeConfig(tracer, config)
this.wrap(http, methodName, fn => makeRequestTrace(fn))
function makeRequestTrace (request) {
return function requestTrace () {
let args
try {
args = normalizeArgs.apply(null, arguments)
} catch (e) {
log.error(e)
return request.apply(this, arguments)
}
const uri = args.uri
const options = args.options
let callback = args.callback
const method = (options.method || 'GET').toUpperCase()
const scope = tracer.scope()
const childOf = scope.active()
const type = config.filter(uri) ? REFERENCE_CHILD_OF : REFERENCE_NOOP
const span = tracer.startSpan('http.request', {
references: [
new Reference(type, childOf)
],
tags: {
[SPAN_KIND]: CLIENT,
'service.name': getServiceName(tracer, config, options),
'resource.name': method,
'span.type': 'http',
'http.method': method,
'http.url': uri
}
})
if (!hasAmazonSignature(options)) {
tracer.inject(span, HTTP_HEADERS, options.headers)
}
analyticsSampler.sample(span, config.analytics)
callback = scope.bind(callback, childOf)
const req = scope.bind(request, span).call(this, options, callback)
const emit = req.emit
req.emit = function (eventName, arg) {
switch (eventName) {
case 'response': {
const res = arg
scope.bind(res)
span.setTag(HTTP_STATUS_CODE, res.statusCode)
addResponseHeaders(res, span, config)
if (!config.validateStatus(res.statusCode)) {
span.setTag('error', 1)
}
res.on('end', () => finish(req, res, span, config))
break
}
case 'error':
addError(span, arg)
case 'abort': // eslint-disable-line no-fallthrough
case 'close': // eslint-disable-line no-fallthrough
finish(req, null, span, config)
}
return emit.apply(this, arguments)
}
scope.bind(req)
return req
}
}
function finish (req, res, span, config) {
addRequestHeaders(req, span, config)
config.hooks.request(span, req, res)
span.finish()
}
function addError (span, error) {
span.addTags({
'error.type': error.name,
'error.msg': error.message,
'error.stack': error.stack
})
return error
}
function addRequestHeaders (req, span, config) {
config.headers.forEach(key => {
const value = req.getHeader(key)
if (value) {
span.setTag(`${HTTP_REQUEST_HEADERS}.${key}`, value)
}
})
}
function addResponseHeaders (res, span, config) {
config.headers.forEach(key => {
const value = res.headers[key]
if (value) {
span.setTag(`${HTTP_RESPONSE_HEADERS}.${key}`, value)
}
})
}
function extractUrl (options) {
const uri = options
const agent = options.agent || http.globalAgent
return typeof uri === 'string' ? uri : url.format({
protocol: options.protocol || agent.protocol,
hostname: options.hostname || options.host || 'localhost',
port: options.port,
pathname: options.path || options.pathname || '/'
})
}
function normalizeArgs (inputURL, inputOptions, callback) {
let options = typeof inputURL === 'string' ? url.parse(inputURL) : Object.assign({}, inputURL)
options.headers = options.headers || {}
if (typeof inputOptions === 'function') {
callback = inputOptions
} else if (typeof inputOptions === 'object') {
options = Object.assign(options, inputOptions)
}
const uri = extractUrl(options)
return { uri, options, callback }
}
}
function getHost (options) {
if (typeof options === 'string') {
return url.parse(options).host
}
const hostname = options.hostname || options.host || 'localhost'
const port = options.port
return [hostname, port].filter(val => val).join(':')
}
function getServiceName (tracer, config, options) {
if (config.splitByDomain) {
return getHost(options)
} else if (config.service) {
return config.service
}
return `${tracer._service}-http-client`
}
function hasAmazonSignature (options) {
if (!options) {
return false
}
if (options.headers) {
const headers = Object.keys(options.headers)
.reduce((prev, next) => Object.assign(prev, {
[next.toLowerCase()]: options.headers[next]
}), {})
if (headers['x-amz-signature']) {
return true
}
if ([].concat(headers['authorization']).some(startsWith('AWS4-HMAC-SHA256'))) {
return true
}
}
return options.path && options.path.toLowerCase().indexOf('x-amz-signature=') !== -1
}
function startsWith (searchString) {
return value => String(value).startsWith(searchString)
}
function unpatch (http) {
this.unwrap(http, 'request')
this.unwrap(http, 'get')
}
function getStatusValidator (config) {
if (typeof config.validateStatus === 'function') {
return config.validateStatus
} else if (config.hasOwnProperty('validateStatus')) {
log.error('Expected `validateStatus` to be a function.')
}
return code => code < 400 || code >= 500
}
function getFilter (tracer, config) {
const blacklist = tracer._url ? [`${tracer._url.href}/v0.4/traces`] : []
config = Object.assign({}, config, {
blacklist: blacklist.concat(config.blacklist || [])
})
return urlFilter.getFilter(config)
}
function normalizeConfig (tracer, config) {
config = config.client || config
const validateStatus = getStatusValidator(config)
const filter = getFilter(tracer, config)
const headers = getHeaders(config)
const hooks = getHooks(config)
return Object.assign({}, config, {
validateStatus,
filter,
headers,
hooks
})
}
function getHeaders (config) {
if (!Array.isArray(config.headers)) return []
return config.headers
.filter(key => typeof key === 'string')
.map(key => key.toLowerCase())
}
function getHooks (config) {
const noop = () => {}
const request = (config.hooks && config.hooks.request) || noop
return { request }
}
module.exports = [
{
name: 'http',
patch: function (http, tracer, config) {
if (config.client === false) return
patch.call(this, http, 'request', tracer, config)
if (semver.satisfies(process.version, '>=8')) {
/**
* In newer Node versions references internal to modules, such as `http(s).get` calling `http(s).request`, do
* not use externally patched versions, which is why we need to also patch `get` here separately.
*/
patch.call(this, http, 'get', tracer, config)
}
},
unpatch
},
{
name: 'https',
patch: function (http, tracer, config) {
if (config.client === false) return
if (semver.satisfies(process.version, '>=9')) {
patch.call(this, http, 'request', tracer, config)
patch.call(this, http, 'get', tracer, config)
} else {
/**
* Below Node v9 the `https` module invokes `http.request`, which would end up counting requests twice.
* So rather then patch the `https` module, we ensure the `http` module is patched and we count only there.
*/
require('http')
}
},
unpatch
}
]