Skip to content

Instantly share code, notes, and snippets.

@hakanson hakanson/aws-xray-utils.js
Last active Dec 28, 2018

Embed
What would you like to do?
adds an AWS X-Ray subsegment and Metadata for an Alexa Skills Kit triggered Lambda
'use strict';
// http://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
/*
* expected usage:
let AWS = require('aws-sdk');
const AWSXRay = require('aws-xray-sdk-core');
const AWSXRayUtil = require('./aws-xray-utils');
if (process.env.AWS_XRAY_DAEMON_ADDRESS) {
// detect AWS X-Ray
AWS = AWSXRay.captureAWS(AWS);
}
const Alexa = require("alexa-sdk");
var handler = function(event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.appId = appId;
alexa.dynamoDBTableName = dynamoDBTableName;
alexa.registerHandlers(handlers, resetHandlers);
alexa.execute();
};
if (AWSXRay) {
handler = AWSXRayUtil.captureLambdaHandler(handler);
}
exports.handler = handler;
*/
const AWSXRay = require('aws-xray-sdk-core');
exports.captureLambdaHandler = function(handler) {
if (!typeof handler === 'function') {
throw new Error('handler is not a function');
}
let subsegment = null;
const wrappedFunction = function(event, context, callback) {
if (process.env.AWS_XRAY_DAEMON_ADDRESS) {
// need subsegment because of following error
// Function "addMetadata" cannot be called on an AWS Lambda segment.
// Please use a subsegment to record data.
const segmentName = process.env._HANDLER || 'index.handler';
subsegment = AWSXRay.getSegment().addNewSubsegment(segmentName);
subsegment.addMetadata('event', event);
subsegment.addMetadata('context', context);
if (context.awsRequestId) {
subsegment.addAnnotation('awsRequestId', context.awsRequestId);
console.log(`AWS X-Ray filter: Annotation.awsRequestId = "${context.awsRequestId}"`);
}
if (event && event.request && event.request.error) {
subsegment.addError(event.request.error, false);
console.log(`ERROR: ${JSON.stringify(event.request.error)}`);
}
// TODO: inspect event to ensure Alexa Skills Kit trigger
addAlexaSkillsKitAnnotations(subsegment, event);
}
try {
handler(event, context, function(err, result) {
callback(err, result);
if (subsegment) {
subsegment.addMetadata('result', result);
subsegment.close(err, false);
}
});
} catch (e) {
if (subsegment) {
subsegment.close(e, false);
}
}
};
return wrappedFunction;
};
function addAlexaSkillsKitAnnotations(subsegment, event) {
if (event.session) {
subsegment.addAnnotation('sessionId', event.session.sessionId);
if (event.session.new == true) {
console.log(`AWS X-Ray filter: Annotation.sessionId = "${event.session.sessionId}"`);
}
}
if (event.context) {
subsegment.addAnnotation('applicationId', event.context.System.application.applicationId);
subsegment.addAnnotation('userId', event.context.System.user.userId);
}
if (event.request) {
subsegment.addAnnotation('requestId', event.request.requestId);
subsegment.addAnnotation('type', event.request.type);
if (event.request.intent) {
subsegment.addAnnotation('intent', event.request.intent.name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.