44use argh:: { FromArgs , TopLevelCommand } ;
55use chrono:: { DateTime , Utc } ;
66use std:: { path:: Path , str:: FromStr , time:: Duration } ;
7+ use write_metrics:: write_metrics;
78
89mod add_battery_reading;
910mod config_file;
@@ -13,6 +14,7 @@ mod report_sync;
1314mod session;
1415mod sync;
1516mod write_attributes;
17+ mod write_metrics;
1618
1719use crate :: {
1820 cli:: version:: format_version,
@@ -108,6 +110,7 @@ enum MemfaultctlCommand {
108110 StartSession ( StartSessionArgs ) ,
109111 EndSession ( EndSessionArgs ) ,
110112 AddCustomDataRecording ( AddCustomDataRecordingArgs ) ,
113+ WriteMetrics ( WriteMetricsArgs ) ,
111114}
112115
113116#[ derive( FromArgs ) ]
@@ -188,6 +191,15 @@ struct WriteAttributesArgs {
188191 attributes : Vec < DeviceAttribute > ,
189192}
190193
194+ #[ derive( FromArgs ) ]
195+ /// write metrics(s) to memfaultd
196+ #[ argh( subcommand, name = "write-metrics" ) ]
197+ struct WriteMetricsArgs {
198+ /// metrics to write, in the format <VAR1=VAL1 ...>, or in statsd format <name:value|type ...>
199+ #[ argh( positional) ]
200+ metrics : Vec < KeyedMetricReading > ,
201+ }
202+
191203#[ derive( FromArgs ) ]
192204/// add a reading to memfaultd's battery metrics in format "[status string]:[0.0-100.0]".
193205#[ argh( subcommand, name = "add-battery-reading" ) ]
@@ -323,10 +335,11 @@ pub fn main() -> Result<()> {
323335 ) )
324336 } else {
325337 check_data_collection_enabled ( & config, "write attributes" ) ?;
326- MarEntryBuilder :: new ( & mar_staging_path) ?
327- . set_metadata ( Metadata :: new_device_attributes ( attributes) )
328- . save ( & network_config)
329- . map ( |_entry| ( ) )
338+ let metrics = attributes
339+ . into_iter ( )
340+ . map ( KeyedMetricReading :: try_from)
341+ . collect :: < Result < Vec < KeyedMetricReading > > > ( ) ?;
342+ write_metrics ( metrics, & config)
330343 }
331344 }
332345 MemfaultctlCommand :: AddBatteryReading ( AddBatteryReadingArgs { reading_string } ) => {
@@ -379,5 +392,10 @@ pub fn main() -> Result<()> {
379392 . save ( & network_config)
380393 . map ( |_entry| ( ) )
381394 }
395+ MemfaultctlCommand :: WriteMetrics ( WriteMetricsArgs { metrics } ) => {
396+ check_data_collection_enabled ( & config, "write metrics" ) ?;
397+
398+ write_metrics ( metrics, & config)
399+ }
382400 }
383401}
0 commit comments