diff --git a/prometheus_client/bridge/graphite.py b/prometheus_client/bridge/graphite.py index 8cadbedc..b9764e3a 100755 --- a/prometheus_client/bridge/graphite.py +++ b/prometheus_client/bridge/graphite.py @@ -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')) diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index af512115..cf654e29 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -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): diff --git a/prometheus_client/openmetrics/exposition.py b/prometheus_client/openmetrics/exposition.py index 26f3109f..c91c993d 100644 --- a/prometheus_client/openmetrics/exposition.py +++ b/prometheus_client/openmetrics/exposition.py @@ -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") @@ -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}' diff --git a/prometheus_client/parser.py b/prometheus_client/parser.py index dc8e30df..5a3f4c30 100644 --- a/prometheus_client/parser.py +++ b/prometheus_client/parser.py @@ -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