8000 more docstrings · prometheus/client_python@303ff91 · GitHub
[go: up one dir, main page]

Skip to content

Commit 303ff91

Browse files
committed
more docstrings
1 parent 72fdf77 commit 303ff91

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

prometheus_client/openmetrics/exposition.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,23 @@ def generate_latest(registry):
9393

9494

9595
def escape_metric_name(s: str) -> str:
96+
"""Escapes the metric name and puts it in quotes iff the name does not
97+
conform to the legacy Prometheus character set.
98+
"""
9699
if _is_valid_legacy_metric_name(s):
97100
return s
98101
return '"{}"'.format(_escape(s))
99102

100103

101104
def escape_label_name(s: str) -> str:
105+
"""Escapes the label name and puts it in quotes iff the name does not
106+
conform to the legacy Prometheus character set.
107+
"""
102108
if _is_valid_legacy_labelname(s):
103109
return s
104110
return '"{}"'.format(_escape(s))
105111

106112

107113
def _escape(s: str) -> str:
114+
"""Performs backslash escaping on \, \n, and " characters."""
108115
return s.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"')

prometheus_client/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def _split_quoted(text, separator, maxsplit=0):
199199

200200

201201
def _unquote_unescape(text):
202-
"""Returns the string, and true if it was quoted"""
202+
"""Returns the string, and true if it was quoted."""
203203
if not text:
204204
return text, False
205205
quoted = False

prometheus_client/validation.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def _init_legacy_validation() -> bool:
1515

1616

1717
def get_legacy_validation() -> bool:
18+
"""Return the current status of the legacy validation setting."""
1819
global _legacy_validation
1920
return _legacy_validation
2021

@@ -32,6 +33,10 @@ def enable_legacy_validation():
3233

3334

3435
def _validate_metric_name(name: str) -> None:
36+
"""Raises ValueError if the provided name is not a valid metric name.
37+
38+
This check uses the global legacy validation setting to determine the validation scheme.
39+
"""
3540
if not name:
3641
raise ValueError("metric name cannot be empty")
3742
global _legacy_validation
@@ -45,11 +50,15 @@ def _validate_metric_name(name: str) -> None:
4550

4651

4752
def _is_valid_legacy_metric_name(name: str) -> bool:
53+
"""Returns true if the provided metric name conforms to the legacy validation scheme."""
4854
return METRIC_NAME_RE.match(name) is not None
4955

5056

5157
def _validate_metric_label_name_token(tok: str) -> None:
52-
"""Check validity of a parsed label name token. UTF-8 names must be quoted."""
58+
"""Raises ValueError if a parsed label name token is invalid.
59+
60+
UTF-8 names must be quoted.
61+
"""
5362
if not tok:
5463
raise ValueError("invalid label name token " + tok)
5564
global _legacy_validation
@@ -65,6 +74,10 @@ def _validate_metric_label_name_token(tok: str) -> None:
6574

6675

6776
def _validate_labelname(l):
77+
"""Raises ValueError if the provided name is not a valid label name.
78+
79+
This check uses the global legacy validation setting to determine the validation scheme.
80+
"""
6881
if get_legacy_validation():
6982
if not METRIC_LABEL_NAME_RE.match(l):
7083
raise ValueError('Invalid label metric name: ' + l)
@@ -80,12 +93,17 @@ def _validate_labelname(l):
8093

8194

8295
def _is_valid_legacy_labelname(l: str) -> bool:
96+
"""Returns true if the provided label name conforms to the legacy validation scheme."""
8397
if METRIC_LABEL_NAME_RE.match(l) is None:
8498
return False
8599
return RESERVED_METRIC_LABEL_NAME_RE.match(l) is None
86100

87101

88102
def _validate_labelnames(cls, labelnames):
103+
"""Raises ValueError if any of the provided names is not a valid label name.
104+
105+
This check uses the global legacy validation setting to determine the validation scheme.
106+
"""
89107
labelnames = tuple(labelnames)
90108
for l in labelnames:
91109
_validate_labelname(l)
@@ -95,6 +113,7 @@ def _validate_labelnames(cls, labelnames):
95113

96114

97115
def _validate_exemplar(exemplar):
116+
"""Raises ValueError if the exemplar is invalid."""
98117
runes = 0
99118
for k, v in exemplar.items():
100119
_validate_labelname(k)

0 commit comments

Comments
 (0)
0