-
Notifications
You must be signed in to change notification settings - Fork 46
Refactoring to allow lazy loading of datadog.api #163
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
Conversation
astuyve
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. Are there other places we're loading boto3 or stats classes, where we could delay it if possible?
not that I am aware of and I don't see any trace of boto3 loading in the flamegraph (except in aws-xray-sdk but I've opened a PR here aws/aws-xray-sdk-python#312 🤞 ) |
|
Should we include something we discussed about #147 here? Seems touching similar code path. |
datadog_lambda/metric.py
Outdated
| self.thread_stats.stop() | ||
|
|
||
|
|
||
| def init_lambda_stats(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed before, might make sense to call init_lambda_stats() in the __init__ method of the wrapper? So it only creates the writer on cold start?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively you could check lambda_stats is None before creating a new writer? But I think it's probably the best that we have both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reverted the PR #147 as it had no effect on /hello route
datadog_lambda/metric.py
Outdated
| ) | ||
| logger.debug("Setting DATADOG_HOST to %s", api._api_host) | ||
| # Set API Key only if extension is not here | ||
| if not should_use_extension: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should put them into a function? and call it in the wrapper's __init__ method? I think we can further reduce the scope here, the API key is ONLY needed when DD_FLUSH_TO_LOG is false (we send metrics synchronously from the library)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the condition
| else: | ||
| logger.debug("Sending metric %s to Datadog via lambda layer", metric_name) | ||
| if should_use_extension: | ||
| lambda_stats.distribution(metric_name, value, tags=tags, timestamp=timestamp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am usually not a big fan of having two if branches which execute the same code but I think for this case it makes the code easier to read
What does this PR do?
This PR
Motivation
Cold start improvements
Testing Guidelines
Unit + Integration tests
Types of Changes
Check all that apply