|
| 1 | +/* |
| 2 | + * Copyright 2020 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const UNKNOWN = 'Unknown' |
| 9 | +const symbols = require('../../../symbols') |
| 10 | +const InstrumentationDescriptor = require('../../../instrumentation-descriptor') |
| 11 | + |
| 12 | +function validate(shim, AWS) { |
| 13 | + if (!shim.isFunction(AWS.NodeHttpClient)) { |
| 14 | + shim.logger.debug('Could not find NodeHttpClient, not instrumenting.') |
| 15 | + return false |
| 16 | + } |
| 17 | + if (!shim.isFunction(AWS.Service) || !shim.isFunction(AWS.Service.prototype.makeRequest)) { |
| 18 | + shim.logger.debug('Could not find AWS.Service#makeRequest, not instrumenting.') |
| 19 | + return false |
| 20 | + } |
| 21 | + return true |
| 22 | +} |
| 23 | + |
| 24 | +function instrument(shim, AWS) { |
| 25 | + shim.wrap(AWS.NodeHttpClient.prototype, 'handleRequest', wrapHandleRequest) |
| 26 | + shim.wrapReturn(AWS.Service.prototype, 'makeRequest', wrapMakeRequest) |
| 27 | +} |
| 28 | + |
| 29 | +function wrapHandleRequest(shim, handleRequest) { |
| 30 | + return function wrappedHandleRequest(httpRequest) { |
| 31 | + if (httpRequest) { |
| 32 | + if (!httpRequest.headers) { |
| 33 | + httpRequest.headers = Object.create(null) |
| 34 | + } |
| 35 | + httpRequest.headers[symbols.disableDT] = true |
| 36 | + } else { |
| 37 | + shim.logger.debug('Unknown arguments to AWS.NodeHttpClient#handleRequest!') |
| 38 | + } |
| 39 | + |
| 40 | + return handleRequest.apply(this, arguments) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +function wrapMakeRequest(shim, fn, name, request) { |
| 45 | + if (!request) { |
| 46 | + shim.logger.trace('No request object returned from Service#makeRequest') |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + const service = getServiceName(this) |
| 51 | + const region = this?.config?.region |
| 52 | + request.on('complete', function onAwsRequestComplete() { |
| 53 | + const httpRequest = request.httpRequest && request.httpRequest.stream |
| 54 | + const segment = shim.getSegment(httpRequest) |
| 55 | + if (!httpRequest || !segment) { |
| 56 | + shim.logger.trace('No segment found for request, not extracting information.') |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + const requestRegion = request?.httpRequest?.region |
| 61 | + const requestId = request?.response?.requestId |
| 62 | + |
| 63 | + segment.addAttribute('aws.operation', request.operation || UNKNOWN) |
| 64 | + segment.addAttribute('aws.requestId', requestId || UNKNOWN) |
| 65 | + segment.addAttribute('aws.service', service || UNKNOWN) |
| 66 | + segment.addAttribute('aws.region', requestRegion || region || UNKNOWN) |
| 67 | + }) |
| 68 | + |
| 69 | + shim.wrap(request, 'promise', function wrapPromiseFunc(shim, original) { |
| 70 | + const activeSegment = shim.getActiveSegment() |
| 71 | + |
| 72 | + return function wrappedPromiseFunc() { |
| 73 | + if (!activeSegment) { |
| 74 | + return original.apply(this, arguments) |
| 75 | + } |
| 76 | + |
| 77 | + const promise = shim.applySegment(original, activeSegment, false, this, arguments) |
| 78 | + |
| 79 | + return shim.bindPromise(promise, activeSegment) |
| 80 | + } |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +function getServiceName(service) { |
| 85 | + if (service.api && (service.api.abbreviation || service.api.serviceId)) { |
| 86 | + return service.api.abbreviation || service.api.serviceId |
| 87 | + } |
| 88 | + |
| 89 | + // In theory, getting the `constructor.prototype` should be redundant with |
| 90 | + // checking `service`. However, the aws-sdk dynamically generates classes and |
| 91 | + // doing this deep check was the recommended method by the maintainers. |
| 92 | + const constructor = service.constructor |
| 93 | + const api = constructor && constructor.prototype && constructor.prototype.api |
| 94 | + if (api) { |
| 95 | + return api.abbreviation || api.serviceId |
| 96 | + } |
| 97 | + return null |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = { |
| 101 | + name: 'core', |
| 102 | + type: InstrumentationDescriptor.TYPE_GENERIC, |
| 103 | + validate, |
| 104 | + instrument |
| 105 | +} |
0 commit comments