diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 345b247d..c676d51c 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -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' @@ -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') diff --git a/prometheus_client/metrics_core.py b/prometheus_client/metrics_core.py index 77b3e446..f1af4cfa 100644 --- a/prometheus_client/metrics_core.py +++ b/prometheus_client/metrics_core.py @@ -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: + 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) @@ -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: @@ -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) diff --git a/prometheus_client/registry.py b/prometheus_client/registry.py index 694e4bd8..e580f86e 100644 --- a/prometheus_client/registry.py +++ b/prometheus_client/registry.py @@ -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 = []