Skip to content

Commit

Permalink
Merge pull request #774 from prometheus/disable-created-series
Browse files Browse the repository at this point in the history
Disable exporting of _created metrics via env var
  • Loading branch information
csmarchbanks committed Apr 5, 2022
2 parents 789b24a + a0cf10b commit 1aee355
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ h = Histogram('request_latency_seconds', 'Description of histogram')
h.observe(4.7, {'trace_id': 'abc123'})
```

### Disabling `_created` metrics

By default counters, histograms, and summaries export an additional series
suffixed with `_created` and a value of the unix timestamp for when the metric
was created. If this information is not helpful, it can be disabled by setting
the environment variable `PROMETHEUS_DISABLE_CREATED_SERIES=True`.

### Process Collector

The Python client automatically exports metrics about process CPU usage, RAM,
Expand Down
30 changes: 22 additions & 8 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from threading import Lock
import time
import types
Expand Down Expand Up @@ -62,6 +63,13 @@ def _validate_exemplar(exemplar):
raise ValueError('Exemplar labels have %d UTF-8 characters, exceeding the limit of 128')


def _get_use_created() -> bool:
return os.environ.get("PROMETHEUS_DISABLE_CREATED_SERIES", 'False').lower() not in ('true', '1', 't')


_use_created = _get_use_created()


class MetricWrapperBase(Collector):
_type: Optional[str] = None
_reserved_labelnames: Sequence[str] = ()
Expand Down Expand Up @@ -291,10 +299,13 @@ def count_exceptions(self, exception: Type[BaseException] = Exception) -> Except
return ExceptionCounter(self, exception)

def _child_samples(self) -> Iterable[Sample]:
return (
Sample('_total', {}, self._value.get(), None, self._value.get_exemplar()),
Sample('_created', {}, self._created, None, None),
)
sample = Sample('_total', {}, self._value.get(), None, self._value.get_exemplar())
if _use_created:
return (
sample,
Sample('_created', {}, self._created, None, None)
)
return (sample,)


class Gauge(MetricWrapperBase):
Expand Down Expand Up @@ -484,11 +495,13 @@ def time(self) -> Timer:
return Timer(self, 'observe')

def _child_samples(self) -> Iterable[Sample]:
return (
samples = [
Sample('_count', {}, self._count.get(), None, None),
Sample('_sum', {}, self._sum.get(), None, None),
Sample('_created', {}, self._created, None, None),
)
]
if _use_created:
samples.append(Sample('_created', {}, self._created, None, None))
return tuple(samples)


class Histogram(MetricWrapperBase):
Expand Down Expand Up @@ -616,7 +629,8 @@ def _child_samples(self) -> Iterable[Sample]:
samples.append(Sample('_count', {}, acc, None, None))
if self._upper_bounds[0] >= 0:
samples.append(Sample('_sum', {}, self._sum.get(), None, None))
samples.append(Sample('_created', {}, self._created, None, None))
if _use_created:
samples.append(Sample('_created', {}, self._created, None, None))
return tuple(samples)


Expand Down
29 changes: 29 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from concurrent.futures import ThreadPoolExecutor
import os
import time
import unittest

import pytest

from prometheus_client import metrics
from prometheus_client.core import (
CollectorRegistry, Counter, CounterMetricFamily, Enum, Gauge,
GaugeHistogramMetricFamily, GaugeMetricFamily, Histogram,
HistogramMetricFamily, Info, InfoMetricFamily, Metric, Sample,
StateSetMetricFamily, Summary, SummaryMetricFamily, UntypedMetricFamily,
)
from prometheus_client.decorator import getargspec
from prometheus_client.metrics import _get_use_created


def assert_not_observable(fn, *args, **kwargs):
Expand Down Expand Up @@ -115,6 +118,32 @@ def test_exemplar_too_long(self):
})


class TestDisableCreated(unittest.TestCase):
def setUp(self):
self.registry = CollectorRegistry()
os.environ['PROMETHEUS_DISABLE_CREATED_SERIES'] = 'True'
metrics._use_created = _get_use_created()

def tearDown(self):
os.environ.pop('PROMETHEUS_DISABLE_CREATED_SERIES', None)
metrics._use_created = _get_use_created()

def test_counter(self):
counter = Counter('c_total', 'help', registry=self.registry)
counter.inc()
self.assertEqual(None, self.registry.get_sample_value('c_created'))

def test_histogram(self):
histogram = Histogram('h', 'help', registry=self.registry)
histogram.observe(3.2)
self.assertEqual(None, self.registry.get_sample_value('h_created'))

def test_summary(self):
summary = Summary('s', 'help', registry=self.registry)
summary.observe(8.2)
self.assertEqual(None, self.registry.get_sample_value('s_created'))


class TestGauge(unittest.TestCase):
def setUp(self):
self.registry = CollectorRegistry()
Expand Down

0 comments on commit 1aee355

Please sign in to comment.