promtext-cli 0.1.2.dev84__py3-none-any.whl → 0.1.2.dev93__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- promtext_cli/main.py +48 -24
- {promtext_cli-0.1.2.dev84.dist-info → promtext_cli-0.1.2.dev93.dist-info}/METADATA +1 -1
- promtext_cli-0.1.2.dev93.dist-info/RECORD +6 -0
- promtext_cli-0.1.2.dev84.dist-info/RECORD +0 -6
- {promtext_cli-0.1.2.dev84.dist-info → promtext_cli-0.1.2.dev93.dist-info}/WHEEL +0 -0
- {promtext_cli-0.1.2.dev84.dist-info → promtext_cli-0.1.2.dev93.dist-info}/entry_points.txt +0 -0
promtext_cli/main.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"""promtext_cli is providing a CLI to cleanly update prometheus textfiles from scripts"""
|
|
2
2
|
|
|
3
|
+
# pylint: disable=C0116,R0914,R0912,R0915
|
|
4
|
+
# this rules will be fixed by a object-oriented refactoring
|
|
5
|
+
|
|
3
6
|
import argparse
|
|
4
7
|
from pathlib import Path
|
|
5
8
|
import logging
|
|
@@ -8,37 +11,43 @@ import sys
|
|
|
8
11
|
from prometheus_client.parser import text_string_to_metric_families
|
|
9
12
|
from prometheus_client import CollectorRegistry, Gauge, write_to_textfile
|
|
10
13
|
|
|
14
|
+
|
|
11
15
|
def promtext():
|
|
12
16
|
# setup argpars
|
|
13
17
|
# required file first
|
|
14
|
-
parser = argparse.ArgumentParser(description=
|
|
18
|
+
parser = argparse.ArgumentParser(description="Prometheus textfile helper")
|
|
15
19
|
parser.add_argument(
|
|
16
|
-
|
|
20
|
+
"filename",
|
|
17
21
|
type=str,
|
|
18
|
-
help=
|
|
19
|
-
|
|
22
|
+
help="Path to existing or new prometheus textfile, will be updated",
|
|
23
|
+
)
|
|
20
24
|
|
|
21
25
|
# metric name, required
|
|
22
|
-
parser.add_argument(
|
|
23
|
-
'metric',
|
|
24
|
-
type=str,
|
|
25
|
-
help='metric name (new or updated)')
|
|
26
|
+
parser.add_argument("metric", type=str, help="metric name (new or updated)")
|
|
26
27
|
|
|
27
28
|
# metric value as int/float, required
|
|
28
|
-
parser.add_argument(
|
|
29
|
+
parser.add_argument("value", type=float, help="metric value")
|
|
29
30
|
|
|
30
31
|
# metric documentation as optional argument
|
|
31
32
|
parser.add_argument(
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
"--docs",
|
|
34
|
+
type=str,
|
|
35
|
+
help="metric documentation",
|
|
36
|
+
default="metric appended by promtext-cli",
|
|
37
|
+
)
|
|
34
38
|
|
|
35
39
|
# labels, key-value, minimum 0, repeatable
|
|
36
|
-
parser.add_argument(
|
|
40
|
+
parser.add_argument(
|
|
41
|
+
"--label", metavar="KEY=VALUE", help="label key=value pairs", action="append"
|
|
42
|
+
)
|
|
37
43
|
|
|
38
44
|
# log level from argparse
|
|
39
45
|
parser.add_argument(
|
|
40
|
-
|
|
41
|
-
|
|
46
|
+
"-v",
|
|
47
|
+
"--verbose",
|
|
48
|
+
action="store_const",
|
|
49
|
+
dest="loglevel",
|
|
50
|
+
const=logging.INFO,
|
|
42
51
|
)
|
|
43
52
|
args = parser.parse_args()
|
|
44
53
|
logging.basicConfig(level=args.loglevel)
|
|
@@ -52,7 +61,7 @@ def promtext():
|
|
|
52
61
|
|
|
53
62
|
# check if args.filename exists with pathlib
|
|
54
63
|
if textfile.is_file():
|
|
55
|
-
for f in text_string_to_metric_families(textfile.read_text()):
|
|
64
|
+
for f in text_string_to_metric_families(textfile.read_text(encoding="utf-8")):
|
|
56
65
|
# per metric: iterate over samples, create metric in registry
|
|
57
66
|
m = False
|
|
58
67
|
samples = []
|
|
@@ -63,8 +72,13 @@ def promtext():
|
|
|
63
72
|
labelnames = list(samples[0].labels.keys())
|
|
64
73
|
# metric-type specific init
|
|
65
74
|
if f.type == "gauge":
|
|
66
|
-
m = Gauge(
|
|
67
|
-
|
|
75
|
+
m = Gauge(
|
|
76
|
+
f.name,
|
|
77
|
+
f.documentation,
|
|
78
|
+
unit=f.unit,
|
|
79
|
+
labelnames=labelnames,
|
|
80
|
+
registry=registry,
|
|
81
|
+
)
|
|
68
82
|
else:
|
|
69
83
|
# we don't support other types yet, continue in these cases
|
|
70
84
|
logger.warning("unsupported metric type %s, dropping", f.type)
|
|
@@ -76,8 +90,11 @@ def promtext():
|
|
|
76
90
|
else:
|
|
77
91
|
m.set(s.value)
|
|
78
92
|
metrics[f.name] = m
|
|
79
|
-
logger.info(
|
|
80
|
-
|
|
93
|
+
logger.info(
|
|
94
|
+
"copy gauge metric %s with labels %s from old file",
|
|
95
|
+
f.name,
|
|
96
|
+
", ".join(labelnames),
|
|
97
|
+
)
|
|
81
98
|
else:
|
|
82
99
|
logger.warning("got empty metric %s from old file, dropping", f.name)
|
|
83
100
|
|
|
@@ -99,18 +116,24 @@ def promtext():
|
|
|
99
116
|
labelvalues = labels.values()
|
|
100
117
|
else:
|
|
101
118
|
m = metrics[args.metric]
|
|
102
|
-
|
|
119
|
+
|
|
120
|
+
# There is no way to access existing labelnames directly
|
|
121
|
+
# pylint: disable=W0212
|
|
103
122
|
old_labelnames = list(m._labelnames)
|
|
104
123
|
for la in old_labelnames:
|
|
105
124
|
logger.info("processing label %s", la)
|
|
106
|
-
if la in labels:
|
|
125
|
+
if la in labels: # labelvalues are needed in order!
|
|
107
126
|
labelvalues.append(labels[la])
|
|
108
127
|
else:
|
|
109
128
|
logger.error("previously known label '%s' missing, cannot update!", la)
|
|
110
129
|
sys.exit(1)
|
|
111
130
|
if len(old_labelnames) != len(labels.keys()):
|
|
112
|
-
logger.error(
|
|
113
|
-
|
|
131
|
+
logger.error(
|
|
132
|
+
"labelnames for metric %s not the same, cannot update! Old: %s, New: %s",
|
|
133
|
+
args.metric,
|
|
134
|
+
old_labelnames,
|
|
135
|
+
list(labels.keys()),
|
|
136
|
+
)
|
|
114
137
|
sys.exit(1)
|
|
115
138
|
logger.info("updating metric %s", args.metric)
|
|
116
139
|
|
|
@@ -124,5 +147,6 @@ def promtext():
|
|
|
124
147
|
write_to_textfile(args.filename, registry)
|
|
125
148
|
logger.info("wrote to %s", args.filename)
|
|
126
149
|
|
|
127
|
-
|
|
150
|
+
|
|
151
|
+
if __name__ == "__main__":
|
|
128
152
|
promtext()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: promtext-cli
|
|
3
|
-
Version: 0.1.2.
|
|
3
|
+
Version: 0.1.2.dev93
|
|
4
4
|
Summary: Prometheus Textfile Tooling
|
|
5
5
|
Project-URL: Documentation, https://codeberg.org/margau/promtext-cli/src/branch/main#readme
|
|
6
6
|
Project-URL: Issues, https://codeberg.org/margau/promtext-cli/issues
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
promtext_cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
promtext_cli/main.py,sha256=Ce6GRsfi1IkHyatbENEZ8kTsoWnaoow_gRlwZRKMNYg,5054
|
|
3
|
+
promtext_cli-0.1.2.dev93.dist-info/METADATA,sha256=K7iBHxvOiTCQxGDpyU0b9_DF4onne4ZCaQ9Mx6XIzgQ,3414
|
|
4
|
+
promtext_cli-0.1.2.dev93.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
5
|
+
promtext_cli-0.1.2.dev93.dist-info/entry_points.txt,sha256=mIY1sCmFCjCpCfn3px-BajeXOaKZIA7iR5_fCaUJ4gg,56
|
|
6
|
+
promtext_cli-0.1.2.dev93.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
promtext_cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
promtext_cli/main.py,sha256=405QZsTW_CHHS6-T-0co0gdf74ilscvN09j5PqvMvKE,4573
|
|
3
|
-
promtext_cli-0.1.2.dev84.dist-info/METADATA,sha256=kNy96EsoUekJyxChy6NS7fiqGl-tvnyG1c7ene9slz0,3414
|
|
4
|
-
promtext_cli-0.1.2.dev84.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
5
|
-
promtext_cli-0.1.2.dev84.dist-info/entry_points.txt,sha256=mIY1sCmFCjCpCfn3px-BajeXOaKZIA7iR5_fCaUJ4gg,56
|
|
6
|
-
promtext_cli-0.1.2.dev84.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|