Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cyclomatic complexity #842

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def sample_line(line):
try:
mname = metric.name
mtype = metric.type

# Munging from OpenMetrics into Prometheus format.
if mtype == 'counter':
mname = mname + '_total'
Expand All @@ -212,7 +213,7 @@ def sample_line(line):
mtype = 'histogram'
elif mtype == 'unknown':
mtype = 'untyped'

output.append('# HELP {} {}\n'.format(
mname, metric.documentation.replace('\\', r'\\').replace('\n', r'\n')))
output.append(f'# TYPE {mname} {mtype}\n')
Expand Down
21 changes: 14 additions & 7 deletions prometheus_client/metrics_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ def __init__(self,
if name.endswith('_total'):
name = name[:-6]
Metric.__init__(self, name, documentation, 'counter', unit)
if labels is not None and value is not None:
raise ValueError('Can only specify at most one of value and labels.')

if labels is None:
labels = []
elif value is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the old logic to be clearer. It was clearly a check at the very beginning to make sure the input was valid, then we assigned a default value. Now we are doing both at the same time which IMO is harder to understand.

raise ValueError('Can only specify at most one of value and labels.')

self._labelnames = tuple(labels)
if value is not None:
self.add_metric([], value, created)
Expand Down Expand Up @@ -196,10 +198,12 @@ def __init__(self,
Metric.__init__(self, name, documentation, 'summary', unit)
if (sum_value is None) != (count_value is None):
raise ValueError('count_value and sum_value must be provided together.')
if labels is not None and count_value is not None:
raise ValueError('Can only specify at most one of value and labels.')
if labels is None:

if (labels is None):
labels = []
elif count_value is not None:
raise ValueError('Can only specify at most one of value and labels.')

self._labelnames = tuple(labels)
# The and clause is necessary only for typing, the above ValueError will raise if only one is set.
if count_value is not None and sum_value is not None:
Expand Down Expand Up @@ -238,12 +242,15 @@ def __init__(self,
unit: str = '',
):
Metric.__init__(self, name, documentation, 'histogram', unit)

if sum_value is not None and buckets is None:
raise ValueError('sum value cannot be provided without buckets.')
if labels is not None and buckets is not None:
raise ValueError('Can only specify at most one of buckets and labels.')

if labels is None:
labels = []
elif buckets is not None:
raise ValueError('Can only specify at most one of buckets and labels.')

self._labelnames = tuple(labels)
if buckets is not None:
self.add_metric([], buckets, sum_value)
Expand Down
5 changes: 2 additions & 3 deletions prometheus_client/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ def _get_names(self, collector):
except AttributeError:
pass
# Otherwise, if auto describe is enabled use the collect function.
if not desc_func and self._auto_describe:
desc_func = collector.collect

if not desc_func:
if self._auto_describe:
desc_func = collector.collect
return []

result = []
Expand Down