MCPcopy Index your code
hub / github.com/prometheus/client_python / _parse_sample

Function _parse_sample

prometheus_client/openmetrics/parser.py:111–139  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

109
110
111def _parse_sample(text):
112 separator = " # "
113 # Detect the labels in the text
114 label_start = _next_unquoted_char(text, '{')
115 if label_start == -1 or separator in text[:label_start]:
116 # We don't have labels, but there could be an exemplar.
117 name_end = _next_unquoted_char(text, ' ')
118 name = text[:name_end]
119 if not _is_valid_legacy_metric_name(name):
120 raise ValueError("invalid metric name:" + text)
121 # Parse the remaining text after the name
122 remaining_text = text[name_end + 1:]
123 value, timestamp, exemplar = _parse_remaining_text(remaining_text)
124 return Sample(name, {}, value, timestamp, exemplar)
125 name = text[:label_start]
126 label_end = _next_unquoted_char(text, '}')
127 labels = parse_labels(text[label_start + 1:label_end], True)
128 if not name:
129 # Name might be in the labels
130 if '__name__' not in labels:
131 raise ValueError
132 name = labels['__name__']
133 del labels['__name__']
134 elif '__name__' in labels:
135 raise ValueError("metric name specified more than once")
136 # Parsing labels succeeded, continue parsing the remaining text
137 remaining_text = text[label_end + 2:]
138 value, timestamp, exemplar = _parse_remaining_text(remaining_text)
139 return Sample(name, labels, value, timestamp, exemplar)
140
141
142def _parse_remaining_text(text):

Callers 1

Calls 5

_next_unquoted_charFunction · 0.85
_parse_remaining_textFunction · 0.85
SampleClass · 0.85
parse_labelsFunction · 0.85

Tested by

no test coverage detected