Determines if the line has a native histogram sample, and parses it if so.
(text, suffixes)
| 232 | |
| 233 | |
| 234 | def _parse_nh_sample(text, suffixes): |
| 235 | """Determines if the line has a native histogram sample, and parses it if so.""" |
| 236 | labels_start = _next_unquoted_char(text, '{') |
| 237 | labels_end = -1 |
| 238 | |
| 239 | # Finding a native histogram sample requires careful parsing of |
| 240 | # possibly-quoted text, which can appear in metric names, label names, and |
| 241 | # values. |
| 242 | # |
| 243 | # First, we need to determine if there are metric labels. Find the space |
| 244 | # between the metric definition and the rest of the line. Look for unquoted |
| 245 | # space or {. |
| 246 | i = 0 |
| 247 | has_metric_labels = False |
| 248 | i = _next_unquoted_char(text, ' {') |
| 249 | if i == -1: |
| 250 | return |
| 251 | |
| 252 | # If the first unquoted char was a {, then that is the metric labels (which |
| 253 | # could contain a UTF-8 metric name). |
| 254 | if text[i] == '{': |
| 255 | has_metric_labels = True |
| 256 | # Consume the labels -- jump ahead to the close bracket. |
| 257 | labels_end = i = _next_unquoted_char(text, '}', i) |
| 258 | if labels_end == -1: |
| 259 | raise ValueError |
| 260 | |
| 261 | # If there is no subsequent unquoted {, then it's definitely not a nh. |
| 262 | nh_value_start = _next_unquoted_char(text, '{', i + 1) |
| 263 | if nh_value_start == -1: |
| 264 | return |
| 265 | |
| 266 | # Edge case: if there is an unquoted # between the metric definition and the {, |
| 267 | # then this is actually an exemplar |
| 268 | exemplar = _next_unquoted_char(text, '#', i + 1) |
| 269 | if exemplar != -1 and exemplar < nh_value_start: |
| 270 | return |
| 271 | |
| 272 | nh_value_end = _next_unquoted_char(text, '}', nh_value_start) |
| 273 | if nh_value_end == -1: |
| 274 | raise ValueError |
| 275 | |
| 276 | if has_metric_labels: |
| 277 | labelstext = text[labels_start + 1:labels_end] |
| 278 | labels = parse_labels(labelstext, True) |
| 279 | name_end = labels_start |
| 280 | name = text[:name_end] |
| 281 | if name.endswith(suffixes): |
| 282 | raise ValueError("the sample name of a native histogram with labels should have no suffixes", name) |
| 283 | if not name: |
| 284 | # Name might be in the labels |
| 285 | if '__name__' not in labels: |
| 286 | raise ValueError |
| 287 | name = labels['__name__'] |
| 288 | del labels['__name__'] |
| 289 | # Edge case: the only "label" is the name definition. |
| 290 | if not labels: |
| 291 | labels = None |
no test coverage detected