Skip to content

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

Merged
merged 13 commits into from
Aug 26, 2021
Merged

Conversation

maxday
Copy link
Contributor

@maxday maxday commented Aug 17, 2021

What does this PR do?

This PR

  • splits StatsWriter and its subclasses in separate files so only the relevant one is included during cold start
  • does not import datadog.api if the extension is used which also speed up the cold start

Motivation

Cold start improvements

Testing Guidelines

Unit + Integration tests

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@maxday maxday requested a review from a team as a code owner August 17, 2021 19:18
Copy link
Contributor

@astuyve astuyve left a 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?

@maxday
Copy link
Contributor Author

maxday commented Aug 17, 2021

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 🤞 )

@tianchu
Copy link
Collaborator

tianchu commented Aug 17, 2021

Should we include something we discussed about #147 here? Seems touching similar code path.

def stop(self):
self.thread_stats.stop()


def init_lambda_stats():
Copy link
Collaborator

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?

Copy link
Collaborator

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?

Copy link
Contributor Author

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

)
logger.debug("Setting DATADOG_HOST to %s", api._api_host)
# Set API Key only if extension is not here
if not should_use_extension:
Copy link
Collaborator

@tianchu tianchu Aug 17, 2021

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)

Copy link
Contributor Author

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

@maxday maxday requested a review from tianchu August 24, 2021 20:45
write_metric_point_to_stdout(metric_name, value, timestamp=timestamp, tags=tags)
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)
Copy link
Contributor Author

@maxday maxday Aug 26, 2021

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

@maxday maxday merged commit 4c48e10 into main Aug 26, 2021
@maxday maxday deleted the maxday/api-init-refacto branch August 26, 2021 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants