Add synchronous mode for serverless environments#227
Open
duncanista wants to merge 2 commits intomasterfrom
Open
Add synchronous mode for serverless environments#227duncanista wants to merge 2 commits intomasterfrom
duncanista wants to merge 2 commits intomasterfrom
Conversation
In AWS Lambda and similar serverless environments, background threads get frozen when the handler returns, causing buffered metrics to be lost. This adds a SynchronousMode option that bypasses the async queue and sends metrics directly on the calling thread. Changes: - Extract IStatsBufferize interface from StatsBufferize - Add SynchronousSender that routes metrics synchronously - Add StatsdConfig.SynchronousMode property (default false) - Suppress telemetry background timer in sync mode - Add unit tests, builder tests, and integration tests
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a138a5f742
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
In the async path, AsynchronousWorker.Dequeue() catches exceptions from Route/Flush and forwards them to optionalExceptionHandler. The sync path was letting exceptions propagate directly into user code, which could crash Lambda handlers on transport failures or serialization errors. Wrap Send and Flush in try-catch to match async behavior.
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.
Summary
StatsdConfig.SynchronousModeoption that bypasses the async background worker and sends metrics directly on the calling threadIStatsBufferizeinterface fromStatsBufferizeto enable swapping async/sync implementationsMotivation
In AWS Lambda and similar serverless environments, the runtime freezes the sandbox immediately after the handler returns. The DogStatsD client processes metrics on a background thread via
AsynchronousWorker, so buffered metrics can be lost when the freeze hits. The existingFlush()method has a hardcoded 3-second timeout and still relies on a background thread to drain the queue — making it fundamentally unreliable in serverless contexts.SynchronousModeeliminates this class of bugs entirely: no background threads, no async queue, no race with the sandbox freeze. Metrics are routed and serialized on the calling thread, batched into efficient UDP packets via the existingBufferBuilder, and the user callsFlush()at the end of their handler to send any remaining partial buffer.Usage
Test plan
SynchronousSender(pool behavior, send/flush, batching, auto-flush on full buffer, dispose triggers flush, thread safety, empty flush)CreateStatsBufferize, passessynchronousModeto telemetry)