@@ -878,7 +878,7 @@ impl std::fmt::Write for LabelValueEncoder<'_> {
878
878
#[ cfg( test) ]
879
879
mod tests {
880
880
use super :: * ;
881
- use crate :: encoding:: EscapingScheme :: NoEscaping ;
881
+ use crate :: encoding:: EscapingScheme :: { NoEscaping , UnderscoreEscaping } ;
882
882
use crate :: encoding:: ValidationScheme :: UTF8Validation ;
883
883
use crate :: metrics:: exemplar:: HistogramWithExemplars ;
884
884
use crate :: metrics:: family:: Family ;
@@ -1028,6 +1028,29 @@ mod tests {
1028
1028
parse_with_python_client ( encoded) ;
1029
1029
}
1030
1030
1031
+ #[ test]
1032
+ fn encode_gauge_with_quoted_metric_name ( ) {
1033
+ let mut registry = RegistryBuilder :: new ( )
1034
+ . with_name_validation_scheme ( UTF8Validation )
1035
+ . with_escaping_scheme ( NoEscaping )
1036
+ . build ( ) ;
1037
+ let gauge = Gauge :: < f32 , AtomicU32 > :: default ( ) ;
1038
+ registry. register ( "my.gauge\" " , "My\n gau\n ge\" " , gauge. clone ( ) ) ;
1039
+
1040
+ gauge. set ( f32:: INFINITY ) ;
1041
+
1042
+ let mut encoded = String :: new ( ) ;
1043
+
1044
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
1045
+
1046
+ let expected = "# HELP \" my.gauge\" \" My\n gau\n ge\" .\n "
1047
+ . to_owned ( )
1048
+ + "# TYPE \" my.gauge\" \" gauge\n "
1049
+ + "{\" my.gauge\" \" } inf\n "
1050
+ + "# EOF\n " ;
1051
+ assert_eq ! ( expected, encoded) ;
1052
+ }
1053
+
1031
1054
#[ test]
1032
1055
fn encode_counter_family ( ) {
1033
1056
let mut registry = Registry :: default ( ) ;
@@ -1109,6 +1132,37 @@ mod tests {
1109
1132
assert_eq ! ( expected, encoded) ;
1110
1133
}
1111
1134
1135
+ #[ test]
1136
+ fn escaped_encode_counter_family_with_prefix_with_label_with_quoted_metric_and_label_names ( ) {
1137
+ let mut registry = RegistryBuilder :: new ( )
1138
+ . with_name_validation_scheme ( UTF8Validation )
1139
+ . with_escaping_scheme ( UnderscoreEscaping )
1140
+ . build ( ) ;
1141
+ let sub_registry = registry. sub_registry_with_prefix ( "my.prefix" ) ;
1142
+ let sub_sub_registry = sub_registry
1143
+ . sub_registry_with_label ( ( Cow :: Borrowed ( "my.key" ) , Cow :: Borrowed ( "my_value" ) ) ) ;
1144
+ let family = Family :: < Vec < ( String , String ) > , Counter > :: default ( ) ;
1145
+ sub_sub_registry. register ( "my_counter_family" , "My counter family" , family. clone ( ) ) ;
1146
+
1147
+ family
1148
+ . get_or_create ( & vec ! [
1149
+ ( "method" . to_string( ) , "GET" . to_string( ) ) ,
1150
+ ( "status" . to_string( ) , "200" . to_string( ) ) ,
1151
+ ] )
1152
+ . inc ( ) ;
1153
+
1154
+ let mut encoded = String :: new ( ) ;
1155
+
1156
+ encode ( & mut encoded, & registry) . unwrap ( ) ;
1157
+
1158
+ let expected = "# HELP my_prefix_my_counter_family My counter family.\n "
1159
+ . to_owned ( )
1160
+ + "# TYPE my_prefix_my_counter_family counter\n "
1161
+ + "my_prefix_my_counter_family_total{my_key=\" my_value\" ,method=\" GET\" ,status=\" 200\" } 1\n "
1162
+ + "# EOF\n " ;
1163
+ assert_eq ! ( expected, encoded) ;
1164
+ }
1165
+
1112
1166
#[ test]
1113
1167
fn encode_info ( ) {
1114
1168
let mut registry = Registry :: default ( ) ;
0 commit comments