@sentry/serverless": "^5.26.0
5.26.0
0
Good afternoon,
After new release:
The sentry service works, But I'm getting this message in Amazon CloudWatch:
ERROR Invoke Error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'finish' of undefined",
"stack": [
"TypeError: Cannot read property 'finish' of undefined",
" at Runtime.eval [as handler] (webpack://rpc-analise-input/./node_modules/@sentry/serverless/esm/awslambda.js?:216:25)",
" at processTicksAndRejections (internal/process/task_queues.js:97:5)"
]
}
Sentry common file
import * as sentry from '@sentry/serverless';
sentry.AWSLambda.init({dsn: 'https://somedsn',
});
export default sentry;
Lambda file
import sentry from '../common/sentry';
const handler = async (event) => {}
exports.handler = sentry.AWSLambda.wrapHandler(handler);
Error location
const transaction = startTransaction({
name: context.functionName,
op: 'awslambda.handler',
});
Line 170: transaction.finish();
I have the same issue with "5.26.0" of "@sentry/serverless", I have to use "5.25.0" for now.
We have the same issue with 5.26.0, but not with 5.25.0.
I tried to create a test to show it, but could not reproduce it in a test case.
I am in the process of switching from our old @sentry/node
based AWS Lambda wrapper with custom flushing to the official @sentry/serverless
package. I ran into this issue with 5.27.1 and just like everyone else downgrading to 5.25.0 fixed it. So I guess I'll stick to that version for the foreseeable future.
Not really related to this issue, but I noticed that the package does not have all TypeScript dependencies set properly so adding the package caused compilation to break until I manually added @google-cloud/functions-framework
and @types/express
packages.
Just to give more details: I saw this when using serverless-offline
to run the Lambda offline _without_ calling AWSLambda.init
. I didn't test if it would have worked inside AWS with the init
because I want the offline handler code to be the same as the online handler.
Try adding import '@sentry/tracing';
where in you call startTransaction
Downgrading to 5.25.0 is resulting in o.init is not a function
... however, I found you need to change to Sentry.init({...
in versions prior to 5.26.0
After doing some digging, I found that the AWSLambda
's handler wrapper makes use of the startTransaction
method, which is dynamically registered by @sentry/tracing
as an "extension" of Sentry's core functionality. My guess is that either the docs are missing a step to correctly import and enable this extension or webpack's tree-shaking (in my case at least) is discarding the tracing code since it's not used _directly_ but only through the extension mechanism.
In any case, I was able to get this working without the error by adding the following before the init
call:
import * as SentryTracing from '@sentry/tracing';
SentryTracing.addExtensionMethods();
I am also experiencing this with "@sentry/serverless": "^5.27.4"
, the workaround mentioned by @asermax fixed it, ty!
Any update on this? Pretty old issue and rather serious to not be able to use this lib in its most basic use case according to the manual (without a hack).
Yeah it seems to be related to webpack tree-shaking feature or something else webpack-related.
Could you please provide a webpack configuration that reproduces the issue? Knowing the webpack version and the versions of plugins will be helpful too.
Some time ago I experimented with webpack + sentry on aws lambda and everything worked fine so I definitely miss the point how it could be broken.
We have seen the issue but don't use webpack at all. We are using typescript for the lambda functions but no bundler.
@p0wl
what build configuration do you use?
we are deploying using aws-cdk, which zips a folder and uploads it as lambda asset. Our typescript config is the aws-cdk standard.
tsconfig:
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": [
"es2018"
],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"skipLibCheck": true,
"typeRoots": [
"./node_modules/@types"
]
},
"exclude": [
"cdk.out"
]
}
and here is the change how we tried to switch from serverless-sentry-lib
to @sentry/serverless
:
--- a/resources/lambda-base/lib/withSentry.ts
+++ b/resources/lambda-base/lib/withSentry.ts
@@ -1,4 +1,14 @@
-const withSentry = require('serverless-sentry-lib').default;
+import * as Sentry from '@sentry/serverless';
+// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+// @ts-ignore
+const withSentry: (handler: any, handlerOptions?: any | undefined) => any =
+ Sentry.AWSLambda.wrapHandler;
+
+Sentry.init({
+ dsn: process.env.SENTRY_DSN,
+});
+
+export { Sentry };
export default withSentry;
Can confirm this issue also happens with regular Webpack builds, currently on @sentry/[email protected]
, using [email protected]
, [email protected]
, [email protected]
& the following webpack.config.js
:
const slsw = require('serverless-webpack');
const webpack = require('webpack');
const { stage } = slsw.lib.serverless.service.provider;
const { isLocal } = slsw.lib.webpack;
module.exports = {
entry: slsw.lib.entries,
target: 'node',
context: process.env.PWD,
// Disable verbose logs
stats: stage === 'prod' ? 'errors-only' : 'normal',
devtool: 'source-map',
externals: [ 'aws-sdk' ],
mode: isLocal ? 'development' : 'production',
performance: {
hints: false,
},
node: {
__dirname: false,
},
resolve: {
symlinks: false,
},
optimization: isLocal
? { splitChunks: false, removeEmptyChunks: false, removeAvailableModules: false }
: { minimize: false },
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(stage === 'prod' ? 'production' : 'development'),
'process.env.WEBPACK_ENV': JSON.stringify('WEBPACK'),
}),
],
};
Producing a HTTP 502 Bad Gateway
with API-Gateway, and the following log in Cloudwatch:
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'finish' of undefined",
"stack": [
"TypeError: Cannot read property 'finish' of undefined",
" at Runtime.handler (/var/task/index.js:23207:25)"
]
}
Can confirm @asermax's fix appears to resolve this issue, my guess is adding SentryTracing.addExtensionMethods()
stops Webpack from stripping out "unnecessary" dependencies:
const Sentry = require('@sentry/serverless');
const SentryTracing = require('@sentry/tracing');
SentryTracing.addExtensionMethods();
Sentry.AWSLambda.init({
// dsn: process.env.SENTRY_DSN, // Populated by env var
tracesSampleRate: 1.0,
});
Most helpful comment
After doing some digging, I found that the
AWSLambda
's handler wrapper makes use of thestartTransaction
method, which is dynamically registered by@sentry/tracing
as an "extension" of Sentry's core functionality. My guess is that either the docs are missing a step to correctly import and enable this extension or webpack's tree-shaking (in my case at least) is discarding the tracing code since it's not used _directly_ but only through the extension mechanism.In any case, I was able to get this working without the error by adding the following before the
init
call: