8000 Disable exporting of _created metrics via env var · barbdowns/client_python@a0cf10b · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit a0cf10b

Browse files
committed
Disable exporting of _created metrics via env var
Users can disable the automatic creation of _created metrics by specifying the environment variable PROMETHEUS_DISABLE_CREATED_SERIES. This is controlled by an environment variable as an end user may not have control over which registry is being used/when metrics are created. Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
1 parent c044b88 commit a0cf10b

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ h = Histogram('request_latency_seconds', 'Description of histogram')
252252
h.observe(4.7, {'trace_id': 'abc123'})
253253
```
254254

255+
### Disabling `_created` metrics
256+
257+
By default counters, histograms, and summaries export an additional series
258+
suffixed with `_created` and a value of the unix timestamp for when the metric
259+
was created. If this information is not helpful, it can be disabled by setting
260+
the environment variable `PROMETHEUS_DISABLE_CREATED_SERIES=True`.
261+
255262
### Process Collector
256263

257264
The Python client automatically exports metrics about process CPU usage, RAM,

prometheus_client/metrics.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from threading import Lock
23
import time
34
import types
@@ -62,6 +63,13 @@ def _validate_exemplar(exemplar):
6263
raise ValueError('Exemplar labels have %d UTF-8 characters, exceeding the limit of 128')
6364

6465

66+
def _get_use_created() -> bool:
67+
return os.environ.get("PROMETHEUS_DISABLE_CREATED_SERIES", 'False').lower() not in ('true', '1', 't')
68+
69+
70+
_use_created = _get_use_created()
71+
72+
6573
class MetricWrapperBase(Collector):
6674
_type: Optional[str] = None
6775
_reserved_labelnames: Sequence[str] = ()
@@ -291,10 +299,13 @@ def count_exceptions(self, exception: Type[BaseException] = Exception) -> Except
291299
return ExceptionCounter(self, exception)
292300

293301
def _child_samples(self) -> Iterable[Sample]:
294-
return (
295-
Sample('_total', {}, self._value.get(), None, self._value.get_exemplar()),
296-
Sample('_created', {}, self._created, None, None),
297-
)
302+
sample = Sample('_total', {}, self._value.get(), None, self._value.get_exemplar())
303+
if _use_created:
304+
return (
305+
sample,
306+
Sample('_created', {}, self._created, None, None)
307+
)
308+
return (sample,)
298309

299310

300311
class Gauge(MetricWrapperBase):
@@ -484,11 +495,13 @@ def time(self) -> Timer:
484495
return Timer(self, 'observe')
485496

486497
def _child_samples(self) -> Iterable[Sample]:
487-
return (
498+
samples = [
488499
Sample('_count', {}, self._count.get(), None, None),
489500
Sample('_sum', {}, self._sum.get(), None, None),
490-
Sample('_created', {}, self._created, None, None),
491-
)
501+
]
502+
if _use_created:
503+
samples.append(Sample('_created', {}, self._created, None, None))
504+
return tuple(samples)
492505

493506

494507
class Histogram(MetricWrapperBase):
@@ -616,7 +629,8 @@ def _child_samples(self) -> Iterable[Sample]:
616629
samples.append(Sample('_count', {}, acc, None, None))
617630
if self._upper_bounds[0] >= 0:
618631
samples.append(Sample('_sum', {}, self._sum.get(), None, None))
619-
samples.append(Sample('_created', {}, self._created, None, None))
632+
if _use_created:
633+
samples.append(Sample('_created', {}, self._created, None, None))
620634
return tuple(samples)
621635

622636

tests/test_core.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
from concurrent.futures import ThreadPoolExecutor
2+
import os
23
import time
34
import unittest
45

56
import pytest
67

8+
from prometheus_client import metrics
79
from prometheus_client.core import (
810
CollectorRegistry, Counter, CounterMetricFamily, Enum, Gauge,
911
GaugeHistogramMetricFamily, GaugeMetricFamily, Histogram,
1012
HistogramMetricFamily, Info, InfoMetricFamily, Metric, Sample,
1113
StateSetMetricFamily, Summary, SummaryMetricFamily, UntypedMetricFamily,
1214
)
1315
from prometheus_client.decorator import getargspec
16+
from prometheus_client.metrics import _get_use_created
1417

1518

1619
def assert_not_observable(fn, *args, **kwargs):
@@ -115,6 +118,32 @@ def test_exemplar_too_long(self):
115118
})
116119

117120

121+
class TestDisableCreated(unittest.TestCase):
122+
def setUp(self):
123+
self.registry = CollectorRegistry()
124+
os.environ['PROMETHEUS_DISABLE_CREATED_SERIES'] = 'True'
125+
metrics._use_created = _get_use_created()
126+
127+
def tearDown(self):
128+
os.environ.pop('PROMETHEUS_DISABLE_CREATED_SERIES', None)
129+
metrics._use_created = _get_use_created()
130+
131+
def test_counter(self):
132+
counter = Counter('c_total', 'help', registry=self.registry)
133+
counter.inc()
134+
self.assertEqual(None, self.registry.get_sample_value('c_created'))
135+
136+
def test_histogram(self):
137+
histogram = Histogram('h', 'help', registry=self.registry)
138+
histogram.observe(3.2)
139+
self.assertEqual(None, self.registry.get_sample_value('h_created'))
140+
141+
def test_summary(self):
142+
summary = Summary('s', 'help', registry=self.registry)
143+
summary.observe(8.2)
144+
self.assertEqual(None, self.registry.get_sample_value('s_created'))
145+
146+
118147
class TestGauge(unittest.TestCase):
119148
def setUp(self):
120149
self.registry = CollectorRegistry()

0 commit comments

Comments
 (0)
0