Skip to content

Commit eee3081

Browse files
author
Jonathan Woollett-Light
committed
fix: tracing::instrument
Adds function instrumentation. Signed-off-by: Jonathan Woollett-Light <jcawl@amazon.co.uk>
1 parent 4e7f1f4 commit eee3081

File tree

222 files changed

+2843
-21
lines changed

Some content is hidden

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

222 files changed

+2843
-21
lines changed

src/api_server/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ impl ApiServer {
6161
/// Constructor for `ApiServer`.
6262
///
6363
/// Returns the newly formed `ApiServer`.
64+
#[tracing::instrument(
65+
level = "debug",
66+
ret(skip),
67+
skip(api_request_sender, vmm_response_receiver, to_vmm_fd)
68+
)]
6469
pub fn new(
6570
api_request_sender: mpsc::Sender<ApiRequest>,
6671
vmm_response_receiver: mpsc::Receiver<ApiResponse>,
@@ -145,6 +150,18 @@ impl ApiServer {
145150
/// let mut buf: [u8; 100] = [0; 100];
146151
/// assert!(sock.read(&mut buf[..]).unwrap() > 0);
147152
/// ```
153+
#[tracing::instrument(
154+
level = "debug",
155+
ret(skip),
156+
skip(
157+
self,
158+
path,
159+
process_time_reporter,
160+
seccomp_filter,
161+
api_payload_limit,
162+
socket_ready
163+
)
164+
)]
148165
pub fn bind_and_run(
149166
&mut self,
150167
path: &PathBuf,
@@ -221,6 +238,11 @@ impl ApiServer {
221238
}
222239

223240
/// Handles an API request received through the associated socket.
241+
#[tracing::instrument(
242+
level = "debug",
243+
ret(skip),
244+
skip(self, request, request_processing_start_us)
245+
)]
224246
pub fn handle_request(
225247
&mut self,
226248
request: &Request,
@@ -250,6 +272,11 @@ impl ApiServer {
250272
}
251273
}
252274

