Skip to content

Commit

Permalink
Allow enabling/disabling _created metrics from code
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
  • Loading branch information
csmarchbanks committed Oct 26, 2023
1 parent 249490e commit 42057fe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion prometheus_client/__init__.py
Expand Up @@ -11,7 +11,7 @@
write_to_textfile,
)
from .gc_collector import GC_COLLECTOR, GCCollector
from .metrics import Counter, Enum, Gauge, Histogram, Info, Summary
from .metrics import Counter, Enum, Gauge, Histogram, Info, Summary, enable_created_metrics, disable_created_metrics
from .metrics_core import Metric
from .platform_collector import PLATFORM_COLLECTOR, PlatformCollector
from .process_collector import PROCESS_COLLECTOR, ProcessCollector
Expand All @@ -27,6 +27,8 @@
'Histogram',
'Info',
'Enum',
'enable_created_metrics',
'disable_created_metrics',
'CONTENT_TYPE_LATEST',
'generate_latest',
'MetricsHandler',
Expand Down
12 changes: 12 additions & 0 deletions prometheus_client/metrics.py
Expand Up @@ -70,6 +70,18 @@ def _get_use_created() -> bool:
_use_created = _get_use_created()


def disable_created_metrics():
"""Disable exporting _created metrics on counters, histograms, and summaries."""
global _use_created
_use_created = False


def enable_created_metrics():
"""Enable exporting _created metrics on counters, histograms, and summaries."""
global _use_created
_use_created = True


class MetricWrapperBase(Collector):
_type: Optional[str] = None
_reserved_labelnames: Sequence[str] = ()
Expand Down
2 changes: 1 addition & 1 deletion prometheus_client/registry.py
Expand Up @@ -94,7 +94,7 @@ def collect(self) -> Iterable[Metric]:
if ti:
yield ti
for collector in collectors:
yield from collector.collect()
yield from collector.collect(created=True)

def restricted_registry(self, names: Iterable[str]) -> "RestrictedRegistry":
"""Returns object that only collects some metrics.
Expand Down

0 comments on commit 42057fe

Please sign in to comment.