Skip to content

Optimization: Provide code readability #1034

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

Open
wants to merge 1 commit 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
32 changes: 16 additions & 16 deletions prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,30 @@ def __init__(self,
self._timeout = timeout_seconds
self._timer = _timer

def push(self, prefix: str = '') -> None:
def push(self, prefix: str = "") -> None:
now = int(self._timer())
output = []

prefixstr = ''
if prefix:
prefixstr = prefix + '.'
prefixstr = prefix + "." if prefix else ""

for metric in self._registry.collect():
for s in metric.samples:
labelstr = ""
if s.labels:
if self._tags:
sep = ';'
fmt = '{0}={1}'
else:
sep = '.'
fmt = '{0}.{1}'
sep = ";"
fmt = "{0}={1}"
if not self._tags:
sep = "."
fmt = "{0}.{1}"
labelstr = sep + sep.join(
[fmt.format(
_sanitize(k), _sanitize(v))
for k, v in sorted(s.labels.items())])
else:
labelstr = ''
output.append(f'{prefixstr}{_sanitize(s.name)}{labelstr} {float(s.value)} {now}\n')
[
fmt.format(_sanitize(k), _sanitize(v))
for k, v in sorted(s.labels.items())
]
)
output.append(
f"{prefixstr}{_sanitize(s.name)}{labelstr} {float(s.value)} {now}\n"
)

conn = socket.create_connection(self._address, self._timeout)
conn.sendall(''.join(output).encode('ascii'))
Expand Down
7 changes: 1 addition & 6 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@


def _build_full_name(metric_type, name, namespace, subsystem, unit):
full_name = ''
if namespace:
full_name += namespace + '_'
if subsystem:
full_name += subsystem + '_'
full_name += name
full_name = "_".join(filter(None, [namespace, subsystem, name]))
if metric_type == 'counter' and full_name.endswith('_total'):
full_name = full_name[:-6] # Munge to OpenMetrics.
if unit and not full_name.endswith("_" + unit):
Expand Down
6 changes: 2 additions & 4 deletions prometheus_client/openmetrics/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ def generate_latest(registry):
if metric.unit:
output.append(f'# UNIT {mname} {metric.unit}\n')
for s in metric.samples:
labelstr = ''
if s.labels:
labelstr = '{{{0}}}'.format(','.join(
['{}="{}"'.format(
k, v.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"'))
for k, v in sorted(s.labels.items())]))
else:
labelstr = ''
exemplarstr = ''
if s.exemplar:
if not _is_valid_exemplar_metric(metric, s):
raise ValueError(f"Metric {metric.name} has exemplars, but is not a histogram bucket or counter")
Expand All @@ -52,8 +52,6 @@ def generate_latest(registry):
labels,
floatToGoString(s.exemplar.value),
)
else:
exemplarstr = ''
timestamp = ''
if s.timestamp is not None:
timestamp = f' {s.timestamp}'
Expand Down
6 changes: 2 additions & 4 deletions prometheus_client/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ def build_metric(name: str, documentation: str, typ: str, samples: List[Sample])
if name.endswith('_total'):
name = name[:-6]
else:
new_samples = []
for s in samples:
new_samples.append(Sample(s[0] + '_total', *s[1:]))
samples = new_samples
new_samples = [Sample(s[0] + '_total', *s[1:]) for s in samples]
samples = new_samples
metric = Metric(name, documentation, typ)
metric.samples = samples
return metric
Expand Down