@@ -238,10 +238,11 @@ def _parse_labels(text):
238
238
239
239
240
240
def _parse_sample (text ):
241
+ seperator = " # "
241
242
# Detect the labels in the text
242
243
label_start = text .find ("{" )
243
- if label_start == - 1 :
244
- # We don't have labels
244
+ if label_start == - 1 or seperator in text [: label_start ] :
245
+ # We don't have labels, but there could be an exemplar.
245
246
name_end = text .index (" " )
246
247
name = text [:name_end ]
247
248
# Parse the remaining text after the name
@@ -250,8 +251,7 @@ def _parse_sample(text):
250
251
return Sample (name , {}, value , timestamp , exemplar )
251
252
# The name is before the labels
252
253
name = text [:label_start ]
253
- seperator = " # "
254
- if text .count (seperator ) == 0 :
254
+ if seperator not in text :
255
255
# Line doesn't contain an exemplar
256
256
# We can use `rindex` to find `label_end`
257
257
label_end = text .rindex ("}" )
@@ -261,7 +261,7 @@ def _parse_sample(text):
261
261
# Line potentially contains an exemplar
262
262
# Fallback to parsing labels with a state machine
263
263
labels , labels_len = _parse_labels_with_state_machine (text [label_start + 1 :])
264
- label_end = labels_len + len (name )
264
+ label_end = labels_len + len (name )
265
265
# Parsing labels succeeded, continue parsing the remaining text
266
266
remaining_text = text [label_end + 2 :]
267
267
value , timestamp , exemplar = _parse_remaining_text (remaining_text )
@@ -564,9 +564,9 @@ def build_metric(name, documentation, typ, unit, samples):
564
564
'_gsum' ] and sample .value < 0 :
565
565
raise ValueError ("Counter-like samples cannot be negative: " + line )
566
566
if sample .exemplar and not (
567
- typ in ['histogram' , 'gaugehistogram' ]
568
- and sample .name .endswith ('_bucket' )):
569
- raise ValueError ("Invalid line only histogram/gaugehistogram buckets can have exemplars: " + line )
567
+ ( typ in ['histogram' , 'gaugehistogram' ] and sample . name . endswith ( '_bucket' ))
568
+ or ( typ in [ 'counter' ] and sample .name .endswith ('_total' ) )):
569
+ raise ValueError ("Invalid line only histogram/gaugehistogram buckets and counters can have exemplars: " + line )
570
570
571
571
if name is not None :
572
572
yield build_metric (name , documentation , typ , unit , samples )
8000
table>
0 commit comments