Skip to content

Instantly share code, notes, and snippets.

@thomasmichaelwallace
Created June 28, 2018 14:55
Show Gist options
  • Save thomasmichaelwallace/a3be1b5df81f9a02cc92e691ba3daac1 to your computer and use it in GitHub Desktop.
Save thomasmichaelwallace/a3be1b5df81f9a02cc92e691ba3daac1 to your computer and use it in GitHub Desktop.
Replace AWS with captured X-Ray
// ensures all aws-sdk calls are based on a singleton, captured using the singleton xray-sdk.
import aws from 'aws-sdk';
import { captureAWS } from 'aws-xray-sdk';
const captured = captureAWS(aws);
export default captured;
const xrayEnabled = (() => {
let flag = process.env.COMPILE_WITH_XRAY === '*';
try {
const { tracing } = slsw.lib.serverless.service.provider; // support serverless-plugin-tracing
flag = flag || (tracing === true) || tracing === 'true';
} catch (_) { /* do nothing */ }
return flag;
})();
const bootstrapXray = new webpack.NormalModuleReplacementPlugin(/^aws-sdk$/, (r) => {
const bootstrap = path.join(__dirname, 'path-to/bootstrap-xray.js');
if (r.contextInfo.issuer === bootstrap) { return; }
console.log(`xray: re-wired aws-sdk for ${r.contextInfo.issuer}`); // eslint-disable-line no-console
const dependency = r.dependencies.find(d => d.request === 'aws-sdk');
/* eslint-disable no-param-reassign */
r.request = bootstrap; // eslint-disable-line no-param-reassign
dependency.request = bootstrap; // eslint-disable-line no-param-reassign
dependency.userRequest = bootstrap; // eslint-disable-line no-param-reassign
/* eslint-enable no-param-reassign */
});
module.exports = {
// your config,
plugins: xrayEnabled ? xrayEnabled ? [bootstrapXray] : [],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment