perf(datadog): reuse one UDP socket per batch and coalesce metrics#13653
Open
shreemaan-abhishek wants to merge 2 commits into
Open
perf(datadog): reuse one UDP socket per batch and coalesce metrics#13653shreemaan-abhishek wants to merge 2 commits into
shreemaan-abhishek wants to merge 2 commits into
Conversation
The datadog plugin opened a fresh UDP socket for every entry in a flushed batch and sent 6 separate datagrams per entry (request.counter, request.latency, upstream.latency, apisix.latency, ingress.size, egress.size). For a batch of N entries this cost N socket setups and 6N sendto syscalls. Reuse a single socket across the whole batch and coalesce each entry's metrics into one newline-delimited DogStatsD datagram, cutting the per-batch work to 1 socket setup and N sends. Newline-delimited multi-metric packets are standard DogStatsD and parsed by the agent. The test mock now splits received datagrams on newlines, as a real DogStatsD server does, so existing metric assertions still hold. Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR improves the performance characteristics of the datadog plugin’s batch flush path by reusing a single UDP socket for the whole batch and by coalescing each entry’s metrics into one newline-delimited DogStatsD datagram (with the test mock updated to parse newline-delimited datagrams like a real DogStatsD server).
Changes:
- Reuse one UDP socket per batch flush rather than creating/closing a socket per entry.
- Coalesce per-entry metrics into a single newline-delimited DogStatsD payload (1 datagram per entry).
- Update the layer4 DogStatsD mock to split incoming datagrams on newlines for existing per-metric assertions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| t/lib/mock_layer4.lua | Logs each newline-delimited metric within a received DogStatsD datagram to preserve existing per-metric test assertions. |
| apisix/plugins/datadog.lua | Builds multi-metric DogStatsD payloads per entry and reuses a single UDP socket across the batch flush. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
198
to
205
| for i = 1, #entries do | ||
| local ok, err = send_metric_over_udp(entries[i], metadata) | ||
| if not ok then | ||
| return false, err, i | ||
| -- coalesce the per-request metrics into one datagram | ||
| local send_ok, send_err = sock:send(build_metrics(entries[i], metadata)) | ||
| if not send_ok then | ||
| sock:close() | ||
| return false, "failed to send metrics to dogstatsd server: host[" .. host | ||
| .. "] port[" .. tostring(port) .. "] err: " .. send_err, i | ||
| end |
Comment on lines
+191
to
+196
| local sock = udp() | ||
| local ok, err = sock:setpeername(host, port) | ||
| if not ok then | ||
| return false, "failed to connect to UDP server: host[" .. host | ||
| .. "] port[" .. tostring(port) .. "] err: " .. err | ||
| end |
Collapse the send loop to a single exit so there is one checked sock:close() instead of an unchecked close on the failure path plus a checked one at the end, matching the existing udp-logger idiom. Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
datadogplugin opened a fresh UDP socket for every entry in a flushed batch and sent 6 separate datagrams per entry (request.counter,request.latency,upstream.latency,apisix.latency,ingress.size,egress.size). For a batch of N entries this cost N socket setups and 6Nsendtosyscalls in the batch-processor flush.This PR:
Per-batch work drops from
N sockets + 6N sendsto1 socket + N sends. Newline-delimited multi-metric packets are standard DogStatsD and are parsed by the Datadog agent, so this is wire-compatible with existing receivers. The plugin schema and the metrics emitted are unchanged.The test mock (
t/lib/mock_layer4.luadogstatsd()) now splits received datagrams on newlines, as a real DogStatsD server does, so the existing per-metric assertions int/plugin/datadog.tcontinue to hold without changes.Checklist
t/plugin/datadog.tassertions cover the emitted metrics; mock updated to reflect coalesced framing)