Skip to content
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

AWS lambda using layer not sending errors to Sentry #13663

Closed
3 tasks done
fdx-caio opened this issue Sep 11, 2024 · 8 comments
Closed
3 tasks done

AWS lambda using layer not sending errors to Sentry #13663

fdx-caio opened this issue Sep 11, 2024 · 8 comments
Labels
Package: aws-serverless Issues related to the Sentry AWS Serverless SDK

Comments

@fdx-caio
Copy link

fdx-caio commented Sep 11, 2024

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/aws-serverless

SDK Version

arn:aws:lambda:ap-southeast-2:943013980633:layer:SentryNodeServerlessSDK:271

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

No response

Steps to Reproduce

I am integrating an serveless app with Sentry and encountered the following problem: I can't seem to find any errors thrown by my lambda in Sentry discovery or elsewhere, can confirm the lambda is throwing a bad request error but not appearing on Sentry. However other logs such as transactions does seem to appear.

For context I am not using the library wrapper or any dependency as I am expecting the layer to integrate and primarily emit any logs from my lambdas.

I have followed the setup strictly from this page and my app is running in CommonJS (CJS): https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer/

Layer: arn:aws:lambda:ap-southeast-2:943013980633:layer:SentryNodeServerlessSDK:271

Environment variables:
NODE_OPTIONS: "-r @sentry/aws-serverless/awslambda-auto"
SENTRY_DSN: "***"
SENTRY_TRACES_SAMPLE_RATE: "1.0"
SENTRY_ENVIRONMENT: "dev"

Expected Result

Expecting to see errors and other types of logs other than transaction on Sentry

Actual Result

Result showing transactions from Sentry

Image

@github-actions github-actions bot added the Package: aws-serverless Issues related to the Sentry AWS Serverless SDK label Sep 11, 2024
@andreiborza
Copy link
Member

Hi @fdx-caio, thanks for writing in.

Could you show some snippets of your lambda function? Also, please link to one of these transactions.

@fdx-caio
Copy link
Author

fdx-caio commented Sep 11, 2024

This is how my lambda code looks like that I am having issues with. This being built using esbuild to CommonJS

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/threads/threads/handlers/rest/GetRole.ts
var GetRole_exports = {};
__export(GetRole_exports, {
  handler: () => handler
});
module.exports = __toCommonJS(GetRole_exports);
var handler = (_event) => {
  console.log("event", _event);
  throw new Error("Not implemented");
  return { statusCode: 204 };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  handler
});

This example works as expected (Able to throw an error and see on Sentry)

async function handler() {
    throw new Error("Not implemented");
    return { statusCode: 204 };
}
module.exports = { handler };

Link to a event transaction on Sentry as requested
https://findex.sentry.io/performance/trace/197345bb1d69174eab73335efc62e694/?environment=dev-cb&eventId=ebf1b575d90f407885cf5d28a397f267&field=title&field=event.type&field=project&field=user.display&field=timestamp&name=All+Events&project=5623962&query=&sort=-timestamp&source=discover&statsPeriod=24h&timestamp=1726093218&yAxis=count%28%29

@andreiborza
Copy link
Member

@fdx-caio there's an error in the transpiled version, where's a defined in Line 9?

@fdx-caio
Copy link
Author

@andreiborza sorry that a shouldn't be there in the first place, I updated the block of code with the right compiled version

@andreiborza
Copy link
Member

andreiborza commented Sep 12, 2024

@fdx-caio ok, so the issue is basically with shimmer and esbuild. The exported handler cannot be redefined which is needed for us to wrap the handler with Sentry.

See: evanw/esbuild#2199 (comment)

Can you try writing your handler using exports.handler instead of module.exports = { handler }; and see if that helps?

If that doesn't help, I'm afraid you'll have to initialize Sentry in your handler and use Sentry.wrapHandler as described in our docs.

@fdx-caio
Copy link
Author

fdx-caio commented Sep 12, 2024

@andreiborza Thanks for providing the esbuild context of the problem, I wasn't aware the exported handler couldn't be redefined.

I was able to make it work by throwing an error and using the module.exports like you mentioned. However when I return a response error object to the lambda instead of throwing an error the Sentry integration layer is not being able to pick up the error, would you know if there is anything I can do to make the integration know it's an error?

For context I am using ApiGateway and making my lambda handle a request and return a response to it

Example of my error response provided to lambda

{
  statusCode: 500,
  isBase64Encoded: false,
  body: '{"error":"UNHANDLED_EXCEPTION","message":"Internal server error, please contact support"}',
  headers: {
    'Content-Type': 'application/json',
    'Access-Control-Allow-Credentials': 'true',
    'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Accept,X-Requested-With,findex-invitation,awsaccesstoken',
    'Access-Control-Allow-Origin': 'http://localhost:4201'
  }
}

@andreiborza
Copy link
Member

@fdx-caio you would probably have to manually check the statuscode and pass the response to Sentry.captureException manually.

@fdx-caio
Copy link
Author

@andreiborza it worked after following your suggestions, thanks for helping me sort out all my issues. We can close this issue as my integration is working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: aws-serverless Issues related to the Sentry AWS Serverless SDK
Projects
Archived in project
Development

No branches or pull requests

2 participants