diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2025-03-04 18:08:19 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2025-03-04 18:53:10 +0300 |
commit | 125a8ad6292e09dfd2fca510d308c63d52cef422 (patch) | |
tree | 47fa39ed9d2501f7a0ae995080a6d7700ba3b7ae /library/python | |
parent | 0ae3f82349eeb4f353c62dd726e4ba06bbc837f9 (diff) | |
download | ydb-125a8ad6292e09dfd2fca510d308c63d52cef422.tar.gz |
Intermediate changes
commit_hash:acdbcb5ab09c7f6e8d5bf8a01ff1954c04d7a80f
Diffstat (limited to 'library/python')
-rw-r--r-- | library/python/monlib/metric_registry.pxd | 20 | ||||
-rw-r--r-- | library/python/monlib/metric_registry.pyx | 51 | ||||
-rw-r--r-- | library/python/monlib/ut/py2/test.py | 48 | ||||
-rw-r--r-- | library/python/monlib/ut/py3/test.py | 48 |
4 files changed, 156 insertions, 11 deletions
diff --git a/library/python/monlib/metric_registry.pxd b/library/python/monlib/metric_registry.pxd index 998acf3141..8889077e5a 100644 --- a/library/python/monlib/metric_registry.pxd +++ b/library/python/monlib/metric_registry.pxd @@ -8,6 +8,9 @@ from library.python.monlib.metric cimport ( cdef extern from "library/cpp/monlib/metrics/metric_registry.h" namespace "NMonitoring" nogil: + cdef struct TMetricOpts: + bint MemOnly + cdef cppclass TMetricRegistry: TMetricRegistry() except + TMetricRegistry(const TLabels&) except + @@ -19,6 +22,23 @@ cdef extern from "library/cpp/monlib/metrics/metric_registry.h" namespace "NMoni THistogram* HistogramCounter(const TLabels&, IHistogramCollectorPtr collector) except + THistogram* HistogramRate(const TLabels&, IHistogramCollectorPtr collector) except + + TGauge* GaugeWithOpts(const TLabels&, TMetricOpts) except + + TIntGauge* IntGaugeWithOpts(const TLabels&, TMetricOpts) except + + TCounter* CounterWithOpts(const TLabels&, TMetricOpts) except + + TRate* RateWithOpts(const TLabels&, TMetricOpts) except + + + THistogram* HistogramCounterWithOpts( + const TLabels&, + IHistogramCollectorPtr collector, + TMetricOpts opts + ) except + + + THistogram* HistogramRateWithOpts( + const TLabels&, + IHistogramCollectorPtr collector, + TMetricOpts opts + ) except + + void Reset() except + void Clear() except + diff --git a/library/python/monlib/metric_registry.pyx b/library/python/monlib/metric_registry.pyx index 6ddf64cd78..bcebfa219d 100644 --- a/library/python/monlib/metric_registry.pyx +++ b/library/python/monlib/metric_registry.pyx @@ -74,6 +74,12 @@ cdef class MetricRegistry: return native_labels @staticmethod + cdef TMetricOpts _py_to_native_opts(dict kwargs) except *: + cdef TMetricOpts native_opts = TMetricOpts() + native_opts.MemOnly = kwargs.get('mem_only', False) + return native_opts + + @staticmethod cdef _native_to_py_labels(const TLabels& native_labels): result = dict() @@ -94,6 +100,7 @@ cdef class MetricRegistry: def _histogram(self, labels, is_rate, hist_type, **kwargs): cdef TLabels native_labels = MetricRegistry._py_to_native_labels(labels) + cdef TMetricOpts native_opts = MetricRegistry._py_to_native_opts(kwargs) cdef IHistogramCollectorPtr collector cdef TVector[double] native_buckets @@ -117,9 +124,9 @@ cdef class MetricRegistry: cdef THistogram* native_hist if is_rate: - native_hist = self.__wrapped.Get().HistogramRate(native_labels, move(collector)) + native_hist = self.__wrapped.Get().HistogramRateWithOpts(native_labels, move(collector), native_opts) else: - native_hist = self.__wrapped.Get().HistogramCounter(native_labels, move(collector)) + native_hist = self.__wrapped.Get().HistogramCounterWithOpts(native_labels, move(collector), native_opts) return Histogram.from_ptr(native_hist) @@ -135,52 +142,72 @@ cdef class MetricRegistry: return labels - def gauge(self, labels): + def gauge(self, labels, **kwargs): """ Gets a gauge counter or creates a new one in case counter with the specified labels does not exist :param labels: A dict of labels which identifies counter + + Keyword arguments: + :param mem_only: flag for memOnly metric (see: https://m.yandex-team.ru/docs/concepts/glossary#memonly) + :returns: Gauge counter """ cdef TLabels native_labels = MetricRegistry._py_to_native_labels(labels) - native_gauge = self.__wrapped.Get().Gauge(native_labels) + cdef TMetricOpts native_opts = MetricRegistry._py_to_native_opts(kwargs) + native_gauge = self.__wrapped.Get().GaugeWithOpts(native_labels, native_opts) return Gauge.from_ptr(native_gauge) - def int_gauge(self, labels): + def int_gauge(self, labels, **kwargs): """ Gets a gauge counter or creates a new one in case counter with the specified labels does not exist :param labels: A dict of labels which identifies counter + + Keyword arguments: + :param mem_only: flag for memOnly metric (see: https://m.yandex-team.ru/docs/concepts/glossary#memonly) + :returns: IntGauge counter """ cdef TLabels native_labels = MetricRegistry._py_to_native_labels(labels) - native_gauge = self.__wrapped.Get().IntGauge(native_labels) + cdef TMetricOpts native_opts = MetricRegistry._py_to_native_opts(kwargs) + native_gauge = self.__wrapped.Get().IntGaugeWithOpts(native_labels, native_opts) return IntGauge.from_ptr(native_gauge) - def counter(self, labels): + def counter(self, labels, **kwargs): """ Gets a counter or creates a new one in case counter with the specified labels does not exist :param labels: A dict of labels which identifies counter + + Keyword arguments: + :param mem_only: flag for memOnly metric (see: https://m.yandex-team.ru/docs/concepts/glossary#memonly) + :returns: Counter counter """ cdef TLabels native_labels = MetricRegistry._py_to_native_labels(labels) - native_counter = self.__wrapped.Get().Counter(native_labels) + cdef TMetricOpts native_opts = MetricRegistry._py_to_native_opts(kwargs) + native_counter = self.__wrapped.Get().CounterWithOpts(native_labels, native_opts) return Counter.from_ptr(native_counter) - def rate(self, labels): + def rate(self, labels, **kwargs): """ Gets a rate counter or creates a new one in case counter with the specified labels does not exist :param labels: A dict of labels which identifies counter + + Keyword arguments: + :param mem_only: flag for memOnly metric (see: https://m.yandex-team.ru/docs/concepts/glossary#memonly) + :returns: Rate counter """ cdef TLabels native_labels = MetricRegistry._py_to_native_labels(labels) - native_rate = self.__wrapped.Get().Rate(native_labels) + cdef TMetricOpts native_opts = MetricRegistry._py_to_native_opts(kwargs) + native_rate = self.__wrapped.Get().RateWithOpts(native_labels, native_opts) return Rate.from_ptr(native_rate) def histogram_counter(self, labels, hist_type, **kwargs): @@ -197,6 +224,7 @@ cdef class MetricRegistry: :param base: the exponential growth factor for buckets' width (exponential) :param scale: linear scale for the buckets. Must be >= 1.0 (exponential) :param start_value: the upper bound of the first bucket (linear) + :param mem_only: flag for memOnly metric (see: https://m.yandex-team.ru/docs/concepts/glossary#memonly) :returns: Histogram counter @@ -236,6 +264,7 @@ cdef class MetricRegistry: :param base: the exponential growth factor for buckets' width (exponential) :param scale: linear scale for the buckets. Must be >= 1.0 (exponential) :param start_value: the upper bound of the first bucket (linear) + :param mem_only: flag for memOnly metric (see: https://m.yandex-team.ru/docs/concepts/glossary#memonly) :returns: Histogram counter @@ -274,4 +303,4 @@ cdef class MetricRegistry: def remove_metric(self, labels): cdef TLabels native_labels = MetricRegistry._py_to_native_labels(labels) - self.__wrapped.Get().RemoveMetric(native_labels) + self.__wrapped.Get().RemoveMetric(native_labels)
\ No newline at end of file diff --git a/library/python/monlib/ut/py2/test.py b/library/python/monlib/ut/py2/test.py index 0289168f04..c845b04353 100644 --- a/library/python/monlib/ut/py2/test.py +++ b/library/python/monlib/ut/py2/test.py @@ -412,3 +412,51 @@ def test_reset_and_clear_registry(): out = dumps(registry, format="json") j = json.loads(out) assert j == {} + + +def test_mem_only_metrics(): + registry = MetricRegistry() + + registry.gauge({"some": "gauge"}, mem_only=True) + with pytest.raises(Exception): + registry.gauge({"some": "gauge"}) + + registry.int_gauge({"some": "int_gauge"}, mem_only=True) + with pytest.raises(Exception): + registry.int_gauge({"some": "int_gauge"}) + + registry.counter({"some": "counter"}, mem_only=True) + with pytest.raises(Exception): + registry.counter({"some": "counter"}) + + registry.rate({"some": "rate"}, mem_only=True) + with pytest.raises(Exception): + registry.rate({"some": "rate"}) + + registry.histogram_counter( + {"some": "histogram_counter"}, + HistogramType.Explicit, + mem_only=True, + buckets=[1, 5, 15, 20, 25] + ) + with pytest.raises(Exception): + registry.histogram_counter( + {"some": "histogram_counter"}, + HistogramType.Explicit, + buckets=[1, 5, 15, 20, 25], + ) + + registry.histogram_rate( + {"some": "histogram_rate"}, + HistogramType.Exponential, + mem_only=True, + bucket_count=5, + base=2 + ) + with pytest.raises(Exception): + registry.histogram_rate( + {"some": "histogram_rate"}, + HistogramType.Exponential, + bucket_count=5, + base=2 + ) diff --git a/library/python/monlib/ut/py3/test.py b/library/python/monlib/ut/py3/test.py index bbb2b42bca..75cd0494cd 100644 --- a/library/python/monlib/ut/py3/test.py +++ b/library/python/monlib/ut/py3/test.py @@ -410,3 +410,51 @@ def test_reset_and_clear_registry(): out = dumps(registry, format="json") j = json.loads(out) assert j == {} + + +def test_mem_only_metrics(): + registry = MetricRegistry() + + registry.gauge({"some": "gauge"}, mem_only=True) + with pytest.raises(Exception): + registry.gauge({"some": "gauge"}) + + registry.int_gauge({"some": "int_gauge"}, mem_only=True) + with pytest.raises(Exception): + registry.int_gauge({"some": "int_gauge"}) + + registry.counter({"some": "counter"}, mem_only=True) + with pytest.raises(Exception): + registry.counter({"some": "counter"}) + + registry.rate({"some": "rate"}, mem_only=True) + with pytest.raises(Exception): + registry.rate({"some": "rate"}) + + registry.histogram_counter( + {"some": "histogram_counter"}, + HistogramType.Explicit, + mem_only=True, + buckets=[1, 5, 15, 20, 25] + ) + with pytest.raises(Exception): + registry.histogram_counter( + {"some": "histogram_counter"}, + HistogramType.Explicit, + buckets=[1, 5, 15, 20, 25], + ) + + registry.histogram_rate( + {"some": "histogram_rate"}, + HistogramType.Exponential, + mem_only=True, + bucket_count=5, + base=2 + ) + with pytest.raises(Exception): + registry.histogram_rate( + {"some": "histogram_rate"}, + HistogramType.Exponential, + bucket_count=5, + base=2 + ) |