Skip to content

Change socket_connect_timeout to boolean, include timeout parameter in stop and wait_for_pending, c - #987

Merged
StephenWakely merged 32 commits into
masterfrom
stephen/stop_timeout
Sep 22, 2026
Merged

StephenWakely merged 32 commits into
masterfrom
stephen/stop_timeout

Conversation

@StephenWakely

@StephenWakely StephenWakely commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Requirements for Contributing to this repository

  • Fill out the template below. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.
  • The pull request must only fix one issue, or add one feature, at the time.
  • The pull request must update the test suite to demonstrate the changed functionality.
  • After you create the pull request, all status checks must be pass before a maintainer reviews your contribution. For more details, please see CONTRIBUTING.

What does this PR do?

Follow on from #986

  • Change socket_connect_timeout configuration option to a boolean socket_connect_retry
  • Add a timeout parameter to stop and wait_for_pending functions.

Description of the Change

With #986 the sender queue effectively drops older metrics from the queue when it fills up. With that in place, when the client is unable to connect to the agent we want to continue to attempt connection for as long as possible. Dropping a metric is not determined by an arbitrarily configured timeout, instead it is down to the queue to determine when a metric gets dropped. The configured queue size determines how much memory the client is willing to dedicate to holding on to unsent metrics.

The stop and wait_for_pending functions need a timeout parameter to determine how long we should wait given the possibility that we may never be able to clear the queue if the connection cannot be created.

Alternate Designs

Possible Drawbacks

Verification Process

Additional Notes

Release Notes

Review checklist (to be filled by reviewers)

  • Feature or bug fix MUST have appropriate tests (unit, integration, etc...)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have one changelog/ label attached. If applicable it should have the backward-incompatible label attached.
  • PR should not have do-not-merge/ label attached.
  • If Applicable, issue must have kind/ and severity/ labels attached at least.

@StephenWakely
StephenWakely changed the base branch from master to stephen/sender_queue September 15, 2026 13:09
_send_to_buffer/_should_flush indexed {False:..., True:...} dicts by
replay_safe, costing a bool() coercion plus a subscript on the single
hottest path in the library (once per metric, ~18k/s per benchmark
client). Measured in the benchmark image: _send_to_buffer 0.6388us on
master -> 0.7344us on this branch (+15.0%), which was ~72% of the whole
per-metric regression (+0.132us, +7.2%).

Hold the two batches as four plain attributes and branch on replay_safe
instead, and inline the size comparison so the hot path makes no call to
_should_flush (which master also paid). _should_flush is retained for the
pre-existing surface and for tests.

Also stop reading the clock in SenderQueue.get() for entries that can
never expire: the argument to _expired() is evaluated before the call, so
every bare-string (replay-safe) get() computed a monotonic() it then
discarded.

Measured after, best-of-7: _send_to_buffer -16.5%, full per-metric -8.5%.
Unit tests: 184 passed, 1 skipped with DD_ORIGIN_DETECTION_ENABLED=false
(the 45 failures otherwise seen are a pre-existing container-id artifact
of the dev host, identical before and after this change).
These four are development aids, not part of the change under review:

  tests/manual/emulate_reconnect_then_write_fails.py
  tests/manual/test_sender_queue_manual.py
  tests/manual/test_shutdown_bound.py
  tests/performance/test_sender_queue_benchmark.py

They are ad-hoc drivers and a benchmark harness rather than tests the suite
runs, and together they accounted for 1278 of the ~1970 added lines under
tests/, which buried the unit coverage that does matter. Retained locally
via .git/info/exclude (which is not committed) so they stay available for
development without shipping in the review.
tests/manual/ holds ad-hoc development drivers, not tests the suite runs, so
none of it belongs in the review. This removes the one remaining tracked file,
test_gauge_with_timestamp_aggregation.py, leaving tests/manual/ entirely
untracked. Retained on disk via .git/info/exclude, which now excludes the
whole directory so future scratch scripts there cannot be added by accident.
return True

# Condition.wait()'s return value can't be used to detect a
# timeout: on Python 2 it is always None. Track the deadline

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I guess we can leave this one out of scope, python 2 is EOL anyway.

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.

Not for datadogpy!

Comment thread datadog/dogstatsd/base.py
sender_queue_timeout=0, # type: Optional[float]
track_instance=True, # type: bool
socket_connect_timeout=DEFAULT_SOCKET_CONNECT_TIMEOUT, # type: Optional[float]
socket_connect_retry=False, # type: bool

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shall we note that in some readme or release notes?

Comment thread datadog/dogstatsd/base.py

backoff = UDS_CONNECT_RETRY_INITIAL_BACKOFF

while deadline is None or time.time() < deadline:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

so no more connect-retries?

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.

It essentially falls back on the main loop here.

Base automatically changed from stephen/sender_queue to master September 22, 2026 09:36
# Conflicts:
#	datadog/dogstatsd/base.py
#	datadog/dogstatsd/sender_queue.py
#	tests/unit/dogstatsd/test_statsd.py
@StephenWakely
StephenWakely marked this pull request as ready for review September 22, 2026 09:53
@StephenWakely
StephenWakely requested review from a team as code owners September 22, 2026 09:53
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-22T09:58:23.949364Z cfa9fb0 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cfa9fb0bfd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread datadog/dogstatsd/base.py
Comment thread datadog/dogstatsd/base.py
@StephenWakely StephenWakely added the changelog/Changed Changed features results into a major version bump label Sep 22, 2026
@github-actions github-actions Bot added the documentation Documentation related changes label Sep 22, 2026
@StephenWakely
StephenWakely merged commit 72ea9af into master Sep 22, 2026
20 checks passed
@StephenWakely
StephenWakely deleted the stephen/stop_timeout branch September 22, 2026 15:37
@StephenWakely StephenWakely mentioned this pull request Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/Changed Changed features results into a major version bump documentation Documentation related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants