Returns the metrics from the registry in latest text format as a string.
(registry, escaping=UNDERSCORES, version="1.0.0")
| 56 | |
| 57 | |
| 58 | def generate_latest(registry, escaping=UNDERSCORES, version="1.0.0"): |
| 59 | '''Returns the metrics from the registry in latest text format as a string.''' |
| 60 | output = [] |
| 61 | for metric in registry.collect(): |
| 62 | try: |
| 63 | mname = metric.name |
| 64 | output.append('# HELP {} {}\n'.format( |
| 65 | escape_metric_name(mname, escaping), _escape(metric.documentation, ALLOWUTF8, _is_legacy_labelname_rune))) |
| 66 | output.append(f'# TYPE {escape_metric_name(mname, escaping)} {metric.type}\n') |
| 67 | if metric.unit: |
| 68 | output.append(f'# UNIT {escape_metric_name(mname, escaping)} {metric.unit}\n') |
| 69 | for s in metric.samples: |
| 70 | if escaping == ALLOWUTF8 and not _is_valid_legacy_metric_name(s.name): |
| 71 | labelstr = escape_metric_name(s.name, escaping) |
| 72 | if s.labels: |
| 73 | labelstr += ',' |
| 74 | else: |
| 75 | labelstr = '' |
| 76 | |
| 77 | if s.labels: |
| 78 | items = sorted(s.labels.items()) |
| 79 | # Label values always support UTF-8 |
| 80 | labelstr += ','.join( |
| 81 | ['{}="{}"'.format( |
| 82 | escape_label_name(k, escaping), _escape(v, ALLOWUTF8, _is_legacy_labelname_rune)) |
| 83 | for k, v in items]) |
| 84 | if labelstr: |
| 85 | labelstr = "{" + labelstr + "}" |
| 86 | if s.exemplar: |
| 87 | exemplarstr = _compose_exemplar_string(metric, s, s.exemplar) |
| 88 | else: |
| 89 | exemplarstr = '' |
| 90 | timestamp = '' |
| 91 | if s.timestamp is not None: |
| 92 | timestamp = f' {s.timestamp}' |
| 93 | |
| 94 | # Skip native histogram samples entirely if version < 2.0.0 |
| 95 | if s.native_histogram and parse_version(version) < (2, 0, 0): |
| 96 | continue |
| 97 | |
| 98 | native_histogram = '' |
| 99 | negative_spans = '' |
| 100 | negative_deltas = '' |
| 101 | positive_spans = '' |
| 102 | positive_deltas = '' |
| 103 | |
| 104 | if s.native_histogram: |
| 105 | # Initialize basic nh template |
| 106 | nh_sample_template = '{{count:{},sum:{},schema:{},zero_threshold:{},zero_count:{}' |
| 107 | |
| 108 | args = [ |
| 109 | s.native_histogram.count_value, |
| 110 | s.native_histogram.sum_value, |
| 111 | s.native_histogram.schema, |
| 112 | s.native_histogram.zero_threshold, |
| 113 | s.native_histogram.zero_count, |
| 114 | ] |
| 115 |
nothing calls this directly
no test coverage detected