77original prometheus_client implementation across representative inputs.
88"""
99
10- import pytest
11-
1210from datadog_checks .base .checks .openmetrics .parser_optimizations import (
1311 _next_unquoted_char ,
1412)
@@ -71,6 +69,20 @@ def test_last_char_is_target(self):
7169 def test_multiple_occurrences_returns_first (self ):
7270 assert _next_unquoted_char ('a{b{c' , '{' ) == 1
7371
72+ def test_skip_target_char_inside_quotes (self ):
73+ # comma inside quoted value must not be returned
74+ assert _next_unquoted_char ('a="apn,gw",b' , ',' ) == 10
75+
76+ def test_skip_brace_inside_quotes (self ):
77+ assert _next_unquoted_char ('label="val}ue"}' , '}' ) == 14
78+
79+ def test_skip_equals_inside_quotes (self ):
80+ assert _next_unquoted_char ('label="a=b"} 1' , '}' ) == 11
81+
82+ def test_escaped_quote_not_treated_as_delimiter (self ):
83+ # backslash-escaped quote does not close the quoted region
84+ assert _next_unquoted_char (r'label="val\"still,inside",next' , ',' ) == 25
85+
7486
7587class TestNextUnquotedCharWithRealMetrics :
7688 """Tests using real Prometheus metric line patterns."""
@@ -113,11 +125,7 @@ class TestParseFullMetricText:
113125 def test_parse_simple_metrics (self ):
114126 from prometheus_client .parser import text_string_to_metric_families
115127
116- text = (
117- '# HELP test_gauge A test gauge.\n '
118- '# TYPE test_gauge gauge\n '
119- 'test_gauge 42\n '
120- )
128+ text = '# HELP test_gauge A test gauge.\n # TYPE test_gauge gauge\n test_gauge 42\n '
121129 families = list (text_string_to_metric_families (text ))
122130 assert len (families ) == 1
123131 assert families [0 ].name == 'test_gauge'
@@ -159,11 +167,7 @@ def test_parse_histogram(self):
159167 def test_parse_escaped_label_value (self ):
160168 from prometheus_client .parser import text_string_to_metric_families
161169
162- text = (
163- '# HELP test_metric A test.\n '
164- '# TYPE test_metric gauge\n '
165- 'test_metric{label="value with \\ "quotes\\ ""} 1\n '
166- )
170+ text = '# HELP test_metric A test.\n # TYPE test_metric gauge\n test_metric{label="value with \\ "quotes\\ ""} 1\n '
167171 families = list (text_string_to_metric_families (text ))
168172 assert len (families ) == 1
169173 assert families [0 ].samples [0 ].labels == {'label' : 'value with "quotes"' }
@@ -187,21 +191,26 @@ def test_parse_multiple_families(self):
187191 def test_parse_empty_label_value (self ):
188192 from prometheus_client .parser import text_string_to_metric_families
189193
190- text = (
191- '# HELP test_metric A test.\n '
192- '# TYPE test_metric gauge\n '
193- 'test_metric{label=""} 1\n '
194- )
194+ text = '# HELP test_metric A test.\n # TYPE test_metric gauge\n test_metric{label=""} 1\n '
195195 families = list (text_string_to_metric_families (text ))
196196 assert families [0 ].samples [0 ].labels == {'label' : '' }
197197
198198 def test_parse_newline_in_label_value (self ):
199199 from prometheus_client .parser import text_string_to_metric_families
200200
201+ text = '# HELP test_metric A test.\n # TYPE test_metric gauge\n test_metric{label="line1\\ nline2"} 1\n '
202+ families = list (text_string_to_metric_families (text ))
203+ assert families [0 ].samples [0 ].labels == {'label' : 'line1\n line2' }
204+
205+ def test_parse_comma_in_label_value (self ):
206+ from prometheus_client .parser import text_string_to_metric_families
207+
201208 text = (
202- '# HELP test_metric A test .\n '
203- '# TYPE test_metric gauge\n '
204- 'test_metric{label="line1 \\ nline2"} 1 \n '
209+ '# HELP apn_active_connections Active connections .\n '
210+ '# TYPE apn_active_connections gauge\n '
211+ 'apn_active_connections{func="apn,gw",proto="tcp"} 8 \n '
205212 )
206213 families = list (text_string_to_metric_families (text ))
207- assert families [0 ].samples [0 ].labels == {'label' : 'line1\n line2' }
214+ assert len (families ) == 1
215+ assert families [0 ].samples [0 ].labels == {'func' : 'apn,gw' , 'proto' : 'tcp' }
216+ assert families [0 ].samples [0 ].value == 8
0 commit comments