Skip to content

Commit

Permalink
Add xray traces monitoring
Browse files Browse the repository at this point in the history
Signed-off-by: jsetton <[email protected]>
  • Loading branch information
jsetton committed Nov 16, 2024
1 parent d0c402e commit 9db2a72
Show file tree
Hide file tree
Showing 5 changed files with 772 additions and 18 deletions.
11 changes: 11 additions & 0 deletions infrastructure/cfn-deployer/skill-stack.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@
"Resource": {
"Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*"
}
},
{
"Effect": "Allow",
"Action": [
"xray:PutTraceSegments",
"xray:PutTelemetryRecords"
],
"Resource": "*"
}
]
}
Expand Down Expand Up @@ -132,6 +140,9 @@
},
"MemorySize": 256,
"Timeout": 10,
"TracingConfig": {
"Mode": "Active"
},
"Environment": {
"Variables": {
"LOG_LEVEL": {
Expand Down
21 changes: 17 additions & 4 deletions lambda/alexa/smarthome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* SPDX-License-Identifier: EPL-2.0
*/

import AWSXRay from 'aws-xray-sdk';
import { AxiosError } from 'axios';
import config from '#root/config.js';
import log from '#root/log.js';
Expand All @@ -22,9 +23,10 @@ import { AlexaError, InvalidDirectiveError } from './errors.js';
/**
* Handles alexa smart home skill request
* @param {Object} request
* @param {Object} context
* @return {Promise}
*/
export const handleRequest = async (request) => {
export const handleRequest = async (request, context) => {
// Initialize directive object
const directive = new AlexaDirective(request.directive);
// Initialize openhab object
Expand All @@ -47,12 +49,23 @@ export const handleRequest = async (request) => {
}

// Get alexa response from handler function
response = await handler(directive, openhab);
await AWSXRay.captureAsyncFunc(handler.name, async (subsegment) => {
const timeoutId = setTimeout(() => subsegment.close("Timed out"), context.getRemainingTimeInMillis() - 250);
subsegment.addAnnotation('handler', handler.name);
response = await handler(directive, openhab);
clearTimeout(timeoutId);
subsegment.close();
});

// Add response context properties if directive has endpoint
if (directive.hasEndpoint) {
const properties = await directive.endpoint.getContextProperties(openhab);
response.setContextProperties(properties);
await AWSXRay.captureAsyncFunc('addContextProperties', async (subsegment) => {
const timeoutId = setTimeout(() => subsegment.close("Timed out"), context.getRemainingTimeInMillis() - 250);
const properties = await directive.endpoint.getContextProperties(openhab);
response.setContextProperties(properties);
clearTimeout(timeoutId);
subsegment.close();
});
}
} catch (error) {
// Log error if not alexa error
Expand Down
17 changes: 15 additions & 2 deletions lambda/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@
* SPDX-License-Identifier: EPL-2.0
*/

import https from 'node:https';
import AWSXRay from 'aws-xray-sdk';
import log from './log.js';
import { handleRequest } from './alexa/smarthome/index.js';

/**
* Defines skill event handler
* @param {Object} event
* @param {Object} context
* @return {Promise}
*/
export const handler = async (event) => {
export const handler = async (event, context) => {
AWSXRay.captureHTTPsGlobal(https);
AWSXRay.capturePromise();

log.info('Received event:', event);

if (event.directive?.header.payloadVersion === '3') {
return handleRequest(event);
return AWSXRay.captureAsyncFunc('handleRequest', async (subsegment) => {
const timeoutId = setTimeout(() => subsegment.close("Timed out"), context.getRemainingTimeInMillis() - 250);
subsegment.addMetadata('event', event);
const response = await handleRequest(event, context);
clearTimeout(timeoutId);
subsegment.close();
return response;
});
}

log.warn('Unsupported event:', event);
Expand Down
Loading

0 comments on commit 9db2a72

Please sign in to comment.