(text)
| 242 | |
| 243 | |
| 244 | def _parse_sample(text): |
| 245 | separator = " # " |
| 246 | # Detect the labels in the text |
| 247 | label_start = _next_unquoted_char(text, '{') |
| 248 | if label_start == -1 or separator in text[:label_start]: |
| 249 | # We don't have labels, but there could be an exemplar. |
| 250 | name_end = _next_unquoted_char(text, ' \t') |
| 251 | name = text[:name_end].strip() |
| 252 | if not _is_valid_legacy_metric_name(name): |
| 253 | raise ValueError("invalid metric name:" + text) |
| 254 | # Parse the remaining text after the name |
| 255 | remaining_text = text[name_end + 1:] |
| 256 | value, timestamp = _parse_value_and_timestamp(remaining_text) |
| 257 | return Sample(name, {}, value, timestamp) |
| 258 | name = text[:label_start].strip() |
| 259 | label_end = _next_unquoted_char(text[label_start:], '}') + label_start |
| 260 | labels = parse_labels(text[label_start + 1:label_end], False) |
| 261 | if not name: |
| 262 | # Name might be in the labels |
| 263 | if '__name__' not in labels: |
| 264 | raise ValueError |
| 265 | name = labels['__name__'] |
| 266 | del labels['__name__'] |
| 267 | elif '__name__' in labels: |
| 268 | raise ValueError("metric name specified more than once") |
| 269 | # Parsing labels succeeded, continue parsing the remaining text |
| 270 | remaining_text = text[label_end + 1:] |
| 271 | value, timestamp = _parse_value_and_timestamp(remaining_text) |
| 272 | return Sample(name, labels, value, timestamp) |
| 273 | |
| 274 | |
| 275 | def text_fd_to_metric_families(fd: TextIO) -> Iterable[Metric]: |
no test coverage detected