-
Notifications
You must be signed in to change notification settings - Fork 309
/
http.js
182 lines (151 loc) · 5 KB
/
http.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
'use strict'
const url = require('url')
const opentracing = require('opentracing')
const semver = require('semver')
const Tags = opentracing.Tags
const FORMAT_HTTP_HEADERS = opentracing.FORMAT_HTTP_HEADERS
function patch (http, methodName, tracer, config) {
this.wrap(http, methodName, fn => makeRequestTrace(fn))
function makeRequestTrace (request) {
return function requestTrace () {
const args = normalizeArgs.apply(null, arguments)
const uri = args.uri
const options = args.options
const callback = args.callback
if (uri === `${tracer._url.href}/v0.4/traces`) {
return request.call(this, options, callback)
}
const method = (options.method || 'GET').toUpperCase()
const parentScope = tracer.scopeManager().active()
const parent = parentScope && parentScope.span()
const span = tracer.startSpan('http.request', {
childOf: parent,
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_CLIENT,
'service.name': getServiceName(tracer, config, options),
'resource.name': method,
'span.type': 'web',
'http.method': method,
'http.url': uri
}
})
if (!hasAmazonSignature(options)) {
tracer.inject(span, FORMAT_HTTP_HEADERS, options.headers)
}
const req = request.call(this, options, callback)
req.on('socket', () => {
// empty the data stream when no other listener exists to consume it
if (req.listenerCount('response') === 1) {
req.on('response', res => res.resume())
}
})
req.on('response', res => {
span.setTag(Tags.HTTP_STATUS_CODE, res.statusCode)
res.on('end', () => span.finish())
})
req.on('error', err => {
span.addTags({
'error.type': err.name,
'error.msg': err.message,
'error.stack': err.stack
})
span.finish()
})
return req
}
}
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')
}
module.exports = [
{
name: 'http',
patch: function (http, tracer, config) {
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 (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
}
]