Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created June 25, 2017 18:48
Show Gist options
  • Select an option

  • Save theburningmonk/0fb966c06b3785bc5289a895a83f027c to your computer and use it in GitHub Desktop.

Select an option

Save theburningmonk/0fb966c06b3785bc5289a895a83f027c to your computer and use it in GitHub Desktop.
service-a
// ... omitted for brevity
module.exports.handler = co.wrap(function* (event, context, callback) {
console.log(JSON.stringify(event));
console.log(JSON.stringify(context));
global.hostname = event.headers.Host;
global.accountId = event.requestContext.accountId;
global.requestId = event.requestContext.requestId;
let segment = AWSXRay.getSegment();
console.log(JSON.stringify(segment));
let n = Math.random() * 3;
segment.addMetadata('random', `${n}`); // this doesn't work
segment.addAnnotation('path', event.path); // this doesn't work
if (n <= 1) {
yield publishSNS(segment);
yield accessS3(segment);
yield accessDynamoDB(segment);
yield invokeLambda(segment);
let message = yield callServiceB(segment, n);
const response = {
statusCode: 200,
body: JSON.stringify({
message: `service-b says ${message}`
}),
};
callback(null, response);
} else if (n <= 2) {
console.log("service-a is going to call the timeout endpoint");
yield utils.request('GET', hostname, '/dev/demo/timeout');
throw new Error("timed out");
} else {
console.log("service-a is going to call the error endpoint");
yield utils.request('GET', hostname, '/dev/demo/error');
throw new Error("boom");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment