Skip to content

Commit 97a82b5

Browse files
author
Jonathan Woollett-Light
committed
feat: Add code instrumentation
Adds `log_instrument::instrument` attribute macros to functions. Signed-off-by: Jonathan Woollett-Light <[email protected]>
1 parent 178531a commit 97a82b5

File tree

226 files changed

+2387
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+2387
-0
lines changed

src/api_server/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct ApiServer {
4343
}
4444

4545
impl ApiServer {
46+
#[log_instrument::instrument]
4647
/// Constructor for `ApiServer`.
4748
///
4849
/// Returns the newly formed `ApiServer`.
@@ -59,6 +60,7 @@ impl ApiServer {
5960
}
6061
}
6162

63+
#[log_instrument::instrument]
6264
/// Runs the Api Server.
6365
///
6466
/// # Arguments
@@ -128,6 +130,7 @@ impl ApiServer {
128130
}
129131
}
130132

133+
#[log_instrument::instrument]
131134
/// Handles an API request received through the associated socket.
132135
pub fn handle_request(
133136
&mut self,
@@ -158,6 +161,7 @@ impl ApiServer {
158161
}
159162
}
160163

164+
#[log_instrument::instrument]
161165
fn serve_vmm_action_request(
162166
&mut self,
163167
vmm_action: Box<VmmAction>,
@@ -199,13 +203,15 @@ impl ApiServer {
199203
response
200204
}
201205

206+
#[log_instrument::instrument]
202207
/// An HTTP response which also includes a body.
203208
pub(crate) fn json_response<T: Into<String> + Debug>(status: StatusCode, body: T) -> Response {
204209
let mut response = Response::new(Version::Http11, status);
205210
response.set_body(Body::new(body.into()));
206211
response
207212
}
208213

214+
#[log_instrument::instrument]
209215
fn json_fault_message<T: AsRef<str> + serde::Serialize + Debug>(msg: T) -> String {
210216
json!({ "fault_message": msg }).to_string()
211217
}

src/api_server/src/parsed_request.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ pub(crate) struct ParsingInfo {
4141
}
4242

4343
impl ParsingInfo {
44+
#[log_instrument::instrument]
4445
pub fn append_deprecation_message(&mut self, message: &str) {
4546
match self.deprecation_message.as_mut() {
4647
None => self.deprecation_message = Some(message.to_owned()),
4748
Some(s) => (*s).push_str(message),
4849
}
4950
}
5051

52+
#[log_instrument::instrument]
5153
pub fn take_deprecation_message(&mut self) -> Option<String> {
5254
self.deprecation_message.take()
5355
}
@@ -61,6 +63,7 @@ pub(crate) struct ParsedRequest {
6163

6264
impl TryFrom<&Request> for ParsedRequest {
6365
type Error = Error;
66+
#[log_instrument::instrument]
6467
fn try_from(request: &Request) -> Result<Self, Self::Error> {
6568
let request_uri = request.uri().get_abs_path().to_string();
6669
log_received_api_request(describe(
@@ -121,21 +124,25 @@ impl TryFrom<&Request> for ParsedRequest {
121124
}
122125

123126
impl ParsedRequest {
127+
#[log_instrument::instrument]
124128
pub(crate) fn new(action: RequestAction) -> Self {
125129
Self {
126130
action,
127131
parsing_info: Default::default(),
128132
}
129133
}
130134

135+
#[log_instrument::instrument]
131136
pub(crate) fn into_parts(self) -> (RequestAction, ParsingInfo) {
132137
(self.action, self.parsing_info)
133138
}
134139

140+
#[log_instrument::instrument]
135141
pub(crate) fn parsing_info(&mut self) -> &mut ParsingInfo {
136142
&mut self.parsing_info
137143
}
138144

145+
#[log_instrument::instrument]
139146
pub(crate) fn success_response_with_data<T>(body_data: &T) -> Response
140147
where
141148
T: ?Sized + Serialize + Debug,
@@ -146,6 +153,7 @@ impl ParsedRequest {
146153
response
147154
}
148155

156+
#[log_instrument::instrument]
149157
pub(crate) fn success_response_with_mmds_value(body_data: &Value) -> Response {
150158
info!("The request was executed successfully. Status code: 200 OK.");
151159
let mut response = Response::new(Version::Http11, StatusCode::OK);
@@ -157,6 +165,7 @@ impl ParsedRequest {
157165
response
158166
}
159167

168+
#[log_instrument::instrument]
160169
pub(crate) fn convert_to_response(
161170
request_outcome: &std::result::Result<VmmData, VmmActionError>,
162171
) -> Response {
@@ -205,12 +214,14 @@ impl ParsedRequest {
205214
}
206215
}
207216

217+
#[log_instrument::instrument]
208218
/// Helper function to avoid boiler-plate code.
209219
pub(crate) fn new_sync(vmm_action: VmmAction) -> ParsedRequest {
210220
ParsedRequest::new(RequestAction::Sync(Box::new(vmm_action)))
211221
}
212222
}
213223

224+
#[log_instrument::instrument]
214225
/// Helper function for writing the received API requests to the log.
215226
///
216227
/// The `info` macro is used for logging.
@@ -219,6 +230,7 @@ fn log_received_api_request(api_description: String) {
219230
info!("The API server received a {}.", api_description);
220231
}
221232

233+
#[log_instrument::instrument]
222234
/// Helper function for metric-logging purposes on API requests.
223235
///
224236
/// # Arguments
@@ -246,6 +258,7 @@ fn describe(method: Method, path: &str, body: Option<&Body>) -> String {
246258
}
247259
}
248260

261+
#[log_instrument::instrument]
249262
fn describe_with_body(method: Method, path: &str, payload_value: &Body) -> String {
250263
format!(
251264
"{:?} request on {:?} with body {:?}",
@@ -257,6 +270,7 @@ fn describe_with_body(method: Method, path: &str, payload_value: &Body) -> Strin
257270
)
258271
}
259272

273+
#[log_instrument::instrument]
260274
/// Generates a `GenericError` for each request method.
261275
pub(crate) fn method_to_error(method: Method) -> Result<ParsedRequest, Error> {
262276
match method {
@@ -296,6 +310,7 @@ pub(crate) enum Error {
296310

297311
// It's convenient to turn errors into HTTP responses directly.
298312
impl From<Error> for Response {
313+
#[log_instrument::instrument]
299314
fn from(err: Error) -> Self {
300315
let msg = ApiServer::json_fault_message(format!("{}", err));
301316
match err {
@@ -309,6 +324,7 @@ impl From<Error> for Response {
309324
}
310325

311326
// This function is supposed to do id validation for requests.
327+
#[log_instrument::instrument]
312328
pub(crate) fn checked_id(id: &str) -> Result<&str, Error> {
313329
// todo: are there any checks we want to do on id's?
314330
// not allow them to be empty strings maybe?
@@ -341,6 +357,7 @@ pub mod tests {
341357
use super::*;
342358

343359
impl PartialEq for ParsedRequest {
360+
#[log_instrument::instrument]
344361
fn eq(&self, other: &ParsedRequest) -> bool {
345362
if self.parsing_info.deprecation_message != other.parsing_info.deprecation_message {
346363
return false;
@@ -355,13 +372,15 @@ pub mod tests {
355372
}
356373
}
357374

375+
#[log_instrument::instrument]
358376
pub(crate) fn vmm_action_from_request(req: ParsedRequest) -> VmmAction {
359377
match req.action {
360378
RequestAction::Sync(vmm_action) => *vmm_action,
361379
_ => panic!("Invalid request"),
362380
}
363381
}
364382

383+
#[log_instrument::instrument]
365384
pub(crate) fn depr_action_from_req(req: ParsedRequest, msg: Option<String>) -> VmmAction {
366385
let (action_req, mut parsing_info) = req.into_parts();
367386
match action_req {
@@ -375,6 +394,7 @@ pub mod tests {
375394
}
376395
}
377396

397+
#[log_instrument::instrument]
378398
fn http_response(body: &str, status_code: i32) -> String {
379399
let header = format!(
380400
"HTTP/1.1 {} \r\nServer: Firecracker API\r\nConnection: keep-alive\r\n",
@@ -394,6 +414,7 @@ pub mod tests {
394414
}
395415
}
396416

417+
#[log_instrument::instrument]
397418
fn http_request(request_type: &str, endpoint: &str, body: Option<&str>) -> String {
398419
let req_no_body = format!(
399420
"{} {} HTTP/1.1\r\nContent-Type: application/json\r\n",

src/api_server/src/request/actions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct ActionBody {
2828
action_type: ActionType,
2929
}
3030

31+
#[log_instrument::instrument]
3132
pub(crate) fn parse_put_actions(body: &Body) -> Result<ParsedRequest, Error> {
3233
METRICS.put_api_requests.actions_count.inc();
3334
let action_body = serde_json::from_slice::<ActionBody>(body.raw()).map_err(|err| {

src/api_server/src/request/balloon.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use super::super::VmmAction;
1010
use crate::parsed_request::{Error, ParsedRequest};
1111
use crate::request::Body;
1212

13+
#[log_instrument::instrument]
1314
pub(crate) fn parse_get_balloon(path_second_token: Option<&str>) -> Result<ParsedRequest, Error> {
1415
match path_second_token {
1516
Some(stats_path) => match stats_path {
@@ -23,12 +24,14 @@ pub(crate) fn parse_get_balloon(path_second_token: Option<&str>) -> Result<Parse
2324
}
2425
}
2526

27+
#[log_instrument::instrument]
2628
pub(crate) fn parse_put_balloon(body: &Body) -> Result<ParsedRequest, Error> {
2729
Ok(ParsedRequest::new_sync(VmmAction::SetBalloonDevice(
2830
serde_json::from_slice::<BalloonDeviceConfig>(body.raw())?,
2931
)))
3032
}
3133

34+
#[log_instrument::instrument]
3235
pub(crate) fn parse_patch_balloon(
3336
body: &Body,
3437
path_second_token: Option<&str>,

src/api_server/src/request/boot_source.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::super::VmmAction;
88
use crate::parsed_request::{Error, ParsedRequest};
99
use crate::request::Body;
1010

11+
#[log_instrument::instrument]
1112
pub(crate) fn parse_put_boot_source(body: &Body) -> Result<ParsedRequest, Error> {
1213
METRICS.put_api_requests.boot_source_count.inc();
1314
Ok(ParsedRequest::new_sync(VmmAction::ConfigureBootSource(

src/api_server/src/request/cpu_configuration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::super::VmmAction;
88
use crate::parsed_request::{Error, ParsedRequest};
99
use crate::request::Body;
1010

11+
#[log_instrument::instrument]
1112
pub(crate) fn parse_put_cpu_config(body: &Body) -> Result<ParsedRequest, Error> {
1213
METRICS.put_api_requests.cpu_cfg_count.inc();
1314

src/api_server/src/request/drive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::super::VmmAction;
88
use crate::parsed_request::{checked_id, Error, ParsedRequest};
99
use crate::request::{Body, StatusCode};
1010

11+
#[log_instrument::instrument]
1112
pub(crate) fn parse_put_drive(
1213
body: &Body,
1314
id_from_path: Option<&str>,
@@ -38,6 +39,7 @@ pub(crate) fn parse_put_drive(
3839
}
3940
}
4041

42+
#[log_instrument::instrument]
4143
pub(crate) fn parse_patch_drive(
4244
body: &Body,
4345
id_from_path: Option<&str>,

src/api_server/src/request/entropy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use vmm::vmm_config::entropy::EntropyDeviceConfig;
77
use crate::parsed_request::{Error, ParsedRequest};
88
use crate::request::Body;
99

10+
#[log_instrument::instrument]
1011
pub(crate) fn parse_put_entropy(body: &Body) -> Result<ParsedRequest, Error> {
1112
let cfg = serde_json::from_slice::<EntropyDeviceConfig>(body.raw())?;
1213
Ok(ParsedRequest::new_sync(VmmAction::SetEntropyDevice(cfg)))

src/api_server/src/request/instance_info.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use vmm::rpc_interface::VmmAction;
66

77
use crate::parsed_request::{Error, ParsedRequest};
88

9+
#[log_instrument::instrument]
910
pub(crate) fn parse_get_instance_info() -> Result<ParsedRequest, Error> {
1011
METRICS.get_api_requests.instance_info_count.inc();
1112
Ok(ParsedRequest::new_sync(VmmAction::GetVmInstanceInfo))

src/api_server/src/request/logger.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::super::VmmAction;
77
use crate::parsed_request::{Error, ParsedRequest};
88
use crate::request::Body;
99

10+
#[log_instrument::instrument]
1011
pub(crate) fn parse_put_logger(body: &Body) -> Result<ParsedRequest, Error> {
1112
METRICS.put_api_requests.logger_count.inc();
1213
let res = serde_json::from_slice::<LoggerConfig>(body.raw());

src/api_server/src/request/machine_configuration.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ use super::super::VmmAction;
88
use crate::parsed_request::{method_to_error, Error, ParsedRequest};
99
use crate::request::{Body, Method};
1010

11+
#[log_instrument::instrument]
1112
pub(crate) fn parse_get_machine_config() -> Result<ParsedRequest, Error> {
1213
METRICS.get_api_requests.machine_cfg_count.inc();
1314
Ok(ParsedRequest::new_sync(VmmAction::GetVmMachineConfig))
1415
}
1516

17+
#[log_instrument::instrument]
1618
pub(crate) fn parse_put_machine_config(body: &Body) -> Result<ParsedRequest, Error> {
1719
METRICS.put_api_requests.machine_cfg_count.inc();
1820
let config = serde_json::from_slice::<MachineConfig>(body.raw()).map_err(|err| {
@@ -27,6 +29,7 @@ pub(crate) fn parse_put_machine_config(body: &Body) -> Result<ParsedRequest, Err
2729
)))
2830
}
2931

32+
#[log_instrument::instrument]
3033
pub(crate) fn parse_patch_machine_config(body: &Body) -> Result<ParsedRequest, Error> {
3134
METRICS.patch_api_requests.machine_cfg_count.inc();
3235
let config_update =

src/api_server/src/request/metrics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::super::VmmAction;
88
use crate::parsed_request::{Error, ParsedRequest};
99
use crate::request::Body;
1010

11+
#[log_instrument::instrument]
1112
pub(crate) fn parse_put_metrics(body: &Body) -> Result<ParsedRequest, Error> {
1213
METRICS.put_api_requests.metrics_count.inc();
1314
Ok(ParsedRequest::new_sync(VmmAction::ConfigureMetrics(

src/api_server/src/request/mmds.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ use vmm::vmm_config::mmds::MmdsConfig;
1010
use crate::parsed_request::{Error, ParsedRequest};
1111
use crate::request::Body;
1212

13+
#[log_instrument::instrument]
1314
pub(crate) fn parse_get_mmds() -> Result<ParsedRequest, Error> {
1415
METRICS.get_api_requests.mmds_count.inc();
1516
Ok(ParsedRequest::new_sync(VmmAction::GetMMDS))
1617
}
1718

19+
#[log_instrument::instrument]
1820
fn parse_put_mmds_config(body: &Body) -> Result<ParsedRequest, Error> {
1921
let config: MmdsConfig = serde_json::from_slice(body.raw()).map_err(|err| {
2022
METRICS.put_api_requests.mmds_fails.inc();
@@ -35,6 +37,7 @@ fn parse_put_mmds_config(body: &Body) -> Result<ParsedRequest, Error> {
3537
Ok(parsed_request)
3638
}
3739

40+
#[log_instrument::instrument]
3841
pub(crate) fn parse_put_mmds(
3942
body: &Body,
4043
path_second_token: Option<&str>,
@@ -58,6 +61,7 @@ pub(crate) fn parse_put_mmds(
5861
}
5962
}
6063

64+
#[log_instrument::instrument]
6165
pub(crate) fn parse_patch_mmds(body: &Body) -> Result<ParsedRequest, Error> {
6266
METRICS.patch_api_requests.mmds_count.inc();
6367
Ok(ParsedRequest::new_sync(VmmAction::PatchMMDS(

src/api_server/src/request/net.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::super::VmmAction;
88
use crate::parsed_request::{checked_id, Error, ParsedRequest};
99
use crate::request::{Body, StatusCode};
1010

11+
#[log_instrument::instrument]
1112
pub(crate) fn parse_put_net(
1213
body: &Body,
1314
id_from_path: Option<&str>,
@@ -40,6 +41,7 @@ pub(crate) fn parse_put_net(
4041
)))
4142
}
4243

44+
#[log_instrument::instrument]
4345
pub(crate) fn parse_patch_net(
4446
body: &Body,
4547
id_from_path: Option<&str>,

src/api_server/src/request/snapshot.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub const MISSING_FIELD: &str =
2222
pub const TOO_MANY_FIELDS: &str =
2323
"too many fields: either `mem_backend` or `mem_file_path` exclusively is required";
2424

25+
#[log_instrument::instrument]
2526
pub(crate) fn parse_put_snapshot(
2627
body: &Body,
2728
request_type_from_path: Option<&str>,
@@ -44,6 +45,7 @@ pub(crate) fn parse_put_snapshot(
4445
}
4546
}
4647

48+
#[log_instrument::instrument]
4749
pub(crate) fn parse_patch_vm_state(body: &Body) -> Result<ParsedRequest, Error> {
4850
let vm = serde_json::from_slice::<Vm>(body.raw())?;
4951

@@ -53,6 +55,7 @@ pub(crate) fn parse_patch_vm_state(body: &Body) -> Result<ParsedRequest, Error>
5355
}
5456
}
5557

58+
#[log_instrument::instrument]
5659
fn parse_put_snapshot_load(body: &Body) -> Result<ParsedRequest, Error> {
5760
let snapshot_config = serde_json::from_slice::<LoadSnapshotConfig>(body.raw())?;
5861

0 commit comments

Comments
 (0)