-
Notifications
You must be signed in to change notification settings - Fork 15
fix(tracer-flare): fix design of TracerFlareManager #1186
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
Open
anais-raison
wants to merge
16
commits into
main
Choose a base branch
from
anais/fix-tracer-flare-design
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−144
Open
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d3acce5
fix(tracer-flare): fix design of TracerFlareManager
anais-raison d43829d
chore(tracer-flare): fix format
anais-raison 3ef0bba
Merge branch 'main' into anais/fix-tracer-flare-design
anais-raison 60e40ae
Merge branch 'main' into anais/fix-tracer-flare-design
anais-raison d66ed5d
Merge branch 'main' into anais/fix-tracer-flare-design
anais-raison eaf105a
fix(tracer-flare): fix design and handling of RemoteConfigFile
anais-raison 3a21e48
Merge branch 'main' into anais/fix-tracer-flare-design
anais-raison fdca20a
fix(tracer-flare): fix zip function
anais-raison 9590516
fix(tracer-flare): fix design to return vec of action to perform
anais-raison a6cd28d
fix: clippy
anais-raison b8c8f25
Merge branch 'main' into anais/fix-tracer-flare-design
anais-raison e42d3d3
fix: remove comment
anais-raison cf8cf29
fix: fmt
anais-raison 8cc455a
fix(tracer-flare): update design to handle action by priority
anais-raison 55ee1e8
Merge branch 'main' into anais/fix-tracer-flare-design
anais-raison d1b051f
fix: clippy
anais-raison 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
Some comments aren't visible on the classic Files Changed page.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,30 +30,18 @@ use crate::error::FlareError; | |
pub struct TracerFlareManager { | ||
pub agent_url: String, | ||
pub language: String, | ||
pub state: State, | ||
pub agent_task: Option<AgentTaskFile>, | ||
anais-raison marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// As a featured option so we can use the component with no Listener | ||
#[cfg(feature = "listener")] | ||
pub listener: Option<Listener>, | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum State { | ||
Idle, | ||
Collecting { | ||
log_level: String, | ||
}, | ||
Sending { | ||
agent_task: AgentTaskFile, | ||
log_level: String, | ||
}, | ||
} | ||
|
||
impl Default for TracerFlareManager { | ||
fn default() -> Self { | ||
TracerFlareManager { | ||
agent_url: hyper::Uri::default().to_string(), | ||
language: "rust".to_string(), | ||
state: State::Idle, | ||
agent_task: None, | ||
#[cfg(feature = "listener")] | ||
listener: None, | ||
} | ||
|
@@ -168,9 +156,11 @@ pub enum LogLevel { | |
#[derive(Debug, PartialEq)] | ||
pub enum ReturnAction { | ||
/// If AGENT_CONFIG received with the right properties. | ||
Start(LogLevel), | ||
Set(LogLevel), | ||
/// If AGENT_CONFIG is removed. | ||
Unset, | ||
/// If AGENT_TASK received with the right properties. | ||
Stop, | ||
Send, | ||
/// If anything else received. | ||
None, | ||
} | ||
|
@@ -179,7 +169,7 @@ impl TryFrom<&str> for LogLevel { | |
type Error = FlareError; | ||
|
||
fn try_from(level: &str) -> Result<Self, FlareError> { | ||
match level { | ||
match level.to_lowercase().as_str() { | ||
"trace" => Ok(LogLevel::Trace), | ||
"debug" => Ok(LogLevel::Debug), | ||
"info" => Ok(LogLevel::Info), | ||
|
@@ -219,51 +209,15 @@ pub fn check_remote_config_file( | |
RemoteConfigData::TracerFlareConfig(agent_config) => { | ||
if agent_config.name.starts_with("flare-log-level.") { | ||
if let Some(log_level) = &agent_config.config.log_level { | ||
if let State::Collecting { log_level: _ } = tracer_flare.state { | ||
ekump marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Err(FlareError::RemoteConfigError( | ||
"Cannot start a flare while one is already running".to_string(), | ||
)); | ||
} | ||
if let State::Sending { | ||
agent_task: _, | ||
log_level: _, | ||
} = tracer_flare.state | ||
{ | ||
return Err(FlareError::RemoteConfigError( | ||
"Cannot start a flare while one is waiting to be sent".to_string(), | ||
)); | ||
} | ||
// Idle state | ||
tracer_flare.state = State::Collecting { | ||
log_level: log_level.to_string(), | ||
}; | ||
let log_level = log_level.as_str().try_into()?; | ||
return Ok(ReturnAction::Start(log_level)); | ||
return Ok(ReturnAction::Set(log_level)); | ||
} | ||
} | ||
} | ||
RemoteConfigData::TracerFlareTask(agent_task) => { | ||
if agent_task.task_type.eq("tracer_flare") { | ||
if let State::Collecting { log_level } = &tracer_flare.state { | ||
tracer_flare.state = State::Sending { | ||
agent_task: agent_task.clone(), | ||
log_level: log_level.to_string(), | ||
}; | ||
return Ok(ReturnAction::Stop); | ||
} | ||
if let State::Sending { | ||
agent_task: _, | ||
log_level: _, | ||
} = tracer_flare.state | ||
{ | ||
return Err(FlareError::RemoteConfigError( | ||
"Cannot stop a flare that it is already waiting to be sent".to_string(), | ||
)); | ||
} | ||
// Idle state | ||
return Err(FlareError::RemoteConfigError( | ||
"Cannot stop an inexisting flare".to_string(), | ||
)); | ||
tracer_flare.agent_task = Some(agent_task.to_owned()); | ||
return Ok(ReturnAction::Send); | ||
} | ||
} | ||
_ => return Ok(ReturnAction::None), | ||
|
@@ -345,6 +299,18 @@ pub async fn run_remote_config_listener( | |
if action != Ok(ReturnAction::None) { | ||
return action; | ||
} | ||
} else if let Change::Remove(file) = change { | ||
match file.contents().as_ref() { | ||
Ok(data) => match data { | ||
RemoteConfigData::TracerFlareConfig(_) => { | ||
return Ok(ReturnAction::Unset) | ||
} | ||
_ => continue, | ||
}, | ||
Err(e) => { | ||
return Err(FlareError::ParsingError(e.to_string())); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -358,9 +324,7 @@ pub async fn run_remote_config_listener( | |
|
||
#[cfg(test)] | ||
mod tests { | ||
ekump marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have a test for |
||
use crate::{ | ||
check_remote_config_file, FlareError, LogLevel, ReturnAction, State, TracerFlareManager, | ||
}; | ||
use crate::{check_remote_config_file, FlareError, LogLevel, ReturnAction, TracerFlareManager}; | ||
use datadog_remote_config::{ | ||
config::{ | ||
agent_config::{AgentConfig, AgentConfigFile}, | ||
|
@@ -410,17 +374,11 @@ mod tests { | |
let mut tracer_flare = TracerFlareManager::default(); | ||
let result = check_remote_config_file(file, &mut tracer_flare); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), ReturnAction::Start(LogLevel::Info)); | ||
assert_eq!( | ||
tracer_flare.state, | ||
State::Collecting { | ||
log_level: "info".to_string() | ||
} | ||
); | ||
assert_eq!(result.unwrap(), ReturnAction::Set(LogLevel::Info)); | ||
} | ||
|
||
#[test] | ||
fn test_check_remote_config_file_with_stop_task() { | ||
fn test_check_remote_config_file_with_send_task() { | ||
let storage = ParsedFileStorage::default(); | ||
let path = Arc::new(RemoteConfigPath { | ||
product: RemoteConfigProduct::AgentTask, | ||
|
@@ -442,23 +400,11 @@ mod tests { | |
let file = storage | ||
.store(1, path.clone(), serde_json::to_vec(&task).unwrap()) | ||
.unwrap(); | ||
let mut tracer_flare = TracerFlareManager { | ||
// Emulate the start action | ||
state: State::Collecting { | ||
log_level: "debug".to_string(), | ||
}, | ||
..Default::default() | ||
}; | ||
let mut tracer_flare = TracerFlareManager::default(); | ||
let result = check_remote_config_file(file, &mut tracer_flare); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), ReturnAction::Stop); | ||
assert_eq!( | ||
tracer_flare.state, | ||
State::Sending { | ||
agent_task: task, | ||
log_level: "debug".to_string() | ||
} | ||
); | ||
assert_eq!(result.unwrap(), ReturnAction::Send); | ||
assert_eq!(tracer_flare.agent_task, Some(task)); | ||
} | ||
|
||
#[test] | ||
|
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.
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.