forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request newrelic#106 from bizob2828/wrap-externals-v3
Wrap externals v3
- Loading branch information
Showing
30 changed files
with
1,097 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright 2021 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
const UNKNOWN = 'Unknown' | ||
|
||
module.exports = function instrumentSmithyClient(shim, name, resolvedName) { | ||
const fileNameIndex = resolvedName.indexOf('/index') | ||
const relativeFolder = resolvedName.substr(0, fileNameIndex) | ||
|
||
// The path changes depending on the version... so we don't want to hard-code the relative | ||
// path from the module root. | ||
const smithyClientExport = shim.require(`${relativeFolder}/client`) | ||
|
||
if (!shim.isFunction(smithyClientExport.Client)) { | ||
shim.logger.debug('Could not find Smithy Client, not instrumenting.') | ||
return | ||
} | ||
|
||
shim.wrapClass(smithyClientExport, 'Client', { post: postClientConstructor, es6: true }) | ||
} | ||
|
||
/** | ||
* Calls the instances middlewareStack.use to register | ||
* a plugin that adds 2 different middleware at various | ||
* stages of a middleware stack | ||
* see: https://aws.amazon.com/blogs/developer/middleware-stack-modular-aws-sdk-js/ | ||
* | ||
* @param {Shim} shim | ||
*/ | ||
function postClientConstructor(shim) { | ||
this.middlewareStack.use(getPlugin(shim, this.config)) | ||
} | ||
|
||
/** | ||
* Returns the plugin object that adds 2 middleware | ||
* | ||
* @param {Shim} shim | ||
* @param {Object} config smithy client config | ||
*/ | ||
function getPlugin(shim, config) { | ||
return { | ||
applyToStack: (clientStack) => { | ||
clientStack.add(headerMiddleware.bind(null, shim), { | ||
name: 'NewRelicHeader', | ||
step: 'build' | ||
}) | ||
clientStack.add(attrMiddleware.bind(null, shim, config), { | ||
name: 'NewRelicDeserialize', | ||
step: 'deserialize' | ||
}) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Wraps the build middleware step to add the disable DT | ||
* header to all outgoing requests | ||
* | ||
* @param {Shim} shim | ||
* @param {function} next next function in middleware chain | ||
* @return {function} | ||
* | ||
*/ | ||
function headerMiddleware(shim, next) { | ||
return async function wrappedHeaderMw(args) { | ||
args.request.headers[shim.DISABLE_DT] = true | ||
return await next(args) | ||
} | ||
} | ||
|
||
/** | ||
* Wraps the deserialize middleware step to add the | ||
* appropriate segment attributes for the AWS command | ||
* | ||
* @param {Shim} shim | ||
* @param {Object} config AWS command configuration | ||
* @param {function} next next function in middleware chain | ||
* @param {Object} contxt AWS command context | ||
* @return {function} | ||
*/ | ||
function attrMiddleware(shim, config, next, context) { | ||
return async function wrappedMiddleware(args) { | ||
let region | ||
try { | ||
region = await config.region() | ||
} catch (err) { | ||
shim.logger.debug(err, 'Failed to get the AWS region') | ||
} finally { | ||
const result = await next(args) | ||
addAwsAttributes({ result, config, region, shim, context }) | ||
return result | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Adds the necessary aws.* attributes to either the External or first | ||
* class operation segment | ||
* | ||
* @param {Object} params | ||
* @param {Object} params.result result from middleware | ||
* @param {Object} params.config AWS config | ||
* @param {string} params.region AWS region | ||
* @param {Shim} params.shim | ||
* @param {Object} params.context smithy client context | ||
*/ | ||
function addAwsAttributes({ result, config, region, shim, context }) { | ||
try { | ||
const { response } = result | ||
const segment = shim.getSegment(response.body.req) | ||
segment.addAttribute('aws.operation', context.commandName || UNKNOWN) | ||
segment.addAttribute('aws.requestId', response.headers['x-amzn-requestid'] || UNKNOWN) | ||
segment.addAttribute('aws.service', config.serviceId || UNKNOWN) | ||
segment.addAttribute('aws.region', region || UNKNOWN) | ||
} catch (err) { | ||
shim.logger.debug(err, 'Failed to add AWS attributes to segment') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "aws-sdk-tests", | ||
"name": "aws-sdk-v2-tests", | ||
"version": "0.0.0", | ||
"private": true, | ||
"tests": [ | ||
|
69 changes: 69 additions & 0 deletions
69
merged/aws-sdk/tests/versioned/aws-sdk-v3/api-gateway.tap.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2021 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const tap = require('tap') | ||
const utils = require('@newrelic/test-utilities') | ||
|
||
const common = require('../common') | ||
const { createEmptyResponseServer, FAKE_CREDENTIALS } = require('../aws-server-stubs') | ||
|
||
tap.test('APIGatewayClient', (t) => { | ||
t.autoend() | ||
let helper = null | ||
let server = null | ||
let service = null | ||
let CreateApiKeyCommand = null | ||
|
||
t.beforeEach(async () => { | ||
server = createEmptyResponseServer() | ||
await new Promise((resolve) => { | ||
server.listen(0, resolve) | ||
}) | ||
helper = utils.TestAgent.makeInstrumented() | ||
common.registerCoreInstrumentation(helper) | ||
const { APIGatewayClient, ...lib } = require('@aws-sdk/client-api-gateway') | ||
CreateApiKeyCommand = lib.CreateApiKeyCommand | ||
const endpoint = `http://localhost:${server.address().port}` | ||
service = new APIGatewayClient({ | ||
credentials: FAKE_CREDENTIALS, | ||
endpoint, | ||
region: 'us-east-1' | ||
}) | ||
}) | ||
|
||
t.afterEach(() => { | ||
server.destroy() | ||
helper && helper.unload() | ||
}) | ||
|
||
t.test('CreateApiKeyCommand', (t) => { | ||
helper.runInTransaction(async (tx) => { | ||
const cmd = new CreateApiKeyCommand({ | ||
customerId: 'STRING_VALUE', | ||
description: 'STRING_VALUE', | ||
enabled: true || false, | ||
generateDistinctId: true || false, | ||
name: 'STRING_VALUE', | ||
stageKeys: [ | ||
{ | ||
restApiId: 'STRING_VALUE', | ||
stageName: 'STRING_VALUE' | ||
} | ||
], | ||
value: 'STRING_VALUE' | ||
}) | ||
await service.send(cmd) | ||
tx.end() | ||
setImmediate(common.checkExternals, { | ||
t, | ||
service: 'API Gateway', | ||
operations: ['CreateApiKeyCommand'], | ||
tx | ||
}) | ||
}) | ||
}) | ||
}) |
65 changes: 65 additions & 0 deletions
65
merged/aws-sdk/tests/versioned/aws-sdk-v3/elasticache.tap.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2021 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const tap = require('tap') | ||
const utils = require('@newrelic/test-utilities') | ||
|
||
const common = require('../common') | ||
const { createResponseServer, FAKE_CREDENTIALS } = require('../aws-server-stubs') | ||
|
||
tap.test('ElastiCacheClient', (t) => { | ||
t.autoend() | ||
let helper = null | ||
let server = null | ||
let service = null | ||
let AddTagsToResourceCommand = null | ||
|
||
t.beforeEach(async () => { | ||
server = createResponseServer() | ||
await new Promise((resolve) => { | ||
server.listen(0, resolve) | ||
}) | ||
helper = utils.TestAgent.makeInstrumented() | ||
common.registerCoreInstrumentation(helper) | ||
const { ElastiCacheClient, ...lib } = require('@aws-sdk/client-elasticache') | ||
AddTagsToResourceCommand = lib.AddTagsToResourceCommand | ||
const endpoint = `http://localhost:${server.address().port}` | ||
service = new ElastiCacheClient({ | ||
credentials: FAKE_CREDENTIALS, | ||
endpoint, | ||
region: 'us-east-1' | ||
}) | ||
}) | ||
|
||
t.afterEach(() => { | ||
server.close() | ||
helper && helper.unload() | ||
}) | ||
|
||
t.test('AddTagsToResourceCommand', (t) => { | ||
helper.runInTransaction(async (tx) => { | ||
const cmd = new AddTagsToResourceCommand({ | ||
ResourceName: 'STRING_VALUE' /* required */, | ||
Tags: [ | ||
/* required */ | ||
{ | ||
Key: 'STRING_VALUE', | ||
Value: 'STRING_VALUE' | ||
} | ||
] | ||
}) | ||
await service.send(cmd) | ||
tx.end() | ||
setImmediate(common.checkExternals, { | ||
t, | ||
service: 'ElastiCache', | ||
operations: ['AddTagsToResourceCommand'], | ||
tx | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.