| 1 |
import math |
| 2 |
import unittest |
| 3 |
|
| 4 |
from prometheus_client.core import ( |
| 5 |
CollectorRegistry, CounterMetricFamily, GaugeMetricFamily, |
| 6 |
HistogramMetricFamily, Metric, Sample, SummaryMetricFamily, |
| 7 |
) |
| 8 |
from prometheus_client.exposition import generate_latest |
| 9 |
from prometheus_client.parser import text_string_to_metric_families |
| 10 |
|
| 11 |
|
| 12 |
class TestParse(unittest.TestCase): |
| 13 |
def assertEqualMetrics(self, first, second, msg=None): |
| 14 |
super().assertEqual(first, second, msg) |
| 15 |
|
| 16 |
|
| 17 |
for a, b in zip(first, second): |
| 18 |
for sa, sb in zip(a.samples, b.samples): |
| 19 |
assert sa.name == sb.name |
| 20 |
|
| 21 |
def test_simple_counter(self): |
| 22 |
families = text_string_to_metric_families("""# TYPE a counter |
| 23 |
# HELP a help |
| 24 |
a 1 |
| 25 |
""") |
| 26 |
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families)) |
| 27 |
|
| 28 |
def test_simple_gauge(self): |
| 29 |
families = text_string_to_metric_families("""# TYPE a gauge |
| 30 |
# HELP a help |
| 31 |
a 1 |
| 32 |
""") |
| 33 |
self.assertEqualMetrics([GaugeMetricFamily("a", "help", value=1)], list(families)) |
| 34 |
|
| 35 |
def test_simple_summary(self): |
| 36 |
families = text_string_to_metric_families("""# TYPE a summary |
| 37 |
# HELP a help |
| 38 |
a_count 1 |
| 39 |
a_sum 2 |
| 40 |
""") |
| 41 |
summary = SummaryMetricFamily("a", "help", count_value=1, sum_value=2) |
| 42 |
self.assertEqualMetrics([summary], list(families)) |
| 43 |
|
| 44 |
def test_summary_quantiles(self): |
| 45 |
families = text_string_to_metric_families("""# TYPE a summary |
| 46 |
# HELP a help |
| 47 |
a_count 1 |
| 48 |
a_sum 2 |
| 49 |
a{quantile="0.5"} 0.7 |
| 50 |
""") |
| 51 |
|
| 52 |
|
| 53 |
metric_family = SummaryMetricFamily("a", "help", count_value=1, sum_value=2) |
| 54 |
metric_family.add_sample("a", {"quantile": "0.5"}, 0.7) |
| 55 |
self.assertEqualMetrics([metric_family], list(families)) |
| 56 |
|
| 57 |
def test_simple_histogram(self): |
| 58 |
families = text_string_to_metric_families("""# TYPE a histogram |
| 59 |
# HELP a help |
| 60 |
a_bucket{le="1"} 0 |
| 61 |
a_bucket{le="+Inf"} 3 |
| 62 |
a_count 3 |
| 63 |
a_sum 2 |
| 64 |
""") |
| 65 |
self.assertEqualMetrics([HistogramMetricFamily("a", "help", sum_value=2, buckets=[("1", 0.0), ("+Inf", 3.0)])], |
| 66 |
list(families)) |
| 67 |
|
| 68 |
def test_no_metadata(self): |
| 69 |
families = text_string_to_metric_families("""a 1 |
| 70 |
""") |
| 71 |
metric_family = Metric("a", "", "untyped") |
| 72 |
metric_family.add_sample("a", {}, 1) |
| 73 |
self.assertEqualMetrics([metric_family], list(families)) |
| 74 |
|
| 75 |
def test_untyped(self): |
| 76 |
|
| 77 |
families = text_string_to_metric_families("""# HELP redis_connected_clients Redis connected clients |
| 78 |
# TYPE redis_connected_clients untyped |
| 79 |
redis_connected_clients{instance="rough-snowflake-web",port="6380"} 10.0 |
| 80 |
redis_connected_clients{instance="rough-snowflake-web",port="6381"} 12.0 |
| 81 |
""") |
| 82 |
m = Metric("redis_connected_clients", "Redis connected clients", "untyped") |
| 83 |
m.samples = [ |
| 84 |
Sample("redis_connected_clients", {"instance": "rough-snowflake-web", "port": "6380"}, 10), |
| 85 |
Sample("redis_connected_clients", {"instance": "rough-snowflake-web", "port": "6381"}, 12), |
| 86 |
] |
| 87 |
self.assertEqualMetrics([m], list(families)) |
| 88 |
|
| 89 |
def test_type_help_switched(self): |
| 90 |
families = text_string_to_metric_families("""# HELP a help |
| 91 |
# TYPE a counter |
| 92 |
a 1 |
| 93 |
""") |
| 94 |
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families)) |
| 95 |
|
| 96 |
def test_blank_lines_and_comments(self): |
| 97 |
families = text_string_to_metric_families(""" |
| 98 |
# TYPE a counter |
| 99 |
# FOO a |
| 100 |
# BAR b |
| 101 |
# HELP a help |
| 102 |
|
| 103 |
a 1 |
| 104 |
""") |
| 105 |
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families)) |
| 106 |
|
| 107 |
def test_tabs(self): |
| 108 |
families = text_string_to_metric_families("""#\tTYPE\ta\tcounter |
| 109 |
#\tHELP\ta\thelp |
| 110 |
a\t1 |
| 111 |
""") |
| 112 |
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families)) |
| 113 |
|
| 114 |
def test_labels_with_curly_braces(self): |
| 115 |
families = text_string_to_metric_families("""# TYPE a counter |
| 116 |
# HELP a help |
| 117 |
a{foo="bar", bar="b{a}z"} 1 |
| 118 |
""") |
| 119 |
metric_family = CounterMetricFamily("a", "help", labels=["foo", "bar"]) |
| 120 |
metric_family.add_metric(["bar", "b{a}z"], 1) |
| 121 |
self.assertEqualMetrics([metric_family], list(families)) |
| 122 |
|
| 123 |
def test_empty_help(self): |
| 124 |
families = text_string_to_metric_families("""# TYPE a counter |
| 125 |
# HELP a |
| 126 |
a 1 |
| 127 |
""") |
| 128 |
self.assertEqualMetrics([CounterMetricFamily("a", "", value=1)], list(families)) |
| 129 |
|
| 130 |
def test_labels_and_infinite(self): |
| 131 |
families = text_string_to_metric_families("""# TYPE a counter |
| 132 |
# HELP a help |
| 133 |
a{foo="bar"} +Inf |
| 134 |
a{foo="baz"} -Inf |
| 135 |
""") |
| 136 |
metric_family = CounterMetricFamily("a", "help", labels=["foo"]) |
| 137 |
metric_family.add_metric(["bar"], float('inf')) |
| 138 |
metric_family.add_metric(["baz"], float('-inf')) |
| 139 |
self.assertEqualMetrics([metric_family], list(families)) |
| 140 |
|
| 141 |
def test_spaces(self): |
| 142 |
families = text_string_to_metric_families("""# TYPE a counter |
| 143 |
# HELP a help |
| 144 |
a{ foo = "bar" } 1 |
| 145 |
a\t\t{\t\tfoo\t\t=\t\t"baz"\t\t}\t\t2 |
| 146 |
a { foo = "buz" } 3 |
| 147 |
a\t { \t foo\t = "biz"\t } \t 4 |
| 148 |
a \t{\t foo = "boz"\t}\t 5 |
| 149 |
a{foo="bez"}6 |
| 150 |
""") |
| 151 |
metric_family = CounterMetricFamily("a", "help", labels=["foo"]) |
| 152 |
metric_family.add_metric(["bar"], 1) |
| 153 |
metric_family.add_metric(["baz"], 2) |
| 154 |
metric_family.add_metric(["buz"], 3) |
| 155 |
metric_family.add_metric(["biz"], 4) |
| 156 |
metric_family.add_metric(["boz"], 5) |
| 157 |
metric_family.add_metric(["bez"], 6) |
| 158 |
self.assertEqualMetrics([metric_family], list(families)) |
| 159 |
|
| 160 |
def test_commas(self): |
| 161 |
families = text_string_to_metric_families("""# TYPE a counter |
| 162 |
# HELP a help |
| 163 |
a{foo="bar",} 1 |
| 164 |
a{foo="baz", } 1 |
| 165 |
# TYPE b counter |
| 166 |
# HELP b help |
| 167 |
b{,} 2 |
| 168 |
# TYPE c counter |
| 169 |
# HELP c help |
| 170 |
c{ ,} 3 |
| 171 |
# TYPE d counter |
| 172 |
# HELP d help |
| 173 |
d{, } 4 |
| 174 |
""") |
| 175 |
a = CounterMetricFamily("a", "help", labels=["foo"]) |
| 176 |
a.add_metric(["bar"], 1) |
| 177 |
a.add_metric(["baz"], 1) |
| 178 |
b = CounterMetricFamily("b", "help", value=2) |
| 179 |
c = CounterMetricFamily("c", "help", value=3) |
| 180 |
d = CounterMetricFamily("d", "help", value=4) |
| 181 |
self.assertEqualMetrics([a, b, c, d], list(families)) |
| 182 |
|
| 183 |
def test_multiple_trailing_commas(self): |
| 184 |
text = """# TYPE a counter |
| 185 |
# HELP a help |
| 186 |
a{foo="bar",, } 1 |
| 187 |
""" |
| 188 |
self.assertRaises(ValueError, |
| 189 |
lambda: list(text_string_to_metric_families(text))) |
| 190 |
|
| 191 |
def test_empty_brackets(self): |
| 192 |
families = text_string_to_metric_families("""# TYPE a counter |
| 193 |
# HELP a help |
| 194 |
a{} 1 |
| 195 |
""") |
| 196 |
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families)) |
| 197 |
|
| 198 |
def test_nan(self): |
| 199 |
families = text_string_to_metric_families("""a NaN |
| 200 |
""") |
| 201 |
|
| 202 |
self.assertTrue(math.isnan(list(families)[0].samples[0][2])) |
| 203 |
|
| 204 |
def test_empty_label(self): |
| 205 |
families = text_string_to_metric_families("""# TYPE a counter |
| 206 |
# HELP a help |
| 207 |
a{foo="bar"} 1 |
| 208 |
a{foo=""} 2 |
| 209 |
""") |
| 210 |
metric_family = CounterMetricFamily("a", "help", labels=["foo"]) |
| 211 |
metric_family.add_metric(["bar"], 1) |
| 212 |
metric_family.add_metric([""], 2) |
| 213 |
self.assertEqualMetrics([metric_family], list(families)) |
| 214 |
|
| 215 |
def test_label_escaping(self): |
| 216 |
for escaped_val, unescaped_val in [ |
| 217 |
('foo', 'foo'), |
| 218 |
('\\foo', '\\foo'), |
| 219 |
('\\\\foo', '\\foo'), |
| 220 |
('foo\\\\', 'foo\\'), |
| 221 |
('\\\\', '\\'), |
| 222 |
('\\n', '\n'), |
| 223 |
('\\\\n', '\\n'), |
| 224 |
('\\\\\\n', '\\\n'), |
| 225 |
('\\"', '"'), |
| 226 |
('\\\\\\"', '\\"')]: |
| 227 |
families = list(text_string_to_metric_families(""" |
| 228 |
# TYPE a counter |
| 229 |
# HELP a help |
| 230 |
a{foo="%s",bar="baz"} 1 |
| 231 |
""" % escaped_val)) |
| 232 |
metric_family = CounterMetricFamily( |
| 233 |
"a", "help", labels=["foo", "bar"]) |
| 234 |
metric_family.add_metric([unescaped_val, "baz"], 1) |
| 235 |
self.assertEqualMetrics([metric_family], list(families)) |
| 236 |
|
| 237 |
def test_help_escaping(self): |
| 238 |
for escaped_val, unescaped_val in [ |
| 239 |
('foo', 'foo'), |
| 240 |
('\\foo', '\\foo'), |
| 241 |
('\\\\foo', '\\foo'), |
| 242 |
('foo\\', 'foo\\'), |
| 243 |
('foo\\\\', 'foo\\'), |
| 244 |
('\\n', '\n'), |
| 245 |
('\\\\n', '\\n'), |
| 246 |
('\\\\\\n', '\\\n'), |
| 247 |
('\\"', '\\"'), |
| 248 |
('\\\\"', '\\"'), |
| 249 |
('\\\\\\"', '\\\\"')]: |
| 250 |
families = list(text_string_to_metric_families(""" |
| 251 |
# TYPE a counter |
| 252 |
# HELP a %s |
| 253 |
a{foo="bar"} 1 |
| 254 |
""" % escaped_val)) |
| 255 |
metric_family = CounterMetricFamily("a", unescaped_val, labels=["foo"]) |
| 256 |
metric_family.add_metric(["bar"], 1) |
| 257 |
self.assertEqualMetrics([metric_family], list(families)) |
| 258 |
|
| 259 |
def test_escaping(self): |
| 260 |
families = text_string_to_metric_families("""# TYPE a counter |
| 261 |
# HELP a he\\n\\\\l\\tp |
| 262 |
a{foo="b\\"a\\nr"} 1 |
| 263 |
a{foo="b\\\\a\\z"} 2 |
| 264 |
""") |
| 265 |
metric_family = CounterMetricFamily("a", "he\n\\l\\tp", labels=["foo"]) |
| 266 |
metric_family.add_metric(["b\"a\nr"], 1) |
| 267 |
metric_family.add_metric(["b\\a\\z"], 2) |
| 268 |
self.assertEqualMetrics([metric_family], list(families)) |
| 269 |
|
| 270 |
def test_timestamps(self): |
| 271 |
families = text_string_to_metric_families("""# TYPE a counter |
| 272 |
# HELP a help |
| 273 |
a{foo="bar"} 1\t000 |
| 274 |
# TYPE b counter |
| 275 |
# HELP b help |
| 276 |
b 2 1234567890 |
| 277 |
b 88 1234566000 |
| 278 |
""") |
| 279 |
a = CounterMetricFamily("a", "help", labels=["foo"]) |
| 280 |
a.add_metric(["bar"], 1, timestamp=0) |
| 281 |
b = CounterMetricFamily("b", "help") |
| 282 |
b.add_metric([], 2, timestamp=1234567.89) |
| 283 |
b.add_metric([], 88, timestamp=1234566) |
| 284 |
self.assertEqualMetrics([a, b], list(families)) |
| 285 |
|
| 286 |
def test_roundtrip(self): |
| 287 |
text = """# HELP go_gc_duration_seconds A summary of the GC invocation durations. |
| 288 |
# TYPE go_gc_duration_seconds summary |
| 289 |
go_gc_duration_seconds{quantile="0"} 0.013300656000000001 |
| 290 |
go_gc_duration_seconds{quantile="0.25"} 0.013638736 |
| 291 |
go_gc_duration_seconds{quantile="0.5"} 0.013759906 |
| 292 |
go_gc_duration_seconds{quantile="0.75"} 0.013962066 |
| 293 |
go_gc_duration_seconds{quantile="1"} 0.021383540000000003 |
| 294 |
go_gc_duration_seconds_sum 56.12904785 |
| 295 |
go_gc_duration_seconds_count 7476.0 |
| 296 |
# HELP go_goroutines Number of goroutines that currently exist. |
| 297 |
# TYPE go_goroutines gauge |
| 298 |
go_goroutines 166.0 |
| 299 |
# HELP prometheus_local_storage_indexing_batch_duration_milliseconds Quantiles for batch indexing duration in milliseconds. |
| 300 |
# TYPE prometheus_local_storage_indexing_batch_duration_milliseconds summary |
| 301 |
prometheus_local_storage_indexing_batch_duration_milliseconds{quantile="0.5"} NaN |
| 302 |
prometheus_local_storage_indexing_batch_duration_milliseconds{quantile="0.9"} NaN |
| 303 |
prometheus_local_storage_indexing_batch_duration_milliseconds{quantile="0.99"} NaN |
| 304 |
prometheus_local_storage_indexing_batch_duration_milliseconds_sum 871.5665949999999 |
| 305 |
prometheus_local_storage_indexing_batch_duration_milliseconds_count 229.0 |
| 306 |
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds. |
| 307 |
# TYPE process_cpu_seconds_total counter |
| 308 |
process_cpu_seconds_total 29323.4 |
| 309 |
# HELP process_virtual_memory_bytes Virtual memory size in bytes. |
| 310 |
# TYPE process_virtual_memory_bytes gauge |
| 311 |
process_virtual_memory_bytes 2.478268416e+09 |
| 312 |
# HELP prometheus_build_info A metric with a constant '1' value labeled by version, revision, and branch from which Prometheus was built. |
| 313 |
# TYPE prometheus_build_info gauge |
| 314 |
prometheus_build_info{branch="HEAD",revision="ef176e5",version="0.16.0rc1"} 1.0 |
| 315 |
# HELP prometheus_local_storage_chunk_ops_total The total number of chunk operations by their type. |
| 316 |
# TYPE prometheus_local_storage_chunk_ops_total counter |
| 317 |
prometheus_local_storage_chunk_ops_total{type="clone"} 28.0 |
| 318 |
prometheus_local_storage_chunk_ops_total{type="create"} 997844.0 |
| 319 |
prometheus_local_storage_chunk_ops_total{type="drop"} 1.345758e+06 |
| 320 |
prometheus_local_storage_chunk_ops_total{type="load"} 1641.0 |
| 321 |
prometheus_local_storage_chunk_ops_total{type="persist"} 981408.0 |
| 322 |
prometheus_local_storage_chunk_ops_total{type="pin"} 32662.0 |
| 323 |
prometheus_local_storage_chunk_ops_total{type="transcode"} 980180.0 |
| 324 |
prometheus_local_storage_chunk_ops_total{type="unpin"} 32662.0 |
| 325 |
""" |
| 326 |
families = list(text_string_to_metric_families(text)) |
| 327 |
|
| 328 |
class TextCollector: |
| 329 |
def collect(self): |
| 330 |
return families |
| 331 |
|
| 332 |
registry = CollectorRegistry() |
| 333 |
registry.register(TextCollector()) |
| 334 |
self.assertEqual(text.encode('utf-8'), generate_latest(registry)) |
| 335 |
|
| 336 |
|
| 337 |
if __name__ == '__main__': |
| 338 |
unittest.main() |