Escapes the label name and puts it in quotes iff the name does not conform to the legacy Prometheus character set.
(s: str, escaping: str = UNDERSCORES)
| 194 | |
| 195 | |
| 196 | def escape_label_name(s: str, escaping: str = UNDERSCORES) -> str: |
| 197 | """Escapes the label name and puts it in quotes iff the name does not |
| 198 | conform to the legacy Prometheus character set. |
| 199 | """ |
| 200 | if len(s) == 0: |
| 201 | return s |
| 202 | if escaping == ALLOWUTF8: |
| 203 | if not _is_valid_legacy_labelname(s): |
| 204 | return '"{}"'.format(_escape(s, escaping, _is_legacy_labelname_rune)) |
| 205 | return _escape(s, escaping, _is_legacy_labelname_rune) |
| 206 | elif escaping == UNDERSCORES: |
| 207 | if _is_valid_legacy_labelname(s): |
| 208 | return s |
| 209 | return _escape(s, escaping, _is_legacy_labelname_rune) |
| 210 | elif escaping == DOTS: |
| 211 | return _escape(s, escaping, _is_legacy_labelname_rune) |
| 212 | elif escaping == VALUES: |
| 213 | if _is_valid_legacy_labelname(s): |
| 214 | return s |
| 215 | return _escape(s, escaping, _is_legacy_labelname_rune) |
| 216 | return s |
| 217 | |
| 218 | |
| 219 | def _escape(s: str, escaping: str, valid_rune_fn: Callable[[str, int], bool]) -> str: |