-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Serverless integration for AWS Lambda - NodeJS #2865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7582e17
Changes:
amarvasekar7700 023e759
Fixed lint issue for awslambda.ts file.
amarvasekar7700 d1cf54c
1. Fixed awslambda.ts and package.json file comments.
amarvasekar7700 f3e7611
Fixed git comments on awslambda.ts and package.json files
amarvasekar7700 ab6b8f1
Changed:
amarvasekar7700 56ae197
1. Added unit test cases for AWS lambda for NodeJS with respective fi…
amarvasekar7700 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,257 @@ | ||
import { EventProcessor, Hub, Integration, Scope } from '@sentry/types'; | ||
|
||
interface AWSLambdaContext { | ||
getRemainingTimeInMillis: () => number; | ||
callbackWaitsForEmptyEventLoop: boolean; | ||
awsRequestId: string; | ||
functionName: string; | ||
functionVersion: string; | ||
invokedFunctionArn: string; | ||
logGroupName: string; | ||
logStreamName: string; | ||
} | ||
|
||
interface Module { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
amarvasekar7700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exports: any; | ||
id: string; | ||
filename: string; | ||
loaded: boolean; | ||
parent: Module | null; | ||
children: Module[]; | ||
path: string; | ||
paths: string[]; | ||
} | ||
/** | ||
* NodeJS integration | ||
* | ||
* Provides a mechanism for NodeJS | ||
* that raises an exception for handled, unhandled, timeout and similar times of error | ||
* and captures the same in Sentry Dashboard | ||
*/ | ||
export class AWSLambda implements Integration { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'AWSLambda'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public name: string = AWSLambda.id; | ||
|
||
/** | ||
* context. | ||
*/ | ||
private _awsContext: AWSLambdaContext = {} as AWSLambdaContext; | ||
|
||
/** | ||
* timeout flag. | ||
*/ | ||
private _timeoutWarning?: boolean = false; | ||
|
||
/** | ||
* flush time in milliseconds to set time for flush. | ||
*/ | ||
private _flushTimeout: number = 2000; | ||
|
||
/** | ||
* Assign Hub | ||
*/ | ||
private _hub: Hub = {} as Hub; | ||
|
||
public constructor() { | ||
// empty constructor | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { | ||
this._hub = getCurrentHub(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public providedContext( | ||
context: AWSLambdaContext, | ||
timeoutWarning: boolean = false, | ||
flushTimeout: number = 2000, | ||
): void { | ||
if (context) { | ||
this._awsContext = context; | ||
} | ||
if (flushTimeout) { | ||
this._flushTimeout = flushTimeout; | ||
} | ||
if (timeoutWarning) { | ||
this._timeoutWarning = timeoutWarning; | ||
} | ||
const flushTime = this._flushTimeout; | ||
const lambdaBootstrap: Module | undefined = require.main; | ||
|
||
if (!this._awsContext.awsRequestId || !lambdaBootstrap) { | ||
return; | ||
} | ||
/** configured time to timeout error and calculate execution time */ | ||
const configuredTimeInMilliseconds = | ||
this._awsContext.getRemainingTimeInMillis && this._awsContext.getRemainingTimeInMillis(); | ||
|
||
/** rapid runtime instance */ | ||
let rapidRuntime; | ||
let originalPostInvocationError = function(): void { | ||
return; | ||
}; | ||
if (lambdaBootstrap.children && lambdaBootstrap.children.length) { | ||
rapidRuntime = lambdaBootstrap.children[0].exports; | ||
/** handler that is invoked in case of unhandled and handled exception */ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
originalPostInvocationError = rapidRuntime.prototype.postInvocationError; | ||
} | ||
|
||
const hub = this._hub; | ||
|
||
/** | ||
* This function sets Additional Runtime Data which are displayed in Sentry Dashboard | ||
* @param scope - holds additional event information | ||
* @hidden | ||
*/ | ||
const setAdditionalRuntimeData = (scope: Scope): void => { | ||
scope.setContext('runtime', { | ||
name: 'node', | ||
version: global.process.version, | ||
}); | ||
}; | ||
|
||
/** | ||
* This function sets Additional Lambda Parameters which are displayed in Sentry Dashboard | ||
* @param scope - holds additional event information | ||
* @hidden | ||
*/ | ||
const setAdditionalLambdaParameters = (scope: Scope): void => { | ||
const remainingTimeInMillisecond: number = | ||
this._awsContext.getRemainingTimeInMillis && this._awsContext.getRemainingTimeInMillis(); | ||
const executionTime: number = configuredTimeInMilliseconds - remainingTimeInMillisecond; | ||
|
||
scope.setContext('lambda', { | ||
aws_request_id: this._awsContext.awsRequestId, | ||
function_name: this._awsContext.functionName, | ||
function_version: this._awsContext.functionVersion, | ||
invoked_function_arn: this._awsContext.invokedFunctionArn, | ||
execution_duration_in_millis: executionTime, | ||
remaining_time_in_millis: remainingTimeInMillisecond, | ||
}); | ||
}; | ||
|
||
/** | ||
* This variable use to generate cloud watch url | ||
* process.env.AWS_REGION - this parameters given the AWS region | ||
* process.env.AWS_LAMBDA_FUNCTION_NAME - this parameter provides the AWS Lambda Function name | ||
*/ | ||
const cloudwatchUrl: string = `https://${process.env.AWS_REGION}.console.aws.amazon.com/cloudwatch/home?region=${process.env.AWS_REGION}#logsV2:log-groups/log-group/$252Faws$252Flambda$252F${process.env.AWS_LAMBDA_FUNCTION_NAME}`; | ||
|
||
/** | ||
* This function sets Cloud Watch Logs data which are displayed in Sentry Dashboard | ||
* @param scope - holds additional event information | ||
* @hidden | ||
*/ | ||
const setCloudwatchLogsData = (scope: Scope): void => { | ||
amarvasekar7700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
scope.setContext('cloudwatch.logs', { | ||
log_group: this._awsContext.logGroupName, | ||
log_stream: this._awsContext.logStreamName, | ||
url: cloudwatchUrl, | ||
}); | ||
}; | ||
|
||
/** | ||
* This function sets tags which are displayed in Sentry Dashboard | ||
* @param scope - holds additional event information | ||
* @hidden | ||
*/ | ||
const setTags = (scope: Scope): void => { | ||
scope.setTag('runtime', `node${global.process.version}`); | ||
scope.setTag('transaction', this._awsContext.functionName); | ||
scope.setTag('runtime.name', 'node'); | ||
scope.setTag('server_name', process.env._AWS_XRAY_DAEMON_ADDRESS || ''); | ||
scope.setTag('url', `awslambda:///${this._awsContext.functionName}`); | ||
scope.setTag('handled', 'no'); | ||
scope.setTag('mechanism', 'awslambda'); | ||
}; | ||
|
||
// timeout warning buffer for timeout error | ||
const timeoutWarningBuffer: number = 1500; | ||
const configuredTimeInSec = Math.floor(configuredTimeInMilliseconds / 1000); | ||
const configuredTimeInMilli = configuredTimeInSec * 1000; | ||
|
||
/** check timeout flag and checking if configured Time In Milliseconds is greater than timeout Warning Buffer */ | ||
if (this._timeoutWarning === true && configuredTimeInMilliseconds > timeoutWarningBuffer) { | ||
this._awsContext.callbackWaitsForEmptyEventLoop = false; | ||
|
||
setTimeout(() => { | ||
const error = new Error( | ||
`WARNING : Function is expected to get timed out. Configured timeout duration = ${configuredTimeInSec + | ||
amarvasekar7700 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1} seconds.`, | ||
); | ||
|
||
// setting parameters in scope which will be displayed as additional data in Sentry dashboard | ||
hub.withScope((scope: Scope) => { | ||
setTags(scope); | ||
// runtime | ||
setAdditionalRuntimeData(scope); | ||
// setting the lambda parameters | ||
setAdditionalLambdaParameters(scope); | ||
// setting the cloudwatch logs parameter | ||
setCloudwatchLogsData(scope); | ||
// setting the sys.argv parameter | ||
scope.setExtra('sys.argv', process.argv); | ||
/** capturing the exception and re-directing it to the Sentry Dashboard */ | ||
hub.captureException(error); | ||
}); | ||
}, configuredTimeInMilli); | ||
void hub.getClient()?.flush(flushTime); | ||
} | ||
|
||
/** | ||
* unhandled and handled exception | ||
* @param error - holds the error captured in AWS Lambda Function | ||
* @param id - holds event id value | ||
* @param callback - callback function | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
rapidRuntime.prototype.postInvocationError = async function( | ||
error: Error, | ||
id: string, | ||
callback: () => void, | ||
): Promise<void> { | ||
// setting parameters in scope which will be displayed as additional data in Sentry dashboard | ||
hub.withScope((scope: Scope) => { | ||
setTags(scope); | ||
// runtime | ||
setAdditionalRuntimeData(scope); | ||
// setting the lambda parameters | ||
setAdditionalLambdaParameters(scope); | ||
// setting the cloudwatch logs parameter | ||
setCloudwatchLogsData(scope); | ||
// setting the sys.argv parameter | ||
scope.setExtra('sys.argv', process.argv); | ||
/** capturing the exception and re-directing it to the Sentry Dashboard */ | ||
hub.captureException(error); | ||
}); | ||
|
||
/** capturing the exception and re-directing it to the Sentry Dashboard */ | ||
const client = hub.getClient(); | ||
if (client) { | ||
await client.flush(flushTime); | ||
} | ||
|
||
/** | ||
* Here, we make sure the error has been captured by Sentry Dashboard | ||
* and then re-raised the exception | ||
*/ | ||
originalPostInvocationError.call(this, error, id, callback); | ||
}; | ||
} | ||
} | ||
|
||
export const AWSLambdaIntegration = new AWSLambda(); |
This file contains hidden or 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 hidden or 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,56 @@ | ||
/** | ||
* This function returns the client file. | ||
* @param additionalLambdaPrelude - Holds the scenario, timeoutWarning and error string. | ||
*/ | ||
|
||
export function lambdaPreludeReplacer(additionalLambdaPrelude: { | ||
scenario: string; | ||
timeoutWarning: boolean; | ||
error: string; | ||
}): string { | ||
const lambdaPrelude = ` | ||
const http = require('http'); | ||
const Sentry = require('@sentry/node'); | ||
const { AWSLambdaIntegration } = require('@sentry/integrations'); | ||
const { HTTPSTransport } = require('@sentry/node/dist/transports'); | ||
|
||
class testTransport extends HTTPSTransport { | ||
constructor() { | ||
super(...arguments); | ||
} | ||
async sendEvent(event) { | ||
console.log('Event:', JSON.stringify(event)); | ||
} | ||
} | ||
|
||
// Configure the Sentry SDK. | ||
Sentry.init({ | ||
dsn: 'https://[email protected]/5224330', | ||
transport: testTransport, | ||
integrations: [AWSLambdaIntegration], | ||
}); | ||
|
||
const lambdaBootstrap = require.main; | ||
let rapidRuntime = lambdaBootstrap.children[0].exports; | ||
rapidRuntime.prototype = { | ||
postInvocationError: function(error, id, callback) {}, | ||
}; | ||
|
||
exports.handler = ${additionalLambdaPrelude.scenario} (event,callback) => { | ||
const context = { | ||
functionVersion: '$LATEST', | ||
functionName: 'aws-lambda-unit-test', | ||
awsRequestId: '95a31fc8-7490-4474-aa7d-951aca216381', | ||
getRemainingTimeInMillis: function getRemainingTimeInMillis() { | ||
return 3000; | ||
}, | ||
}; | ||
AWSLambdaIntegration.providedContext(context, ${additionalLambdaPrelude.timeoutWarning}, 2000); | ||
|
||
${additionalLambdaPrelude.error} | ||
}; | ||
|
||
exports.handler(); | ||
`; | ||
return lambdaPrelude; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.