33"""Tests the metrics system."""
44
55import os
6+ from pathlib import Path
67
78import host_tools .drive as drive_tools
89from framework .artifacts import GUEST_KERNEL_DEFAULT , pin_guest_kernel
@@ -23,6 +24,88 @@ def test_flush_metrics(uvm):
2324 validate_fc_metrics (metrics )
2425
2526
27+ def _configure_metrics_via_api (microvm , ** kwargs ):
28+ """Configure metrics through the API rather than the `--metrics-path` CLI
29+ option (the two ways of initializing metrics are mutually exclusive), and
30+ wire up the host-side metrics file so the line can be read back."""
31+ microvm .spawn (metrics_path = None )
32+ microvm .basic_config ()
33+
34+ metrics_path = Path (microvm .path ) / "metrics.ndjson"
35+ metrics_path .touch ()
36+ microvm .metrics_file = metrics_path
37+
38+ microvm .api .metrics .put (
39+ metrics_path = microvm .create_jailed_resource (metrics_path ), ** kwargs
40+ )
41+ microvm .start ()
42+
43+
44+ @pin_guest_kernel (GUEST_KERNEL_DEFAULT )
45+ def test_metrics_default_shape (uvm ):
46+ """
47+ Check that, with neither field configured, a metrics line carries neither
48+ the top-level `id` field nor the `properties` map.
49+ """
50+ microvm = uvm
51+ microvm .spawn ()
52+ microvm .basic_config ()
53+ microvm .start ()
54+
55+ metrics = microvm .flush_metrics ()
56+ validate_fc_metrics (metrics )
57+ assert "id" not in metrics
58+ assert "properties" not in metrics
59+
60+
61+ @pin_guest_kernel (GUEST_KERNEL_DEFAULT )
62+ def test_metrics_emit_id (uvm ):
63+ """
64+ Check that `emit_id` makes every metrics line carry the top-level `id` field
65+ holding the microVM instance id, without requiring custom properties.
66+ """
67+ microvm = uvm
68+ _configure_metrics_via_api (microvm , emit_id = True )
69+
70+ metrics = microvm .flush_metrics ()
71+ validate_fc_metrics (metrics )
72+ assert metrics ["id" ] == microvm .id
73+ assert "properties" not in metrics
74+
75+
76+ @pin_guest_kernel (GUEST_KERNEL_DEFAULT )
77+ def test_metrics_with_properties (uvm ):
78+ """
79+ Check that configuring custom properties via `PUT /metrics` makes every
80+ metrics line carry the operator-defined `properties` map, without emitting
81+ the `id` field when `emit_id` is not set.
82+ """
83+ microvm = uvm
84+ properties = {"customer_id" : "1234" , "bundle_id" : "fn-abc" }
85+ _configure_metrics_via_api (microvm , properties = properties )
86+
87+ metrics = microvm .flush_metrics ()
88+ validate_fc_metrics (metrics )
89+ assert metrics ["properties" ] == properties
90+ assert "id" not in metrics
91+
92+
93+ @pin_guest_kernel (GUEST_KERNEL_DEFAULT )
94+ def test_metrics_emit_id_and_properties (uvm ):
95+ """
96+ Check that `emit_id` and custom properties can be configured together, so a
97+ metrics line carries both the top-level `id` field and the `properties` map.
98+ """
99+ microvm = uvm
100+ properties = {"customer_id" : "1234" , "bundle_id" : "fn-abc" }
101+ _configure_metrics_via_api (microvm , emit_id = True , properties = properties )
102+
103+ metrics = microvm .flush_metrics ()
104+ validate_fc_metrics (metrics )
105+ assert metrics ["id" ] == microvm .id
106+ assert metrics ["properties" ] == properties
107+
108+
26109@pin_guest_kernel (GUEST_KERNEL_DEFAULT )
27110def test_net_metrics (uvm ):
28111 """
0 commit comments