-
Notifications
You must be signed in to change notification settings - Fork 20
feat: [Trace Stats] Add skeleton of concentrator #842
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
9 commits
Select commit
Hold shift + click to select a range
f2848b3
feat: [Trace Stats] Add skeleton of concentrator
lym953 426bbb2
Fix tests
lym953 59247e8
fmt
lym953 a522609
Revert unnecessary changes
lym953 da8134c
Remove stats agent
lym953 953af7e
fmt
lym953 69f51c9
Add comments
lym953 0dbf697
Rename: get_stats() -> flush()
lym953 46efff5
Use thiserror
lym953 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
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,38 @@ | ||
| use crate::config::Config; | ||
| use datadog_trace_protobuf::pb; | ||
| use std::sync::Arc; | ||
|
|
||
| // Event sent to the stats concentrator | ||
| #[derive(Clone, Copy)] | ||
| pub struct StatsEvent { | ||
| pub time: u64, | ||
| pub aggregation_key: AggregationKey, | ||
| pub stats: Stats, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)] | ||
| pub struct AggregationKey {} | ||
|
|
||
| #[derive(Clone, Debug, Default, Copy)] | ||
| pub struct Stats {} | ||
|
|
||
| pub struct StatsConcentrator { | ||
| _config: Arc<Config>, | ||
| } | ||
|
|
||
| // Aggregates stats into buckets, which are then pulled by the stats aggregator. | ||
| impl StatsConcentrator { | ||
| #[must_use] | ||
| pub fn new(config: Arc<Config>) -> Self { | ||
| Self { _config: config } | ||
| } | ||
|
|
||
| pub fn add(&mut self, _stats_event: StatsEvent) {} | ||
|
|
||
| // force_flush: If true, flush all stats. If false, flush stats except for the few latest | ||
| // buckets, which may still be getting data. | ||
| #[must_use] | ||
| pub fn flush(&mut self, _force_flush: bool) -> Vec<pb::ClientStatsPayload> { | ||
| vec![] | ||
| } | ||
| } |
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,79 @@ | ||
| use tokio::sync::{mpsc, oneshot}; | ||
|
|
||
| use crate::config::Config; | ||
| use crate::traces::stats_concentrator::StatsConcentrator; | ||
| use crate::traces::stats_concentrator::StatsEvent; | ||
| use datadog_trace_protobuf::pb; | ||
| use std::sync::Arc; | ||
| use tracing::error; | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum StatsError { | ||
| #[error("Failed to send command to concentrator: {0}")] | ||
| SendError(mpsc::error::SendError<ConcentratorCommand>), | ||
| #[error("Failed to receive response from concentrator: {0}")] | ||
| RecvError(oneshot::error::RecvError), | ||
| } | ||
|
|
||
| pub enum ConcentratorCommand { | ||
| Add(StatsEvent), | ||
| Flush(bool, oneshot::Sender<Vec<pb::ClientStatsPayload>>), | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct StatsConcentratorHandle { | ||
| tx: mpsc::UnboundedSender<ConcentratorCommand>, | ||
| } | ||
|
|
||
| impl StatsConcentratorHandle { | ||
| pub fn add( | ||
| &self, | ||
| stats_event: StatsEvent, | ||
| ) -> Result<(), mpsc::error::SendError<ConcentratorCommand>> { | ||
| self.tx.send(ConcentratorCommand::Add(stats_event)) | ||
| } | ||
|
|
||
| pub async fn flush( | ||
| &self, | ||
| force_flush: bool, | ||
| ) -> Result<Vec<pb::ClientStatsPayload>, StatsError> { | ||
| let (response_tx, response_rx) = oneshot::channel(); | ||
| self.tx | ||
| .send(ConcentratorCommand::Flush(force_flush, response_tx)) | ||
| .map_err(StatsError::SendError)?; | ||
| let stats = response_rx.await.map_err(StatsError::RecvError)?; | ||
| Ok(stats) | ||
| } | ||
| } | ||
|
|
||
| pub struct StatsConcentratorService { | ||
| concentrator: StatsConcentrator, | ||
| rx: mpsc::UnboundedReceiver<ConcentratorCommand>, | ||
| } | ||
|
|
||
| // A service that handles add() and flush() requests in the same queue, | ||
| // to avoid using mutex, which may cause lock contention. | ||
| impl StatsConcentratorService { | ||
| #[must_use] | ||
| pub fn new(config: Arc<Config>) -> (Self, StatsConcentratorHandle) { | ||
| let (tx, rx) = mpsc::unbounded_channel(); | ||
| let handle = StatsConcentratorHandle { tx }; | ||
| let concentrator = StatsConcentrator::new(config); | ||
| let service: StatsConcentratorService = Self { concentrator, rx }; | ||
| (service, handle) | ||
| } | ||
|
|
||
| pub async fn run(mut self) { | ||
| while let Some(command) = self.rx.recv().await { | ||
| match command { | ||
| ConcentratorCommand::Add(stats_event) => self.concentrator.add(stats_event), | ||
| ConcentratorCommand::Flush(force_flush, response_tx) => { | ||
| let stats = self.concentrator.flush(force_flush); | ||
| if let Err(e) = response_tx.send(stats) { | ||
| error!("Failed to return trace stats: {e:?}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we should have a submodule called
statsfor all of this stuff, instead of having it asstats_...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might do it in a separate PR