-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathrequest.js
More file actions
260 lines (221 loc) · 7.78 KB
/
Copy pathrequest.js
File metadata and controls
260 lines (221 loc) · 7.78 KB
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
'use strict'
// TODO: Add test with slow or unresponsive agent.
// TODO: Add telemetry for things like dropped requests, errors, etc.
const { Readable } = require('stream')
const http = require('http')
const https = require('https')
const zlib = require('zlib')
const { storage } = require('../../../../datadog-core')
const log = require('../../log')
const { isLoopbackHost, parseUrl } = require('./url')
const docker = require('./docker')
const { httpAgent, httpsAgent } = require('./agents')
const {
getMaxAttempts,
getRetryDelay,
isRetriableNetworkError,
markEndpointReached,
} = require('./retry')
const legacyStorage = storage('legacy')
const maxActiveBufferSize = 1024 * 1024 * 64
let activeBufferSize = 0
/**
* @param {Buffer|string|Readable|Array<Buffer|string>} data
* @param {object} options
* @param {(error: Error|null, result?: string|null, statusCode?: number,
* headers?: import('node:http').IncomingHttpHeaders) => void} callback
*/
function request (data, options, callback) {
if (!options.headers) {
options.headers = {}
}
if (options.url) {
const url = parseUrl(options.url)
if (url.protocol === 'unix:') {
options.socketPath = url.pathname
} else {
if (!options.path) options.path = url.path
options.protocol = url.protocol
options.hostname = url.hostname // for IPv6 this should be '::1' and not '[::1]'
options.port = url.port
}
}
// Never put the Datadog API key on a cleartext connection to a non-loopback host; that would
// expose it on the wire. Loopback (local agent, dev proxy, tests) is exempt. Strip the key
// rather than drop the request: the agent proxies telemetry with its own key, while an https
// intake URL is required to authenticate agentless traffic.
const hasApiKey = options.headers['dd-api-key'] !== undefined || options.headers['DD-API-KEY'] !== undefined
if (hasApiKey && options.protocol === 'http:' && !isLoopbackHost(options.hostname)) {
log.error(
'Not sending the Datadog API key over a non-TLS connection to %s. Configure an https intake URL.',
options.hostname
)
delete options.headers['dd-api-key']
delete options.headers['DD-API-KEY']
}
if (data instanceof Readable) {
const chunks = []
data
.on('data', (data) => {
chunks.push(data)
})
.on('end', () => {
request(Buffer.concat(chunks), options, callback)
})
.on('error', (err) => {
callback(err)
})
return
}
// The timeout should be kept low to avoid excessive queueing.
const timeout = options.timeout || 2000
const isSecure = options.protocol === 'https:'
const client = isSecure ? https : http
let dataArray = data
if (!Array.isArray(data)) {
dataArray = [data]
}
options.headers['Content-Length'] = byteLength(dataArray)
docker.inject(options.headers)
const connectionOptions = {
...options,
agent: options.agent ?? (isSecure ? httpsAgent : httpAgent),
}
/**
* @param {import('node:http').IncomingMessage} res
* @param {(error: Error|null, result?: string|null, statusCode?: number,
* headers?: import('node:http').IncomingHttpHeaders) => void} complete
* @param {(error: Error) => void} handleError
*/
const onResponse = (res, complete, handleError) => {
markEndpointReached(options)
const chunks = []
res.setTimeout(timeout)
res.once('aborted', () => {
handleError(Object.assign(new Error('Response aborted'), { code: 'ECONNRESET' }))
})
res.once('error', handleError)
res.once('timeout', () => {
const error = Object.assign(new Error('Response timed out'), { code: 'ETIMEDOUT' })
res.destroy(error)
handleError(error)
})
res.on('data', chunk => {
chunks.push(chunk)
})
res.once('end', () => {
const buffer = Buffer.concat(chunks)
if (res.statusCode >= 200 && res.statusCode <= 299) {
const contentEncoding = res.headers['content-encoding']
const isGzip = typeof contentEncoding === 'string' && contentEncoding.toLowerCase() === 'gzip'
if (isGzip) {
zlib.gunzip(buffer, (err, result) => {
if (err) {
log.error('Could not gunzip response: %s', err.message)
complete(null, '', res.statusCode, res.headers)
} else {
complete(null, result.toString(), res.statusCode, res.headers)
}
})
} else {
complete(null, buffer.toString(), res.statusCode, res.headers)
}
} else {
let errorMessage = ''
try {
const fullUrl = new URL(
options.path,
options.url || options.hostname || `http://localhost:${options.port}`
).href
errorMessage = `Error from ${fullUrl}: ${res.statusCode} ${http.STATUS_CODES[res.statusCode]}.`
} catch {
// ignore error
}
const responseData = buffer.toString()
if (responseData) {
errorMessage += ` Response from the endpoint: "${responseData}"`
}
const error = new log.NoTransmitError(errorMessage)
error.status = res.statusCode
complete(error, null, res.statusCode, res.headers)
}
})
}
// Retries always run via setTimeout so the AsyncLocalStorage store survives
// the gap before socket.connect(); ALS.run() does not call ALS.enterWith()
// outside AsyncContextFrame, so a synchronous re-entry would lose the store.
/** @param {number} attemptIndex */
const attempt = attemptIndex => {
if (!request.writable) {
log.debug('Maximum number of active requests reached: payload is discarded.')
return callback(null)
}
activeBufferSize += options.headers['Content-Length'] ?? 0
legacyStorage.run({ noop: true }, () => {
let finished = false
let settled = false
const finalize = () => {
if (finished) return
finished = true
activeBufferSize -= options.headers['Content-Length'] ?? 0
}
/**
* @param {Error | null} error
* @param {string | null} [result]
* @param {number} [statusCode]
* @param {import('node:http').IncomingHttpHeaders} [headers]
*/
const complete = (error, result, statusCode, headers) => {
if (settled) return
settled = true
finalize()
callback(error, result, statusCode, headers)
}
/**
* @param {Error} error
*/
const handleError = (error) => {
if (settled) return
if (options.retry !== false &&
attemptIndex < getMaxAttempts(options) &&
isRetriableNetworkError(error)) {
settled = true
finalize()
// Unref so a pending retry never keeps the host process alive past
// its natural exit point; long-running apps still retry because the
// event loop is held open by their own work.
setTimeout(attempt, getRetryDelay(options, attemptIndex), attemptIndex + 1).unref?.()
} else {
complete(error)
}
}
const req = client.request(connectionOptions, (res) => onResponse(res, complete, handleError))
req.once('close', finalize)
req.once('timeout', finalize)
req.once('error', handleError)
req.setTimeout(timeout, () => {
try {
if (typeof req.abort === 'function') {
req.abort()
} else {
req.destroy()
}
} catch {
// ignore
}
})
for (const buffer of dataArray) req.write(buffer)
req.end()
})
}
attempt(1)
}
function byteLength (data) {
return data.length > 0 ? data.reduce((prev, next) => prev + Buffer.byteLength(next, 'utf8'), 0) : 0
}
Object.defineProperty(request, 'writable', {
get () {
return activeBufferSize < maxActiveBufferSize
},
})
module.exports = request