-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add an Aggregator for Traces and Stats #534
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1b59cd1
add `start_trace_agent`
duncanista f8766a1
rename methods in flushers
duncanista c3c58b0
add `prost` crate
duncanista 0775538
fix logs aggregator
duncanista ad83d28
add the `MAX_CONTENT_SIZE_BYTES` for traces
duncanista a9e77ea
add `MessageAggregator`
duncanista 96fc6ba
refactor `StatsFlusher`
duncanista 0e25f63
use `StatsAggregator` accordingly
duncanista c3afbb4
add `TraceAggregator`
duncanista ecf783a
use `TraceAggregator` in `TraceFlusher`
duncanista f03cfbe
modify `TraceAgent`
duncanista 588585e
remove generic for `StatsAggregator`
duncanista 683bd2c
add unit tests for `StatsAggregator`
duncanista 8575639
add unit tests for `TraceAggregator`
duncanista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| use datadog_trace_protobuf::pb::ClientStatsPayload; | ||
| use prost::Message; | ||
| use std::collections::VecDeque; | ||
|
|
||
| /// Maximum number of entries in a stat payload. | ||
| /// | ||
| /// <https://github.com/DataDog/datadog-agent/blob/996dd54337908a6511948fabd2a41420ba919a8b/pkg/trace/writer/stats.go#L35-L41> | ||
| // const MAX_BATCH_ENTRIES_SIZE: usize = 4000; | ||
|
|
||
| /// Aproximate size an entry in a stat payload occupies | ||
| /// | ||
| /// <https://github.com/DataDog/datadog-agent/blob/996dd54337908a6511948fabd2a41420ba919a8b/pkg/trace/writer/stats.go#L33-L35> | ||
| // const MAX_ENTRY_SIZE_BYTES: usize = 375; | ||
|
|
||
| /// Maximum content size per payload in compressed bytes, | ||
| /// | ||
| /// <https://github.com/DataDog/datadog-agent/blob/996dd54337908a6511948fabd2a41420ba919a8b/pkg/trace/writer/stats.go#L35-L41> | ||
| const MAX_CONTENT_SIZE_BYTES: usize = 3 * 1024 * 1024; // ~3MB | ||
|
|
||
| #[allow(clippy::module_name_repetitions)] | ||
| pub struct StatsAggregator { | ||
| queue: VecDeque<ClientStatsPayload>, | ||
| max_content_size_bytes: usize, | ||
| buffer: Vec<ClientStatsPayload>, | ||
| } | ||
|
|
||
| impl Default for StatsAggregator { | ||
| fn default() -> Self { | ||
| StatsAggregator { | ||
| queue: VecDeque::new(), | ||
| max_content_size_bytes: MAX_CONTENT_SIZE_BYTES, | ||
| buffer: Vec::with_capacity(MAX_CONTENT_SIZE_BYTES), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl StatsAggregator { | ||
| #[allow(dead_code)] | ||
| #[allow(clippy::must_use_candidate)] | ||
| pub fn new(max_content_size_bytes: usize) -> Self { | ||
duncanista marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| StatsAggregator { | ||
| queue: VecDeque::new(), | ||
| max_content_size_bytes, | ||
| buffer: Vec::with_capacity(max_content_size_bytes), | ||
| } | ||
| } | ||
|
|
||
| pub fn add(&mut self, payload: ClientStatsPayload) { | ||
| self.queue.push_back(payload); | ||
| } | ||
|
|
||
| pub fn get_batch(&mut self) -> Vec<ClientStatsPayload> { | ||
| let mut batch_size = 0; | ||
|
|
||
| // Fill the batch | ||
| while batch_size < self.max_content_size_bytes { | ||
| if let Some(payload) = self.queue.pop_front() { | ||
| let payload_size = payload.encoded_len(); | ||
|
|
||
| // Put stats back in the queue | ||
| if batch_size + payload_size > self.max_content_size_bytes { | ||
| self.queue.push_front(payload); | ||
| break; | ||
| } | ||
| batch_size += payload_size; | ||
| self.buffer.push(payload); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| std::mem::take(&mut self.buffer) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[allow(clippy::unwrap_used)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_add() { | ||
| let mut aggregator = StatsAggregator::default(); | ||
| let payload = ClientStatsPayload { | ||
| hostname: "hostname".to_string(), | ||
| env: "dev".to_string(), | ||
| version: "version".to_string(), | ||
| stats: vec![], | ||
| lang: "rust".to_string(), | ||
| tracer_version: "tracer.version".to_string(), | ||
| runtime_id: "hash".to_string(), | ||
| sequence: 0, | ||
| agent_aggregation: "aggregation".to_string(), | ||
| service: "service".to_string(), | ||
| container_id: "container_id".to_string(), | ||
| tags: vec![], | ||
| git_commit_sha: "git_commit_sha".to_string(), | ||
| image_tag: "image_tag".to_string(), | ||
| }; | ||
|
|
||
| aggregator.add(payload.clone()); | ||
| assert_eq!(aggregator.queue.len(), 1); | ||
| assert_eq!(aggregator.queue[0], payload); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_batch() { | ||
| let mut aggregator = StatsAggregator::default(); | ||
| let payload = ClientStatsPayload { | ||
| hostname: "hostname".to_string(), | ||
| env: "dev".to_string(), | ||
| version: "version".to_string(), | ||
| stats: vec![], | ||
| lang: "rust".to_string(), | ||
| tracer_version: "tracer.version".to_string(), | ||
| runtime_id: "hash".to_string(), | ||
| sequence: 0, | ||
| agent_aggregation: "aggregation".to_string(), | ||
| service: "service".to_string(), | ||
| container_id: "container_id".to_string(), | ||
| tags: vec![], | ||
| git_commit_sha: "git_commit_sha".to_string(), | ||
| image_tag: "image_tag".to_string(), | ||
| }; | ||
| aggregator.add(payload.clone()); | ||
| assert_eq!(aggregator.queue.len(), 1); | ||
| let batch = aggregator.get_batch(); | ||
| assert_eq!(batch, vec![payload]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_batch_full_entries() { | ||
| let mut aggregator = StatsAggregator::new(230); | ||
| // Payload below is 115 bytes | ||
| let payload = ClientStatsPayload { | ||
| hostname: "hostname".to_string(), | ||
| env: "dev".to_string(), | ||
| version: "version".to_string(), | ||
| stats: vec![], | ||
| lang: "rust".to_string(), | ||
| tracer_version: "tracer.version".to_string(), | ||
| runtime_id: "hash".to_string(), | ||
| sequence: 0, | ||
| agent_aggregation: "aggregation".to_string(), | ||
| service: "service".to_string(), | ||
| container_id: "container_id".to_string(), | ||
| tags: vec![], | ||
| git_commit_sha: "git_commit_sha".to_string(), | ||
| image_tag: "image_tag".to_string(), | ||
| }; | ||
|
|
||
| // Add 3 payloads | ||
| aggregator.add(payload.clone()); | ||
| aggregator.add(payload.clone()); | ||
| aggregator.add(payload.clone()); | ||
|
|
||
| // The batch should only contain the first 2 payloads | ||
| let first_batch = aggregator.get_batch(); | ||
| assert_eq!(first_batch, vec![payload.clone(), payload.clone()]); | ||
| assert_eq!(aggregator.queue.len(), 1); | ||
|
|
||
| // The second batch should only contain the last log | ||
| let second_batch = aggregator.get_batch(); | ||
| assert_eq!(second_batch, vec![payload]); | ||
| assert_eq!(aggregator.queue.len(), 0); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.