@@ -238,10 +238,11 @@ def _parse_labels(text):
238238
239239
240240def _parse_sample (text ):
241+ seperator = " # "
241242 # Detect the labels in the text
242243 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.
245246 name_end = text .index (" " )
246247 name = text [:name_end ]
247248 # Parse the remaining text after the name
@@ -250,8 +251,7 @@ def _parse_sample(text):
250251 return Sample (name , {}, value , timestamp , exemplar )
251252 # The name is before the labels
252253 name = text [:label_start ]
253- seperator = " # "
254- if text .count (seperator ) == 0 :
254+ if seperator not in text :
255255 # Line doesn't contain an exemplar
256256 # We can use `rindex` to find `label_end`
257257 label_end = text .rindex ("}" )
@@ -261,7 +261,7 @@ def _parse_sample(text):
261261 # Line potentially contains an exemplar
262262 # Fallback to parsing labels with a state machine
263263 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 )
265265 # Parsing labels succeeded, continue parsing the remaining text
266266 remaining_text = text [label_end + 2 :]
267267 value , timestamp , exemplar = _parse_remaining_text (remaining_text )
@@ -564,9 +564,9 @@ def build_metric(name, documentation, typ, unit, samples):
564564 '_gsum' ] and sample .value < 0 :
565565 raise ValueError ("Counter-like samples cannot be negative: " + line )
566566 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 )
570570
571571 if name is not None :
572572 yield build_metric (name , documentation , typ , unit , samples )
0 commit comments