Skip to content

Use single socket instance for all xray api calls.#467

Merged
purple4reina merged 2 commits intomainfrom
rey.abolofia/socket
Apr 9, 2024
Merged

Use single socket instance for all xray api calls.#467
purple4reina merged 2 commits intomainfrom
rey.abolofia/socket

Conversation

@purple4reina
Copy link
Copy Markdown
Contributor

What does this PR do?

  • Use single socket connection for all interactions with the xray api
  • Memoize xray api host/port identification
  • Ensure socket creation is done as lazily as possible

Motivation

The need for speed.

Testing Guidelines

Additional Notes

Previously, we were creating a new socket connection and closing the connection for each xray api call. When xray is enabled, and the lambda has extracted trace context from an upstream event, the xray api is called twice.

When an xray api is called, this change decreases runtime duration by somewhere between 1-5%.

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 9, 2024 17:59
except Exception as e_send:
logger.error("Error occurred submitting to xray daemon: %s", e_send)

def reset(self):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

reset is used for tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpick: can we move it to tests files?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, good call. Done.

self._host_port_tuple = self._get_xray_host_port(
os.environ.get(XrayDaemon.XRAY_DAEMON_ADDRESS, "")
)
return self._host_port_tuple
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This value is now memoized so we only need to parse the env var once.

self.assertEqual(result.sampling_priority, 1)
mock_send.assert_called_once()
(_, raw_payload), _ = mock_send.call_args
(raw_payload,), _ = mock_send.call_args
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Signature of the xray.send method changed.

@purple4reina
Copy link
Copy Markdown
Contributor Author

Confirmed metadata is still being sent to the xray api.
Screenshot 2024-04-09 at 11 31 20 AM

Copy link
Copy Markdown
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

except Exception as e_send:
logger.error("Error occurred submitting to xray daemon: %s", e_send)

def reset(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpick: can we move it to tests files?

@purple4reina purple4reina merged commit 3414cfb into main Apr 9, 2024
@purple4reina purple4reina deleted the rey.abolofia/socket branch April 9, 2024 19:43
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.

2 participants