Skip to content

Commit aca95c6

Browse files
committed
feat(mmds): Add metric to count GET requests without tokens
The metric is useful for users to track whether v1-style requests are issued or not inside a guest. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent d426149 commit aca95c6

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ and this project adheres to
2121
"X-aws-ec2-metadata-token" and "X-aws-ec2-metadata-token-ttl-seconds")
2222
alongside the MMDS-specific ones.
2323
- [#5290](https://github.com/firecracker-microvm/firecracker/pull/5290): Added
24-
`mmds.rx_invalid_token` metric to track the number of GET requests that were
25-
rejected due to token validation failures in MMDS version 2. This metric also
26-
counts requests that would be rejected in MMDS version 2 when MMDS version 1
27-
is configured. This helps users assess readiness for migrating to MMDS version
28-
2.
24+
`mmds.rx_invalid_token` and `mmds.rx_no_token` metrics to track the number of
25+
GET requests that were rejected due to token validation failures in MMDS
26+
version 2. These metrics also count requests that would be rejected in MMDS
27+
version 2 when MMDS version 1 is configured. They helps users assess readiness
28+
for migrating to MMDS version 2.
2929

3030
### Changed
3131

docs/mmds/mmds-user-guide.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ As in version 2, version 1 also supports a session oriented method in order to
235235
make the migration easier. See [the next section](#version-2) for the session
236236
oriented method. Note that version 1 returns a successful response to a `GET`
237237
request even with an invalid token or no token not to break existing workloads.
238-
`mmds.rx_invalid_token` metric tracks the number of `GET` requests with invalid
239-
tokens, helping users evaluate their readiness for migrating to MMDS version 2.
238+
`mmds.rx_invalid_token` and `mmds.rx_no_token` metrics track the number of
239+
`GET` requests with invalid tokens and missing tokens respectively, helping
240+
users evaluate their readiness for migrating to MMDS version 2.
240241

241242
Requests containing any other HTTP methods than `GET` and `PUT` will receive
242243
**405 Method Not Allowed** error.

src/vmm/src/logger/metrics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,8 @@ pub struct MmdsMetrics {
558558
pub rx_bad_eth: SharedIncMetric,
559559
/// The number of GET requests with invalid tokens.
560560
pub rx_invalid_token: SharedIncMetric,
561+
/// The number of GET requests with no tokens.
562+
pub rx_no_token: SharedIncMetric,
561563
/// The total number of successful receive operations by the MMDS.
562564
pub rx_count: SharedIncMetric,
563565
/// The total number of bytes sent by the MMDS.
@@ -582,6 +584,7 @@ impl MmdsMetrics {
582584
rx_accepted_unusual: SharedIncMetric::new(),
583585
rx_bad_eth: SharedIncMetric::new(),
584586
rx_invalid_token: SharedIncMetric::new(),
587+
rx_no_token: SharedIncMetric::new(),
585588
rx_count: SharedIncMetric::new(),
586589
tx_bytes: SharedIncMetric::new(),
587590
tx_count: SharedIncMetric::new(),

src/vmm/src/mmds/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn respond_to_get_request_v1(mmds: &Mmds, request: Request) -> Response {
149149
}
150150
}
151151
None => {
152-
// TODO: Increment a metric that will be added in an upcoming commit.
152+
METRICS.mmds.rx_no_token.inc();
153153
}
154154
}
155155

@@ -162,6 +162,7 @@ fn respond_to_get_request_v2(mmds: &Mmds, request: Request) -> Response {
162162
let token = match x_metadata_token.0 {
163163
Some(token) => token,
164164
None => {
165+
METRICS.mmds.rx_no_token.inc();
165166
let error_msg = VmmMmdsError::NoTokenProvided.to_string();
166167
return build_response(
167168
request.http_version(),
@@ -533,9 +534,11 @@ mod tests {
533534
MediaType::ApplicationJson,
534535
);
535536
let prev_rx_invalid_token = METRICS.mmds.rx_invalid_token.count();
537+
let prev_rx_no_token = METRICS.mmds.rx_no_token.count();
536538
let actual_response = convert_to_response(mmds.clone(), request);
537539
assert_eq!(actual_response, expected_response);
538540
assert_eq!(prev_rx_invalid_token, METRICS.mmds.rx_invalid_token.count());
541+
assert_eq!(prev_rx_no_token + 1, METRICS.mmds.rx_no_token.count());
539542

540543
// Test valid v2 request.
541544
let request = Request::try_from(
@@ -560,9 +563,11 @@ mod tests {
560563
MediaType::ApplicationJson,
561564
);
562565
let prev_rx_invalid_token = METRICS.mmds.rx_invalid_token.count();
566+
let prev_rx_no_token = METRICS.mmds.rx_no_token.count();
563567
let actual_response = convert_to_response(mmds.clone(), request);
564568
assert_eq!(actual_response, expected_response);
565569
assert_eq!(prev_rx_invalid_token, METRICS.mmds.rx_invalid_token.count());
570+
assert_eq!(prev_rx_no_token, METRICS.mmds.rx_no_token.count());
566571

567572
// Test GET request with invalid token is accepted when v1 is configured.
568573
let (request, expected_response) = generate_request_and_expected_response(
@@ -572,12 +577,14 @@ mod tests {
572577
MediaType::ApplicationJson,
573578
);
574579
let prev_rx_invalid_token = METRICS.mmds.rx_invalid_token.count();
580+
let prev_rx_no_token = METRICS.mmds.rx_no_token.count();
575581
let actual_response = convert_to_response(mmds, request);
576582
assert_eq!(actual_response, expected_response);
577583
assert_eq!(
578584
prev_rx_invalid_token + 1,
579585
METRICS.mmds.rx_invalid_token.count()
580586
);
587+
assert_eq!(prev_rx_no_token, METRICS.mmds.rx_no_token.count());
581588
}
582589

583590
#[test]
@@ -713,9 +720,11 @@ mod tests {
713720
MediaType::ApplicationJson,
714721
);
715722
let prev_rx_invalid_token = METRICS.mmds.rx_invalid_token.count();
723+
let prev_rx_no_token = METRICS.mmds.rx_no_token.count();
716724
let actual_response = convert_to_response(mmds.clone(), request);
717725
assert_eq!(actual_response, expected_response);
718726
assert_eq!(prev_rx_invalid_token, METRICS.mmds.rx_invalid_token.count());
727+
assert_eq!(prev_rx_no_token, METRICS.mmds.rx_no_token.count());
719728

720729
// Test invalid customer header value is ignored if not PUT request to /latest/api/token.
721730
#[rustfmt::skip]
@@ -775,8 +784,10 @@ mod tests {
775784
let mut expected_response = Response::new(Version::Http10, StatusCode::Unauthorized);
776785
expected_response.set_content_type(MediaType::PlainText);
777786
expected_response.set_body(Body::new(VmmMmdsError::NoTokenProvided.to_string()));
787+
let prev_rx_no_token = METRICS.mmds.rx_no_token.count();
778788
let actual_response = convert_to_response(mmds.clone(), request);
779789
assert_eq!(actual_response, expected_response);
790+
assert_eq!(prev_rx_no_token + 1, METRICS.mmds.rx_no_token.count());
780791

781792
// Create a new MMDS token that expires in one second.
782793
let request = Request::try_from(
@@ -809,12 +820,14 @@ mod tests {
809820
expected_response.set_content_type(MediaType::PlainText);
810821
expected_response.set_body(Body::new(VmmMmdsError::InvalidToken.to_string()));
811822
let prev_rx_invalid_token = METRICS.mmds.rx_invalid_token.count();
823+
let prev_rx_no_token = METRICS.mmds.rx_no_token.count();
812824
let actual_response = convert_to_response(mmds.clone(), request);
813825
assert_eq!(actual_response, expected_response);
814826
assert_eq!(
815827
prev_rx_invalid_token + 1,
816828
METRICS.mmds.rx_invalid_token.count()
817829
);
830+
assert_eq!(prev_rx_no_token, METRICS.mmds.rx_no_token.count());
818831

819832
// Wait for the second token to expire.
820833
std::thread::sleep(Duration::from_secs(1));

tests/host_tools/fcmetrics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ def validate_fc_metrics(metrics):
186186
"rx_accepted_unusual",
187187
"rx_bad_eth",
188188
"rx_invalid_token",
189+
"rx_no_token",
189190
"rx_count",
190191
"tx_bytes",
191192
"tx_count",

0 commit comments

Comments
 (0)