|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import fileinput |
| 4 | +import re |
4 | 5 | import sys |
5 | 6 |
|
| 7 | +# Read Prometheus metrics from the files provided on the command line, |
| 8 | +# or on stdin, and save their types and descriptions into `metrics`. |
6 | 9 | metrics = {} |
7 | | -for line in fileinput.input(encoding='utf-8'): |
| 10 | +for line in fileinput.input(encoding="utf-8"): |
8 | 11 | comment, keyword, name, args = line.strip().split(maxsplit=3) |
9 | | - if comment == '#' and keyword in ['TYPE', 'HELP']: |
| 12 | + if comment == "#" and keyword in ["TYPE", "HELP"]: |
10 | 13 | metrics.setdefault(name, {})[keyword] = args |
11 | 14 |
|
12 | | -print("""<!-- This file is automatically generated. Do not edit! |
13 | | -
|
14 | | -To regenerate this file, start a Feldera pipeline (any one will do) |
15 | | -and obtain Prometheus metrics for it with, for example, `fda metrics |
16 | | ---format=prometheus` or `curl https://server/v0/metrics`, then run |
17 | | -`metrics.py` from the same directory as this, like so: |
18 | | -
|
19 | | -metrics.py < metrics.txt > metrics.md |
20 | | ---> |
21 | | -
|
22 | | -# Metrics |
23 | | -
|
24 | | -This reference lists all of the metrics that Feldera exports through |
25 | | -its `/metrics` endpoint in [Prometheus exposition format]. It is |
26 | | -automatically generated using the documentation embedded in Prometheus |
27 | | -output. |
28 | | -
|
29 | | -All of the metrics exported by a particular Feldera pipeline are |
30 | | -labeled with the pipeline's UUID as `pipeline`. Some metrics have |
31 | | -additional labels, as documented below. |
32 | | -
|
33 | | -[Prometheus exposition format]: https://prometheus.io/docs/instrumenting/exposition_formats |
34 | | -
|
| 15 | +# Read Markdown template from metrics.md.in, making some substitutions: |
| 16 | +# |
| 17 | +# - Delete lines up to the first `#`, and copy that line to the output. |
| 18 | +# |
| 19 | +# - Add a warning about the file being automatically generated, so |
| 20 | +# that people don't edit it. (This has to go *after* the first `#` |
| 21 | +# because docusaurus won't skip the comment when it goes looking |
| 22 | +# for the page title.) |
| 23 | +# |
| 24 | +# - Copy the rest of the file to the output, substituting lines that |
| 25 | +# are bracketed by {{}} by autogenerated metrics documentation. |
| 26 | +template = open("metrics.md.in", "r") |
| 27 | +output = open("metrics.md", "w") |
| 28 | +for line in template: |
| 29 | + line = line.rstrip() |
| 30 | + if line.startswith("#"): |
| 31 | + break |
| 32 | +output.write(f"""{line} |
| 33 | +
|
| 34 | +<!-- This file is automatically generated. Do not edit! |
| 35 | +
|
| 36 | +To update the documentation, please edit metrics.md.in instead and |
| 37 | +then regenerate this file using the instructions in that file. --> |
35 | 38 | """) |
36 | | - |
37 | | -def document_section(name, heading): |
38 | | - print(heading.strip()) |
39 | | - print() |
40 | | - |
41 | | - section_metrics = sorted([key for key in metrics.keys() if key.startswith(f"{name}_")]) |
42 | | - assert section_metrics != [] |
43 | | - print("| Name | Type | Description |") |
44 | | - print("| :--- | :--- | :---------- |") |
45 | | - for metric in section_metrics: |
46 | | - type_ = metrics[metric]['TYPE'] |
47 | | - help = metrics[metric]['HELP'] |
48 | | - print(f"| `{metric}` |{type_} | {help} |") |
49 | | - del metrics[metric] |
50 | | - print() |
51 | | - |
52 | | -document_section("process", """## Process Metrics |
53 | | -
|
54 | | -These metrics report statistics for a running Feldera pipeline |
55 | | -process. When a pipeline process is killed and restarts from a |
56 | | -checkpoint, the new process's metrics are for it alone, not cumulative |
57 | | -with any previous instantiations. |
58 | | -
|
59 | | -These metrics are intended to match the standard [Prometheus |
60 | | -definitions]. |
61 | | -
|
62 | | -[Prometheus definitions]: https://prometheus.io/docs/instrumenting/writing_clientlibs/#process-metrics""") |
63 | | - |
64 | | -document_section("feldera", """## Feldera metrics |
65 | | -
|
66 | | -These metrics report statistics for Feldera operations.""") |
67 | | - |
68 | | -document_section("dbsp", """## DBSP metrics |
69 | | -
|
70 | | -These metrics report statistics for [DBSP], the low-level mechanism on |
71 | | -which Feldera is built. |
72 | | -
|
73 | | -[DBSP]: https://docs.rs/dbsp/latest/dbsp/""") |
74 | | - |
75 | | -document_section("records", """## Record Processing |
76 | | -
|
77 | | -These metrics report the status of record input, processing, and |
78 | | -output as a whole. They are maintained consistently across checkpoint |
79 | | -and resume. |
80 | | -
|
81 | | -""") |
82 | | - |
83 | | -document_section("storage", """## Storage Performance |
84 | | -
|
85 | | -These metrics report the performance of storage, which allows Feldera |
86 | | -to work with data larger than memory.""") |
87 | | - |
88 | | -document_section("pipeline", """## Pipeline Status |
89 | | -
|
90 | | -These metrics report the status of the pipeline. |
91 | | -
|
92 | | -""") |
93 | | - |
94 | | -document_section("input_connector", """## Input Connectors |
95 | | -
|
96 | | -These metrics are per-input connector, labeled with `endpoint` set to |
97 | | -the name of the input connector, which is either the name assigned in |
98 | | -the SQL program or automatically generated as `unnamed-<number>`, |
99 | | -where `<number>` is 1 for the first connector for a given table, 2 for |
100 | | -the second, and so on.""") |
101 | | - |
102 | | -document_section("output_connector", """## Output Connectors |
103 | | -
|
104 | | -These metrics are per-output connector, labeled with `endpoint` set to |
105 | | -the name of the output connector, which is either the name assigned in |
106 | | -the SQL program or automatically generated as `unnamed-<number>`, |
107 | | -where `<number>` is 1 for the first connector for a given view, |
108 | | -2 for the second, and so on.""") |
109 | | - |
110 | | -document_section("compaction", """## Merge Status |
111 | | -
|
112 | | -These metrics reports the status of the merger. |
113 | | -""") |
114 | | - |
115 | | -document_section("output_buffered", """## Output Batches |
116 | | -
|
117 | | -These metrics report output buffering status.""") |
118 | | - |
119 | | -document_section("files", """## File metrics |
120 | | -
|
121 | | -These report use of files within Feldera storage.""") |
| 39 | +for line in template: |
| 40 | + line = line.rstrip() |
| 41 | + m = re.match(r"{{(.*)}}", line) |
| 42 | + if m: |
| 43 | + regex = re.compile(m.group(1)) |
| 44 | + matching_metrics = sorted([key for key in metrics.keys() if regex.match(key)]) |
| 45 | + assert matching_metrics != [] |
| 46 | + output.write("| Name | Type | Description |\n") |
| 47 | + output.write("| :--- | :--- | :---------- |\n") |
| 48 | + for metric in matching_metrics: |
| 49 | + type_ = metrics[metric]["TYPE"] |
| 50 | + help = metrics[metric]["HELP"] |
| 51 | + output.write(f"| `{metric}` |{type_} | {help} |\n") |
| 52 | + del metrics[metric] |
| 53 | + else: |
| 54 | + output.write(f"{line}\n") |
122 | 55 |
|
123 | 56 | if len(metrics) > 0: |
124 | | - sys.stderr.write(f"error: the following metrics need documentation sections: {metrics.keys()}\n") |
| 57 | + sys.stderr.write( |
| 58 | + f"error: the following metrics need to be included in documentation: {metrics.keys()}\n" |
| 59 | + ) |
125 | 60 | sys.exit(1) |
0 commit comments