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

request not captured in @sentry/aws-serverless + express #14239

Open
3 tasks done
relm923 opened this issue Nov 12, 2024 · 13 comments
Open
3 tasks done

request not captured in @sentry/aws-serverless + express #14239

relm923 opened this issue Nov 12, 2024 · 13 comments
Labels
Package: aws-serverless Issues related to the Sentry AWS Serverless SDK

Comments

@relm923
Copy link

relm923 commented Nov 12, 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

8.37.1

Framework Version

AWS Lambda + Express 4.21.1

Link to Sentry event

https://patina-health.sentry.io/issues/5996169896?project=6044571

Reproduction Example/SDK Setup

import * as Sentry from '@sentry/aws-serverless';

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  environment: 'development',
  integrations: [Sentry.expressIntegration()],
  tracesSampleRate: 1.0,
  profilesSampleRate: 1.0,
});

import * as Sentry from '@sentry/aws-serverless';
import express from 'express';
import serverless from 'serverless-http';

const app = express();

app.get('/debug-sentry', (req, res) => {
  throw new Error('My first Sentry error!');
});

Sentry.setupExpressErrorHandler(app);

export const handler = Sentry.wrapHandler(serverless(app));

Steps to Reproduce

Run the above code ideally deployed to AWS. Invoke with payload similar to:

{
  "headers": { "content-type": "application/json" },
  "httpMethod": "GET",
  "path": "/debug-sentry"
}

Error is reported to Sentry but the request information is not included

Expected Result

Sentry event should include the request information.

This worked correctly with the same setup on Sentry 7

Actual Result

Sentry event does include aws.cloudwatch.logs and aws.lambda but no request information

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Nov 12, 2024
@github-actions github-actions bot added the Package: aws-serverless Issues related to the Sentry AWS Serverless SDK label Nov 12, 2024
@chargome
Copy link
Member

Hey @relm923, thanks for reaching out. integrations: [Sentry.expressIntegration()], is not needed anymore in v8, you can safely remove that. Theoretically the requestDataIntegration should be enabled by default and include your request data. So can you please remove the integrations field in your init call and if it still not works add debug: true and check the output?

The latest docs on express can be found here.

@relm923
Copy link
Author

relm923 commented Nov 12, 2024

Hi @chargome removing integrations does not appear to enable Express

import * as Sentry from '@sentry/aws-serverless';

Sentry.init({
  debug: true,
  dsn: process.env.SENTRY_DSN,
  environment: 'development',
  tracesSampleRate: 1.0,
  profilesSampleRate: 1.0,
});

Sentry Logger [log]: Integration installed: InboundFilters
Sentry Logger [log]: Integration installed: FunctionToString
Sentry Logger [log]: Integration installed: LinkedErrors
Sentry Logger [log]: Integration installed: RequestData
Sentry Logger [log]: Integration installed: Console
Sentry Logger [log]: Integration installed: Http
Sentry Logger [log]: Integration installed: NodeFetch
Sentry Logger [log]: Integration installed: OnUncaughtException
Sentry Logger [log]: Integration installed: OnUnhandledRejection
Sentry Logger [log]: Integration installed: ContextLines
Sentry Logger [log]: Integration installed: LocalVariablesAsync
Sentry Logger [log]: Integration installed: Context
Sentry Logger [log]: Integration installed: ProcessAndThreadBreadcrumbs
Sentry Logger [log]: Integration installed: Modules
Sentry Logger [log]: Integration installed: Aws
Sentry Logger [log]: Integration installed: AwsLambda
Sentry Logger [log]: Running in CommonJS mode.
import * as Sentry from '@sentry/aws-serverless';

Sentry.init({
  debug: true,
  dsn: process.env.SENTRY_DSN,
  environment: 'development',
  integrations: [Sentry.expressIntegration()],
  tracesSampleRate: 1.0,
  profilesSampleRate: 1.0,
});

Sentry Logger [log]: Integration installed: InboundFilters
Sentry Logger [log]: Integration installed: FunctionToString
Sentry Logger [log]: Integration installed: LinkedErrors
Sentry Logger [log]: Integration installed: RequestData
Sentry Logger [log]: Integration installed: Console
Sentry Logger [log]: Integration installed: Http
Sentry Logger [log]: Integration installed: NodeFetch
Sentry Logger [log]: Integration installed: OnUncaughtException
Sentry Logger [log]: Integration installed: OnUnhandledRejection
Sentry Logger [log]: Integration installed: ContextLines
Sentry Logger [log]: Integration installed: LocalVariablesAsync
Sentry Logger [log]: Integration installed: Context
Sentry Logger [log]: Integration installed: ProcessAndThreadBreadcrumbs
Sentry Logger [log]: Integration installed: Modules
Sentry Logger [log]: Integration installed: Aws
Sentry Logger [log]: Integration installed: AwsLambda
Sentry Logger [log]: Integration installed: Express
Sentry Logger [log]: Running in CommonJS mode.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Nov 12, 2024
@chargome
Copy link
Member

You won't need this integration since it gets auto-instrumented out of the box. Does the setup work like this?

@relm923
Copy link
Author

relm923 commented Nov 12, 2024

It does not work. Without the integrations param this appears in the logs

[Sentry] express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.

Looking at the code it looks like aws-serverless not auto-instrument performance/tracing integrations

return [...getDefaultIntegrationsWithoutPerformance(), awsIntegration(), awsLambdaIntegration()];

https://github.com/getsentry/sentry-javascript/blob/develop/packages/node/src/sdk/index.ts#L57

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Nov 12, 2024
@chargome
Copy link
Member

@relm923 some questions:

  • How are you creating your lambda? e.g. are you uploading your code alongside your node modules? Are you transpiling your code to cjs or is the code in the issue exactly the code that is executed in your lambda?
  • What kind of request information would you expect to see in your case?

@relm923
Copy link
Author

relm923 commented Nov 12, 2024

How are you creating your lambda? e.g. are you uploading your code alongside your node modules? Are you transpiling your code to cjs or is the code in the issue exactly the code that is executed in your lambda?

Transpiring with ESBuild to CJS and explicitly including express outside of bundling to ensure OpenTelemetry can inject

I'm able to reproduce the same behavior locally without any bundling as well

What kind of request information would you expect to see in your case?

Everything from RequestData

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Nov 12, 2024
@chargome
Copy link
Member

got it, maybe the issue is related to #13746 then, wdyt @mydea ?

@mydea
Copy link
Member

mydea commented Nov 13, 2024

Yeah, I guess it is true that we do not set the request for AWS handlers. We will look into this and see if we can add this data to the events!

@mydea mydea changed the title aws-serverless + express + Sentry 8 breaks inbound request tracking request not captured in @sentry/aws-serverless + express Nov 13, 2024
@mydea
Copy link
Member

mydea commented Nov 13, 2024

One question: You wrote this:

Run the above code ideally deployed to AWS. Invoke with payload similar to:

{
  "headers": { "content-type": "application/json" },
  "httpMethod": "GET",
  "path": "/debug-sentry"
}

Is this some kind of standard, and if so, where is this defined? We get an event and context passed to the handler, looking at the types I do not see an obvious/standard way to access this data 🤔 (I am not an AWS expert though, so def. possible I am missing something there). I found some posts indicating we may find something at event.requestContext.http but not sure if this is standardized 🤔 cc @andreiborza & @Lms24

I will put this in our backlog generally, as we are pretty swamped with stuff right now. PRs are also welcome if you want to take a stab yourself to fix this - you'd have to put code into the wrapHandler function similar to what we already do with enhanceScopeWithEnvironmentData, but with something like scope.setSDKProcessingMetadata({ request: { headers, method } }) or similar.

@relm923
Copy link
Author

relm923 commented Nov 13, 2024

Generally for AWS Lambda event and context can basically be anything. For our setup we have APIGateway in front of our Lambdas so the inputs follow the API Gateway spec. There's no guarantee that all Lambdas will use this pattern though.

I would expect the raw request/input to the lambda (event & context) and the express request/input (headers, method, etc) both be capture by Sentry. In v7 the express was automatically captured when using the Express integration

I would also suggest updating the v8 docs as aws-serverless does NOT auto-instrument any of the performance/tracing integrations

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Nov 13, 2024
@mydea
Copy link
Member

mydea commented Nov 14, 2024

I think it makes sense to extract a proper request from the API Gateway spec, if it is there!

Additionally, we could also store the event & context via Sentry.setContext(), I suppose - cc @andreiborza / @Lms24 any thoughts on this? This could also be opt-in in the SDK, if we are concerned about PII etc.

I would also suggest updating the v8 docs as aws-serverless does NOT auto-instrument any of the performance/tracing integrations

@relm923 What exactly do you mean? Do you mean that most auto instrumentation is not enabled by default in AWS? If yes, you are right - we should probably be more explicit on this in the setup docs! Do you mind opening a separate issue for this here: https://github.com/getsentry/sentry-docs so we can track this separately? Thank you!

@andreiborza
Copy link
Member

No concerns from my side, I think an opt-in option to capture event and context data makes sense.

@relm923
Copy link
Author

relm923 commented Nov 14, 2024

@relm923 What exactly do you mean? Do you mean that most auto instrumentation is not enabled by default in AWS?

Yes @sentry/node automatically enables performance integrations (express, pg, etc) if any of the tracing options are enabled. @sentry/aws-serverless only enables these integrations if explicitly added via the integrations option. This led to some confusion above with @chargome and I've seen similar confusion in other GH issues

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
Status: No status
Development

No branches or pull requests

4 participants