Return ``(help_map, type_map, samples)`` from exposition text. samples keys are ``(metric_name, frozenset(labels.items()))``.
(exposition: str)
| 19 | |
| 20 | |
| 21 | def _parse(exposition: str) -> tuple[dict[str, str], dict[str, str], dict[tuple[str, frozenset[tuple[str, str]]], str]]: |
| 22 | """Return ``(help_map, type_map, samples)`` from exposition text. |
| 23 | |
| 24 | samples keys are ``(metric_name, frozenset(labels.items()))``. |
| 25 | """ |
| 26 | help_map: dict[str, str] = {} |
| 27 | type_map: dict[str, str] = {} |
| 28 | samples: dict[tuple[str, frozenset[tuple[str, str]]], str] = {} |
| 29 | for raw in exposition.splitlines(): |
| 30 | line = raw.strip() |
| 31 | if not line: |
| 32 | continue |
| 33 | if line.startswith("# HELP "): |
| 34 | name, _, text = line[len("# HELP ") :].partition(" ") |
| 35 | help_map[name] = text |
| 36 | continue |
| 37 | if line.startswith("# TYPE "): |
| 38 | name, _, kind = line[len("# TYPE ") :].partition(" ") |
| 39 | type_map[name] = kind |
| 40 | continue |
| 41 | match = _SAMPLE_RE.match(line) |
| 42 | assert match, f"unparseable exposition line: {line!r}" |
| 43 | name = match.group("name") |
| 44 | labels_blob = match.group("labels") or "" |
| 45 | labels: dict[str, str] = {} |
| 46 | if labels_blob: |
| 47 | inner = labels_blob[1:-1] |
| 48 | # Cheap parser — values never contain "," or "\"" in these tests. |
| 49 | for part in inner.split(","): |
| 50 | k, _, v = part.partition("=") |
| 51 | labels[k] = v.strip('"') |
| 52 | samples[(name, frozenset(labels.items()))] = match.group("value") |
| 53 | return help_map, type_map, samples |
| 54 | |
| 55 | |
| 56 | async def test_empty_accumulator_renders_zero_totals(): |
no test coverage detected