|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import { Status, CanonicalCode, Span } from '@opentelemetry/types'; |
| 17 | +import { Status, CanonicalCode, Span, Attributes } from '@opentelemetry/types'; |
18 | 18 | import { |
19 | 19 | RequestOptions, |
20 | 20 | IncomingMessage, |
21 | 21 | ClientRequest, |
22 | 22 | IncomingHttpHeaders, |
23 | 23 | OutgoingHttpHeaders, |
| 24 | + ServerResponse, |
24 | 25 | } from 'http'; |
25 | 26 | import { IgnoreMatcher, Err, ParsedRequestOptions } from './types'; |
26 | 27 | import { AttributeNames } from './enums/AttributeNames'; |
27 | 28 | import * as url from 'url'; |
| 29 | +import { Socket } from 'net'; |
28 | 30 |
|
29 | 31 | export const OT_REQUEST_HEADER = 'x-opentelemetry-outgoing-request'; |
30 | 32 | /** |
@@ -280,3 +282,157 @@ export const isValidOptionsType = (options: unknown): boolean => { |
280 | 282 | export const isOpenTelemetryRequest = (options: RequestOptions) => { |
281 | 283 | return !!(options && options.headers && options.headers[OT_REQUEST_HEADER]); |
282 | 284 | }; |
| 285 | + |
| 286 | +/** |
| 287 | + * Returns outgoing request attributes scoped to the options passed to the request |
| 288 | + * @param {ParsedRequestOptions} requestOptions the same options used to make the request |
| 289 | + * @param {{ component: string, hostname: string }} options used to pass data needed to create attributes |
| 290 | + */ |
| 291 | +export const getOutgoingRequestAttributes = ( |
| 292 | + requestOptions: ParsedRequestOptions, |
| 293 | + options: { component: string; hostname: string } |
| 294 | +): Attributes => { |
| 295 | + const host = requestOptions.host; |
| 296 | + const hostname = |
| 297 | + requestOptions.hostname || |
| 298 | + host?.replace(/^(.*)(\:[0-9]{1,5})/, '$1') || |
| 299 | + 'localhost'; |
| 300 | + const requestMethod = requestOptions.method; |
| 301 | + const method = requestMethod ? requestMethod.toUpperCase() : 'GET'; |
| 302 | + const headers = requestOptions.headers || {}; |
| 303 | + const userAgent = headers['user-agent']; |
| 304 | + |
| 305 | + const attributes: Attributes = { |
| 306 | + [AttributeNames.HTTP_URL]: getAbsoluteUrl( |
| 307 | + requestOptions, |
| 308 | + headers, |
| 309 | + `${options.component}:` |
| 310 | + ), |
| 311 | + [AttributeNames.HTTP_METHOD]: method, |
| 312 | + [AttributeNames.HTTP_TARGET]: requestOptions.path || '/', |
| 313 | + [AttributeNames.NET_PEER_NAME]: hostname, |
| 314 | + }; |
| 315 | + |
| 316 | + if (userAgent !== undefined) { |
| 317 | + attributes[AttributeNames.HTTP_USER_AGENT] = userAgent; |
| 318 | + } |
| 319 | + return attributes; |
| 320 | +}; |
| 321 | + |
| 322 | +/** |
| 323 | + * Returns attributes related to the kind of HTTP protocol used |
| 324 | + * @param {string} [kind] Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC". |
| 325 | + */ |
| 326 | +export const getAttributesFromHttpKind = (kind?: string): Attributes => { |
| 327 | + const attributes: Attributes = {}; |
| 328 | + if (kind) { |
| 329 | + attributes[AttributeNames.HTTP_FLAVOR] = kind; |
| 330 | + if (kind.toUpperCase() !== 'QUIC') { |
| 331 | + attributes[AttributeNames.NET_TRANSPORT] = AttributeNames.IP_TCP; |
| 332 | + } else { |
| 333 | + attributes[AttributeNames.NET_TRANSPORT] = AttributeNames.IP_UDP; |
| 334 | + } |
| 335 | + } |
| 336 | + return attributes; |
| 337 | +}; |
| 338 | + |
| 339 | +/** |
| 340 | + * Returns outgoing request attributes scoped to the response data |
| 341 | + * @param {IncomingMessage} response the response object |
| 342 | + * @param {{ hostname: string }} options used to pass data needed to create attributes |
| 343 | + */ |
| 344 | +export const getOutgoingRequestAttributesOnResponse = ( |
| 345 | + response: IncomingMessage, |
| 346 | + options: { hostname: string } |
| 347 | +): Attributes => { |
| 348 | + const { statusCode, statusMessage, httpVersion, socket } = response; |
| 349 | + const { remoteAddress, remotePort } = socket; |
| 350 | + const attributes: Attributes = { |
| 351 | + [AttributeNames.NET_PEER_IP]: remoteAddress, |
| 352 | + [AttributeNames.NET_PEER_PORT]: remotePort, |
| 353 | + [AttributeNames.HTTP_HOST]: `${options.hostname}:${remotePort}`, |
| 354 | + }; |
| 355 | + |
| 356 | + if (statusCode) { |
| 357 | + attributes[AttributeNames.HTTP_STATUS_CODE] = statusCode; |
| 358 | + attributes[AttributeNames.HTTP_STATUS_TEXT] = ( |
| 359 | + statusMessage || '' |
| 360 | + ).toUpperCase(); |
| 361 | + } |
| 362 | + |
| 363 | + const httpKindAttributes = getAttributesFromHttpKind(httpVersion); |
| 364 | + return Object.assign(attributes, httpKindAttributes); |
| 365 | +}; |
| 366 | + |
| 367 | +/** |
| 368 | + * Returns incoming request attributes scoped to the request data |
| 369 | + * @param {IncomingMessage} request the request object |
| 370 | + * @param {{ component: string, serverName?: string }} options used to pass data needed to create attributes |
| 371 | + */ |
| 372 | +export const getIncomingRequestAttributes = ( |
| 373 | + request: IncomingMessage, |
| 374 | + options: { component: string; serverName?: string } |
| 375 | +): Attributes => { |
| 376 | + const headers = request.headers; |
| 377 | + const userAgent = headers['user-agent']; |
| 378 | + const ips = headers['x-forwarded-for']; |
| 379 | + const method = request.method || 'GET'; |
| 380 | + const httpVersion = request.httpVersion; |
| 381 | + const requestUrl = request.url ? url.parse(request.url) : null; |
| 382 | + const host = requestUrl?.host || headers.host; |
| 383 | + const hostname = |
| 384 | + requestUrl?.hostname || |
| 385 | + host?.replace(/^(.*)(\:[0-9]{1,5})/, '$1') || |
| 386 | + 'localhost'; |
| 387 | + const serverName = options.serverName; |
| 388 | + const attributes: Attributes = { |
| 389 | + [AttributeNames.HTTP_URL]: getAbsoluteUrl( |
| 390 | + requestUrl, |
| 391 | + headers, |
| 392 | + `${options.component}:` |
| 393 | + ), |
| 394 | + [AttributeNames.HTTP_HOST]: host, |
| 395 | + [AttributeNames.NET_HOST_NAME]: hostname, |
| 396 | + [AttributeNames.HTTP_METHOD]: method, |
| 397 | + }; |
| 398 | + |
| 399 | + if (typeof ips === 'string') { |
| 400 | + attributes[AttributeNames.HTTP_CLIENT_IP] = ips.split(',')[0]; |
| 401 | + } |
| 402 | + |
| 403 | + if (typeof serverName === 'string') { |
| 404 | + attributes[AttributeNames.HTTP_SERVER_NAME] = serverName; |
| 405 | + } |
| 406 | + |
| 407 | + if (requestUrl) { |
| 408 | + attributes[AttributeNames.HTTP_TARGET] = requestUrl.path || '/'; |
| 409 | + attributes[AttributeNames.HTTP_ROUTE] = requestUrl.pathname || '/'; |
| 410 | + } |
| 411 | + |
| 412 | + if (userAgent !== undefined) { |
| 413 | + attributes[AttributeNames.HTTP_USER_AGENT] = userAgent; |
| 414 | + } |
| 415 | + |
| 416 | + const httpKindAttributes = getAttributesFromHttpKind(httpVersion); |
| 417 | + return Object.assign(attributes, httpKindAttributes); |
| 418 | +}; |
| 419 | + |
| 420 | +/** |
| 421 | + * Returns incoming request attributes scoped to the response data |
| 422 | + * @param {(ServerResponse & { socket: Socket; })} response the response object |
| 423 | + */ |
| 424 | +export const getIncomingRequestAttributesOnResponse = ( |
| 425 | + response: ServerResponse & { socket: Socket } |
| 426 | +): Attributes => { |
| 427 | + const { statusCode, statusMessage, socket } = response; |
| 428 | + const { localAddress, localPort, remoteAddress, remotePort } = socket; |
| 429 | + |
| 430 | + return { |
| 431 | + [AttributeNames.NET_HOST_IP]: localAddress, |
| 432 | + [AttributeNames.NET_HOST_PORT]: localPort, |
| 433 | + [AttributeNames.NET_PEER_IP]: remoteAddress, |
| 434 | + [AttributeNames.NET_PEER_PORT]: remotePort, |
| 435 | + [AttributeNames.HTTP_STATUS_CODE]: statusCode, |
| 436 | + [AttributeNames.HTTP_STATUS_TEXT]: (statusMessage || '').toUpperCase(), |
| 437 | + }; |
| 438 | +}; |
0 commit comments