1- use std:: { io, path:: PathBuf } ;
1+ use std:: {
2+ io,
3+ path:: { Path , PathBuf } ,
4+ } ;
25
36use metrics:: gauge;
47use tokio:: fs;
8+ use tracing:: debug;
59
610#[ derive( thiserror:: Error , Debug ) ]
711pub enum Error {
@@ -68,7 +72,7 @@ pub(crate) async fn poll(path: PathBuf, labels: &[(String, String)]) -> Result<(
6872 gauge ! ( metric_prefix, labels) . set ( value) ;
6973 } else {
7074 // Key-value pairs
71- if kv_pairs ( content, & metric_prefix, labels) . is_err ( ) {
75+ if kv_pairs ( & file_path , content, & metric_prefix, labels) . is_err ( ) {
7276 // File may fail to parse, for instance cgroup.controllers
7377 // is a list of strings.
7478 continue ;
@@ -80,9 +84,31 @@ pub(crate) async fn poll(path: PathBuf, labels: &[(String, String)]) -> Result<(
8084 Ok ( ( ) )
8185}
8286
83- fn kv_pairs ( content : & str , metric_prefix : & str , labels : & [ ( String , String ) ] ) -> Result < ( ) , Error > {
87+ fn kv_pairs (
88+ file_path : & Path ,
89+ content : & str ,
90+ metric_prefix : & str ,
91+ labels : & [ ( String , String ) ] ,
92+ ) -> Result < ( ) , Error > {
8493 for line in content. lines ( ) {
8594 let mut parts = line. split_whitespace ( ) ;
95+ if let Some ( key) = parts. next ( ) {
96+ if let Some ( value_str) = parts. next ( ) {
97+ let value: f64 = value_str. parse ( ) ?;
98+ let metric_name = format ! ( "{metric_prefix}.{key}" ) ;
99+ gauge ! ( metric_name, labels) . set ( value) ;
100+ } else {
101+ debug ! (
102+ "[{path}] missing value in key/value pair: {content}" ,
103+ path = file_path. to_string_lossy( ) ,
104+ ) ;
105+ }
106+ } else {
107+ debug ! (
108+ "[{path} missing key in key/value pair: {content}" ,
109+ path = file_path. to_string_lossy( ) ,
110+ ) ;
111+ }
86112 let key = parts. next ( ) . expect ( "malformed key-value pair" ) ;
87113 let value_str = parts. next ( ) . expect ( "malformed key-value pair" ) ;
88114 let value: f64 = value_str. parse ( ) ?;
0 commit comments