-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathclient.js
255 lines (202 loc) · 6.92 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
'use strict'
const ClientPlugin = require('../../dd-trace/src/plugins/client')
const { storage } = require('../../datadog-core')
const tags = require('../../../ext/tags')
const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
const formats = require('../../../ext/formats')
const HTTP_HEADERS = formats.HTTP_HEADERS
const urlFilter = require('../../dd-trace/src/plugins/util/urlfilter')
const log = require('../../dd-trace/src/log')
const { CLIENT_PORT_KEY, COMPONENT, ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
const { URL } = require('url')
const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
const HTTP_REQUEST_HEADERS = tags.HTTP_REQUEST_HEADERS
const HTTP_RESPONSE_HEADERS = tags.HTTP_RESPONSE_HEADERS
class HttpClientPlugin extends ClientPlugin {
static get id () { return 'http' }
static get prefix () { return `apm:http:client:request` }
bindStart (message) {
const { args, http = {} } = message
const store = storage.getStore()
const options = args.options
const agent = options.agent || options._defaultAgent || http.globalAgent || {}
const protocol = options.protocol || agent.protocol || 'http:'
const hostname = options.hostname || options.host || 'localhost'
const host = options.port ? `${hostname}:${options.port}` : hostname
const pathname = options.path || options.pathname
const path = pathname ? pathname.split(/[?#]/)[0] : '/'
const uri = `${protocol}//${host}${path}`
const allowed = this.config.filter(uri)
const method = (options.method || 'GET').toUpperCase()
const childOf = store && allowed ? store.span : null
// TODO delegate to super.startspan
const span = this.startSpan(this.operationName(), {
childOf,
meta: {
[COMPONENT]: this.constructor.id,
'span.kind': 'client',
'service.name': this.serviceName({ pluginConfig: this.config, sessionDetails: extractSessionDetails(options) }),
'resource.name': method,
'span.type': 'http',
'http.method': method,
'http.url': uri,
'out.host': hostname
},
metrics: {
[CLIENT_PORT_KEY]: parseInt(options.port)
}
}, false)
// TODO: Figure out a better way to do this for any span.
if (!allowed) {
span._spanContext._trace.record = false
}
if (this.shouldInjectTraceHeaders(options, uri)) {
this.tracer.inject(span, HTTP_HEADERS, options.headers)
}
analyticsSampler.sample(span, this.config.measured)
message.span = span
message.parentStore = store
message.currentStore = { ...store, span }
return message.currentStore
}
shouldInjectTraceHeaders (options, uri) {
if (hasAmazonSignature(options) && !this.config.enablePropagationWithAmazonHeaders) {
return false
}
if (!this.config.propagationFilter(uri)) {
return false
}
return true
}
bindAsyncStart ({ parentStore }) {
return parentStore
}
finish ({ req, res, span }) {
if (!span) return
if (res) {
const status = res.status || res.statusCode
span.setTag(HTTP_STATUS_CODE, status)
if (!this.config.validateStatus(status)) {
span.setTag('error', 1)
}
addResponseHeaders(res, span, this.config)
}
addRequestHeaders(req, span, this.config)
this.config.hooks.request(span, req, res)
this.tagPeerService(span)
span.finish()
}
error ({ span, error, args, customRequestTimeout }) {
if (!span) return
if (error) {
span.addTags({
[ERROR_TYPE]: error.name,
[ERROR_MESSAGE]: error.message || error.code,
[ERROR_STACK]: error.stack
})
} else {
// conditions for no error:
// 1. not using a custom agent instance with custom timeout specified
// 2. no invocation of `req.setTimeout` or `socket.setTimeout`
if (!args.options.agent?.options.timeout && !customRequestTimeout) return
span.setTag('error', 1)
}
}
configure (config) {
return super.configure(normalizeClientConfig(config))
}
}
function addResponseHeaders (res, span, config) {
if (!res.headers) return
const headers = typeof res.headers.entries === 'function'
? Object.fromEntries(res.headers.entries())
: res.headers
config.headers.forEach(([key, tag]) => {
const value = headers[key]
if (value) {
span.setTag(tag || `${HTTP_RESPONSE_HEADERS}.${key}`, value)
}
})
}
function addRequestHeaders (req, span, config) {
const headers = req.headers && typeof req.headers.entries === 'function'
? Object.fromEntries(req.headers.entries())
: req.headers || req.getHeaders()
config.headers.forEach(([key, tag]) => {
const value = Array.isArray(headers[key]) ? headers[key].toString() : headers[key]
if (value) {
span.setTag(tag || `${HTTP_REQUEST_HEADERS}.${key}`, value)
}
})
}
function normalizeClientConfig (config) {
const validateStatus = getStatusValidator(config)
const filter = getFilter(config)
const propagationFilter = getFilter({ blocklist: config.propagationBlocklist })
const headers = getHeaders(config)
const hooks = getHooks(config)
return Object.assign({}, config, {
validateStatus,
filter,
propagationFilter,
headers,
hooks
})
}
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 (config) {
config = Object.assign({}, config, {
blocklist: config.blocklist || []
})
return urlFilter.getFilter(config)
}
function getHeaders (config) {
if (!Array.isArray(config.headers)) return []
return config.headers
.filter(key => typeof key === 'string')
.map(h => h.split(':'))
.map(([key, tag]) => [key.toLowerCase(), tag])
}
function getHooks (config) {
const noop = () => {}
const request = (config.hooks && config.hooks.request) || noop
return { request }
}
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
}
}
const search = options.search || options.path
return search && search.toLowerCase().indexOf('x-amz-signature=') !== -1
}
function extractSessionDetails (options) {
if (typeof options === 'string') {
return new URL(options).host
}
const host = options.hostname || options.host || 'localhost'
const port = options.port
return { host, port }
}
function startsWith (searchString) {
return value => String(value).startsWith(searchString)
}
module.exports = HttpClientPlugin