275+
#[tracing::instrument(
276+
level = "debug",
277+
ret(skip),
278+
skip(self, vmm_action, request_processing_start_us)
279+
)]
253280
fn serve_vmm_action_request(
254281
&mut self,
255282
vmm_action: Box<VmmAction>,
@@ -292,12 +319,14 @@ impl ApiServer {
292319
}
293320

294321
/// An HTTP response which also includes a body.
322+
#[tracing::instrument(level = "debug", ret(skip), skip(status, body))]
295323
pub(crate) fn json_response<T: Into<String> + Debug>(status: StatusCode, body: T) -> Response {
296324
let mut response = Response::new(Version::Http11, status);
297325
response.set_body(Body::new(body.into()));
298326
response
299327
}
300328

329+
#[tracing::instrument(level = "debug", ret(skip), skip(msg))]
301330
fn json_fault_message<T: AsRef<str> + serde::Serialize + Debug>(msg: T) -> String {
302331
json!({ "fault_message": msg }).to_string()
303332
}

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+
#[tracing::instrument(level = "debug", ret(skip), skip(self, message))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(self))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(request))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(action))]
124128
pub(crate) fn new(action: RequestAction) -> Self {
125129
Self {
126130
action,
127131
parsing_info: Default::default(),
128132
}
129133
}
130134

135+
#[tracing::instrument(level = "debug", ret(skip), skip(self))]
131136
pub(crate) fn into_parts(self) -> (RequestAction, ParsingInfo) {
132137
(self.action, self.parsing_info)
133138
}
134139

140+
#[tracing::instrument(level = "debug", skip(self))]
135141
pub(crate) fn parsing_info(&mut self) -> &mut ParsingInfo {
136142
&mut self.parsing_info
137143
}
138144

145+
#[tracing::instrument(level = "debug", ret(skip), skip(body_data))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(body_data))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(request_outcome))]
160169
pub(crate) fn convert_to_response(
161170
request_outcome: &std::result::Result<VmmData, VmmActionError>,
162171
) -> Response {
@@ -206,6 +215,7 @@ impl ParsedRequest {
206215
}
207216

208217
/// Helper function to avoid boiler-plate code.
218+
#[tracing::instrument(level = "debug", ret(skip), skip(vmm_action))]
209219
pub(crate) fn new_sync(vmm_action: VmmAction) -> ParsedRequest {
210220
ParsedRequest::new(RequestAction::Sync(Box::new(vmm_action)))
211221
}
@@ -215,6 +225,7 @@ impl ParsedRequest {
215225
///
216226
/// The `info` macro is used for logging.
217227
#[inline]
228+
#[tracing::instrument(level = "debug", ret(skip), skip(api_description))]
218229
fn log_received_api_request(api_description: String) {
219230
info!("The API server received a {}.", api_description);
220231
}
@@ -226,6 +237,7 @@ fn log_received_api_request(api_description: String) {
226237
/// * `method` - one of `GET`, `PATCH`, `PUT`
227238
/// * `path` - path of the API request
228239
/// * `body` - body of the API request
240+
#[tracing::instrument(level = "debug", ret(skip), skip(method, path, body))]
229241
fn describe(method: Method, path: &str, body: Option<&Body>) -> String {
230242
match (path, body) {
231243
("/mmds", Some(_)) | (_, None) => format!("{:?} request on {:?}", method, path),
@@ -234,6 +246,7 @@ fn describe(method: Method, path: &str, body: Option<&Body>) -> String {
234246
}
235247
}
236248

249+
#[tracing::instrument(level = "debug", ret(skip), skip(method, path, payload_value))]
237250
fn describe_with_body(method: Method, path: &str, payload_value: &Body) -> String {
238251
format!(
239252
"{:?} request on {:?} with body {:?}",
@@ -246,6 +259,7 @@ fn describe_with_body(method: Method, path: &str, payload_value: &Body) -> Strin
246259
}
247260

248261
/// Generates a `GenericError` for each request method.
262+
#[tracing::instrument(level = "debug", ret(skip), skip(method))]
249263
pub(crate) fn method_to_error(method: Method) -> Result<ParsedRequest, Error> {
250264
match method {
251265
Method::Get => Err(Error::Generic(
@@ -284,6 +298,7 @@ pub(crate) enum Error {
284298

285299
// It's convenient to turn errors into HTTP responses directly.
286300
impl From<Error> for Response {
301+
#[tracing::instrument(level = "debug", ret(skip), skip(err))]
287302
fn from(err: Error) -> Self {
288303
let msg = ApiServer::json_fault_message(format!("{}", err));
289304
match err {
@@ -297,6 +312,7 @@ impl From<Error> for Response {
297312
}
298313

299314
// This function is supposed to do id validation for requests.
315+
#[tracing::instrument(level = "debug", ret(skip), skip(id))]
300316
pub(crate) fn checked_id(id: &str) -> Result<&str, Error> {
301317
// todo: are there any checks we want to do on id's?
302318
// not allow them to be empty strings maybe?
@@ -329,6 +345,7 @@ pub mod tests {
329345
use super::*;
330346

331347
impl PartialEq for ParsedRequest {
348+
#[tracing::instrument(level = "debug", ret(skip), skip(self, other))]
332349
fn eq(&self, other: &ParsedRequest) -> bool {
333350
if self.parsing_info.deprecation_message != other.parsing_info.deprecation_message {
334351
return false;
@@ -343,13 +360,15 @@ pub mod tests {
343360
}
344361
}
345362

363+
#[tracing::instrument(level = "debug", ret(skip), skip(req))]
346364
pub(crate) fn vmm_action_from_request(req: ParsedRequest) -> VmmAction {
347365
match req.action {
348366
RequestAction::Sync(vmm_action) => *vmm_action,
349367
_ => panic!("Invalid request"),
350368
}
351369
}
352370

371+
#[tracing::instrument(level = "debug", ret(skip), skip(req, msg))]
353372
pub(crate) fn depr_action_from_req(req: ParsedRequest, msg: Option<String>) -> VmmAction {
354373
let (action_req, mut parsing_info) = req.into_parts();
355374
match action_req {
@@ -363,6 +382,7 @@ pub mod tests {
363382
}
364383
}
365384

385+
#[tracing::instrument(level = "debug", ret(skip), skip(body, status_code))]
366386
fn http_response(body: &str, status_code: i32) -> String {
367387
let header = format!(
368388
"HTTP/1.1 {} \r\nServer: Firecracker API\r\nConnection: keep-alive\r\n",
@@ -382,6 +402,7 @@ pub mod tests {
382402
}
383403
}
384404

405+
#[tracing::instrument(level = "debug", ret(skip), skip(request_type, endpoint, body))]
385406
fn http_request(request_type: &str, endpoint: &str, body: Option<&str>) -> String {
386407
let req_no_body = format!(
387408
"{} {} 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+
#[tracing::instrument(level = "debug", ret(skip), skip(body))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(path_second_token))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(body))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(body, path_second_token))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(body))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(body))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(body, id_from_path))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(body, id_from_path))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip(body))]
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+
#[tracing::instrument(level = "debug", ret(skip), skip())]
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
@@ -8,6 +8,7 @@ use super::super::VmmAction;
88
use crate::parsed_request::{Error, ParsedRequest};
99
use crate::request::Body;
1010

11+
#[tracing::instrument(level = "debug", ret(skip), skip(body))]
1112
pub(crate) fn parse_put_logger(body: &Body) -> Result<ParsedRequest, Error> {
1213
METRICS.put_api_requests.logger_count.inc();
1314
let res = serde_json::from_slice::<LoggerConfig>(body.raw());

0 commit comments

Comments
 (0)