Skip to content

Reduce allocations during event parsing. #463

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 3 commits into from
Apr 5, 2024

Conversation

purple4reina
Copy link
Contributor

What does this PR do?

Reduces allocations in all functions in the datadog_lambda/tracing.py file.

Motivation

It's faster.

Testing Guidelines

Additional Notes

Screenshot 2024-04-02 at 3 03 39 PM

Before and after comparison. Note this only compares the parsing of lambda function urls. Therefore, other event types could see more/less performance improvements.

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)

@purple4reina purple4reina requested a review from a team as a code owner April 2, 2024 22:05
@@ -66,6 +66,8 @@

telemetry_writer.enable()

is_lambda_context = os.environ.get(XrayDaemon.FUNCTION_NAME_HEADER_NAME) != ""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic used to be in a function. But since the function name header won't change between invocations, we can parse it just once at the global level.

Copy link
Contributor

@joeyzhao2018 joeyzhao2018 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Moreover, I actually overlooked allocations such as get("key", _some_default). We should probably summarize these things as a guide and add it to the PR template.

Comment on lines 255 to 268
dd_json_data = None
dd_json_data_type = dd_payload.get("Type", dd_payload.get("dataType", ""))
dd_json_data_type = dd_payload.get("Type")
if dd_json_data_type is None:
dd_json_data_type = dd_payload.get("dataType")
if dd_json_data_type == "Binary":
dd_json_data = dd_payload.get(
"binaryValue",
dd_payload.get("Value", r"{}"),
)
dd_json_data = base64.b64decode(dd_json_data)
dd_json_data = dd_payload.get("binaryValue")
if dd_json_data is None:
dd_json_data = dd_payload.get("Value")
if dd_json_data:
dd_json_data = base64.b64decode(dd_json_data)
elif dd_json_data_type == "String":
dd_json_data = dd_payload.get(
"stringValue",
dd_payload.get("Value", r"{}"),
)
dd_json_data = dd_payload.get("stringValue")
if dd_json_data is None:
dd_json_data = dd_payload.get("Value")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I like that we are reducing allocations, this is really hard to read 😢 maybe in the future when we split the files, this would make more sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes agreed. This is difficult to read. I'll take a stab at improving it a bit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another PR is fine, I guess when this is separated, this can be much easier! Not a blocking comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@duncanista Okay, I made some updates. Let me know if this is any better for you.

@purple4reina purple4reina force-pushed the rey.abolofia/trace-clean branch from 28ad948 to 87242a8 Compare April 4, 2024 21:28
@purple4reina purple4reina merged commit 36610c1 into main Apr 5, 2024
51 checks passed
@purple4reina purple4reina deleted the rey.abolofia/trace-clean branch April 5, 2024 16:15
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