getsentry / sentry-python

The official Python SDK for Sentry.io
https://sentry.io/for/python/
MIT License
1.93k stars 509 forks source link

Slow Initialization Time for Sentry SDK in AWS Lambda #3763

Open kalyan-koneru opened 1 week ago

kalyan-koneru commented 1 week ago

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.18.0

Steps to Reproduce

While implementing a simple AWS Lambda function, I observed significant delays related to Sentry SDK initialization. Specifically:

  1. import sentry_sdk takes approximately ~ 300 ms.
  2. sentry_sdk.init takes an additional 25SampleSentrySdkFunction.zip 0 ms.

These delays contribute to an increased cold start time for AWS Lambda functions, which could impact performance in latency-sensitive applications.

I have attached the lambda function I used to test the performance

Expected Result

Expect the sentry initialization time to be under 50ms

Actual Result

Sentry initialization time is over 500ms

Elapsed time import sentry_sdk: 304 ms Elapsed time sentry_sdk.init: 257 ms

szokeasaurusrex commented 1 week ago

@kalyan-koneru thank you for the feedback.

First of all, when measuring the time it takes for the Sentry SDK to initialize, you should use time.process_time_ns() rather than time.time().

The problem with time.time() is that it measures the entire elapsed time of the SDK's initialization, including time where the process initializing the Sentry SDK is sleeping. On the other hand, time.process_time_ns() excludes time when the process is sleeping. So, time.process_time_ns() will give you a more accurate idea of how much time it actually takes the Sentry SDK to initialize.

I don't find it that surprising though that there is some delay to initializing the Sentry SDK. We need to do quite a bit of work to instrument your application properly with the SDK, so it is no surprise that this does add a delay.

Did you notice a sudden performance regression from a previous version of the Sentry SDK, or has it always been like this for you?

kalyan-koneru commented 1 week ago

@szokeasaurusrex

Thank you for the clarification on using time.process_time_ns() to measure initialization time more accurately. We were indeed using time.time() and will switch to time.process_time_ns() to get a clearer view of the SDK's active processing time.

Regarding performance, we haven't specifically checked for regressions in the current version of the Sentry SDK. This issue came to our attention while investigating cold startup times in our Python application, where we observed the Sentry SDK initialization taking around 500ms. Do you consider this a reasonable performance expectation, or are there optimizations we could look into?

szokeasaurusrex commented 1 week ago

@kalyan-koneru Could you first try timing the import and initialization with time.process_time_ns()?

It's hard to tell whether a 500ms duration measured with time.time() is reasonable because the duration with time.time() depends on many external factors that are specific to your system, and which could even vary between individual runs of the program.