UNPKG

appdynamics

Version:

Performance Profiler and Monitor

118 lines (99 loc) 3.89 kB
/* Copyright (c) AppDynamics, Inc., and its affiliates 2015 All Rights Reserved */ 'use strict'; const url = require('url'); const { SemanticAttributes } = require('@opentelemetry/semantic-conventions'); const { SpanStatusCode } = require('@opentelemetry/api'); module.exports.getIncomingRequestAttributes = getIncomingRequestAttributes; module.exports.getIncomingRequestAttributesOnResponse = getIncomingRequestAttributesOnResponse; module.exports.parseResponseStatus = parseResponseStatus; module.exports.getOutgoingRequestAttributes = getOutgoingRequestAttributes; module.exports.getOutgoingRequestAttributesOnResponse = getOutgoingRequestAttributesOnResponse; function parseResponseStatus(response) { if (!response || !response.statusCode) { return { code: SpanStatusCode.ERROR }; } if (response.statusCode >= 100 && response.statusCode < 400) { return { code: SpanStatusCode.OK }; } return { code: SpanStatusCode.ERROR }; } function getAbsoluteUrl(requestUrl, headers) { const reqUrlObject = requestUrl || {}; const headersObject = headers || {}; const protocol = reqUrlObject.protocol || 'http:'; const port = (reqUrlObject.port || '').toString(); const path = reqUrlObject.path || '/'; var host = reqUrlObject.host || reqUrlObject.hostname || headersObject.host || 'localhost'; // if there is no port in host and there is a port, // it should be displayed if it's not 80 and 443 (default ports) if ( host.indexOf(':') === -1 && port && port !== '80' && port !== '443' ) { host += `:${port}`; } return `${protocol}//${host}${path}`; } function getIncomingRequestAttributes(request) { const attributes = {}; if (request) { const headers = request.headers; const userAgent = headers && headers['user-agent']; const method = request.method || 'GET'; const httpVersion = request.httpVersion; const requestUrl = request.url && url.parse(request.url); const host = requestUrl && requestUrl.host ? requestUrl.host : headers && headers.host ? headers.host : 'localhost'; attributes[SemanticAttributes.HTTP_URL] = getAbsoluteUrl(requestUrl, headers); attributes[SemanticAttributes.HTTP_HOST] = host; attributes[SemanticAttributes.HTTP_METHOD] = method; if(requestUrl) { attributes[SemanticAttributes.HTTP_TARGET] = requestUrl.pathname || '/'; } if(userAgent) { attributes[SemanticAttributes.HTTP_USER_AGENT] = userAgent; } if(httpVersion) { attributes[SemanticAttributes.HTTP_FLAVOR] = httpVersion; } } return attributes; } function getIncomingRequestAttributesOnResponse(response) { const attributes = {}; if(response) { attributes[SemanticAttributes.HTTP_STATUS_CODE] = response.statusCode; } return attributes; } function getOutgoingRequestAttributes(parsedRequest, headers) { const attributes = {}; const userAgent = headers && headers['user-agent'] || undefined; if(userAgent) { attributes[SemanticAttributes.HTTP_USER_AGENT] = userAgent; } attributes[SemanticAttributes.HTTP_METHOD] = parsedRequest && parsedRequest.method || 'GET'; attributes[SemanticAttributes.HTTP_TARGET] = parsedRequest && parsedRequest.path || '/'; attributes[SemanticAttributes.HTTP_URL] = getAbsoluteUrl(parsedRequest, headers); return attributes; } function getOutgoingRequestAttributesOnResponse(response, hostname) { const attributes = {}; if (response) { const statusCode = response.statusCode; const httpVersion = response.httpVersion; const socket = response.socket; const remotePort = socket && socket.remotePort || 0; attributes[SemanticAttributes.HTTP_HOST] = `${hostname}:${remotePort}`; attributes[SemanticAttributes.HTTP_STATUS_CODE] = statusCode; if(httpVersion) { attributes[SemanticAttributes.HTTP_FLAVOR] = httpVersion; } } return attributes; }