Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 14 additions & 60 deletions bottlecap/src/lifecycle/invocation/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ impl ContextBuffer {
.find(|context| context.request_id == *request_id)
}

/// Adds the init duration to a `Context` in the buffer. If the `Context` is not found, a new
/// `Context` is created and added to the buffer.
/// Creates a new `Context` and adds it to the buffer.
///
pub fn create_context(&mut self, request_id: String) {
self.insert(Context::new(request_id, 0.0, 0.0, 0, None));
}

/// Adds the init duration to a `Context` in the buffer.
///
pub fn add_init_duration(&mut self, request_id: &String, init_duration_ms: f64) {
if let Some(context) = self
Expand All @@ -104,18 +109,11 @@ impl ContextBuffer {
{
context.init_duration_ms = init_duration_ms;
} else {
self.insert(Context::new(
request_id.clone(),
0.0,
init_duration_ms,
0,
None,
));
debug!("Could not add init duration - context not found");
}
}

/// Adds the start time to a `Context` in the buffer. If the `Context` is not found, a new
/// `Context` is created and added to the buffer.
/// Adds the start time to a `Context` in the buffer.
///
pub fn add_start_time(&mut self, request_id: &String, start_time: i64) {
if let Some(context) = self
Expand All @@ -125,12 +123,11 @@ impl ContextBuffer {
{
context.start_time = start_time;
} else {
self.insert(Context::new(request_id.clone(), 0.0, 0.0, start_time, None));
debug!("Could not add start time - context not found");
}
}

/// Adds the runtime duration to a `Context` in the buffer. If the `Context` is not found, a new
/// `Context` is created and added to the buffer.
/// Adds the runtime duration to a `Context` in the buffer.
///
pub fn add_runtime_duration(&mut self, request_id: &String, runtime_duration_ms: f64) {
if let Some(context) = self
Expand All @@ -140,18 +137,11 @@ impl ContextBuffer {
{
context.runtime_duration_ms = runtime_duration_ms;
} else {
self.insert(Context::new(
request_id.clone(),
runtime_duration_ms,
0.0,
0,
None,
));
debug!("Could not add runtime duration - context not found");
}
}

/// Adds the network offset to a `Context` in the buffer. If the `Context` is not found, a new
/// `Context` is created and added to the buffer.
/// Adds the network offset to a `Context` in the buffer.
///
pub fn add_network_offset(&mut self, request_id: &String, network_data: Option<NetworkData>) {
if let Some(context) = self
Expand All @@ -161,7 +151,7 @@ impl ContextBuffer {
{
context.network_offset = network_data;
} else {
self.insert(Context::new(request_id.clone(), 0.0, 0.0, 0, network_data));
debug!("Could not add network offset - context not found");
}
}

Expand Down Expand Up @@ -265,15 +255,6 @@ mod tests {

buffer.add_init_duration(&request_id, 100.0);
assert_eq!(buffer.get(&request_id).unwrap().init_duration_ms, 100.0);

// Add init duration to a context that doesn't exist
let unexistent_request_id = String::from("unexistent");
buffer.add_init_duration(&unexistent_request_id, 200.0);
assert_eq!(buffer.size(), 2);
assert_eq!(
buffer.get(&unexistent_request_id).unwrap().init_duration_ms,
200.0
);
}

#[test]
Expand All @@ -288,12 +269,6 @@ mod tests {

buffer.add_start_time(&request_id, 100);
assert_eq!(buffer.get(&request_id).unwrap().start_time, 100);

// Add start time to a context that doesn't exist
let unexistent_request_id = String::from("unexistent");
buffer.add_start_time(&unexistent_request_id, 200);
assert_eq!(buffer.size(), 2);
assert_eq!(buffer.get(&unexistent_request_id).unwrap().start_time, 200);
}

#[test]
Expand All @@ -308,18 +283,6 @@ mod tests {

buffer.add_runtime_duration(&request_id, 100.0);
assert_eq!(buffer.get(&request_id).unwrap().runtime_duration_ms, 100.0);

// Add runtime duration to a context that doesn't exist
let unexistent_request_id = String::from("unexistent");
buffer.add_runtime_duration(&unexistent_request_id, 200.0);
assert_eq!(buffer.size(), 2);
assert_eq!(
buffer
.get(&unexistent_request_id)
.unwrap()
.runtime_duration_ms,
200.0
);
}

#[test]
Expand All @@ -342,14 +305,5 @@ mod tests {
buffer.get(&request_id).unwrap().network_offset,
network_offset,
);

// Add network offset to a context that doesn't exist
let unexistent_request_id = String::from("unexistent");
buffer.add_network_offset(&unexistent_request_id, network_offset);
assert_eq!(buffer.size(), 2);
assert_eq!(
buffer.get(&unexistent_request_id).unwrap().network_offset,
network_offset
);
}
}
2 changes: 2 additions & 0 deletions bottlecap/src/lifecycle/invocation/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ impl Processor {
/// Given a `request_id`, add the enhanced metric offsets to the context buffer.
///
pub fn on_invoke_event(&mut self, request_id: String) {
self.context_buffer.create_context(request_id.clone());

let network_offset: Option<NetworkData> = proc::get_network_data().ok();
self.context_buffer
.add_network_offset(&request_id, network_offset);
Expand Down