def test_record_call_latency_sync(self): definitions = [ MetricDefinition("Histogram", "histo", "An histogram", ["foo", "bar"]) ] prometheus_metrics = PrometheusMetrics( definitions=definitions, registry=prometheus_client.CollectorRegistry(), ) label_call_args = [] def get_labels(*args, **kwargs): label_call_args.append((args, kwargs)) return {"foo": "FOO", "bar": "BAR"} @prometheus_metrics.record_call_latency("histo", get_labels=get_labels) def func(param1, param2=None): return param1 obj = object() result = func(obj, param2="baz") self.assertIs(result, obj) # the get_labels function is called with the same args as the function self.assertEqual(label_call_args, [((obj, ), {"param2": "baz"})]) self.assertIn( 'histo_count{bar="BAR",foo="FOO"} 1.0', prometheus_metrics.generate_latest().decode("ascii"), )
def test_record_call_latency_async(self): definitions = [ MetricDefinition('Histogram', 'histo', 'An histogram', ['foo', 'bar']) ] prometheus_metrics = PrometheusMetrics( definitions=definitions, registry=prometheus_client.CollectorRegistry()) label_call_args = [] def get_labels(*args, **kwargs): label_call_args.append((args, kwargs)) return {'foo': 'FOO', 'bar': 'BAR'} @prometheus_metrics.record_call_latency('histo', get_labels=get_labels) @inlineCallbacks def func(param1, param2=None): yield returnValue(param1) obj = object() result = yield func(obj, param2='baz') self.assertIs(result, obj) # the get_labels function is called with the same args as the function self.assertEqual(label_call_args, [((obj, ), {'param2': 'baz'})]) self.assertIn('histo_count{bar="BAR",foo="FOO"} 1.0', prometheus_metrics.generate_latest().decode('ascii'))
def test_update_call_value_class(self): definitions = [MetricDefinition('Counter', 'a_counter', 'A Counter')] prometheus_metrics = PrometheusMetrics( definitions=definitions, registry=prometheus_client.CollectorRegistry()) prometheus_metrics.update('a_counter', 'set', value=22) self.assertIn('a_counter 22.0', prometheus_metrics.generate_latest().decode('ascii'))
def test_with_update_handlers(self): def update_gauge(metrics): metrics.update('a_gauge', 'set', value=33) prometheus_metrics = PrometheusMetrics( definitions=[MetricDefinition('Gauge', 'a_gauge', 'A Gauge', [])], update_handlers=[update_gauge], registry=prometheus_client.CollectorRegistry()) self.assertIn('a_gauge 33.0', prometheus_metrics.generate_latest().decode('ascii'))
def test_update_call_value_class(self): definitions = [MetricDefinition("Counter", "a_counter", "A Counter")] prometheus_metrics = PrometheusMetrics( definitions=definitions, registry=prometheus_client.CollectorRegistry(), ) prometheus_metrics.update("a_counter", "set", value=22) self.assertIn( "a_counter 22.0", prometheus_metrics.generate_latest().decode("ascii"), )
def test_with_update_handlers(self): def update_gauge(metrics): metrics.update("a_gauge", "set", value=33) prometheus_metrics = PrometheusMetrics( definitions=[MetricDefinition("Gauge", "a_gauge", "A Gauge", [])], update_handlers=[update_gauge], registry=prometheus_client.CollectorRegistry(), ) self.assertIn( "a_gauge 33.0", prometheus_metrics.generate_latest().decode("ascii"), )
def test_update(self): definitions = [ MetricDefinition('Gauge', 'a_gauge', 'A Gauge', ['foo', 'bar']) ] prometheus_metrics = PrometheusMetrics( definitions=definitions, registry=prometheus_client.CollectorRegistry()) prometheus_metrics.update('a_gauge', 'set', value=22, labels={ 'foo': 'FOO', 'bar': 'BAR' }) self.assertIn('a_gauge{bar="BAR",foo="FOO"} 22.0', prometheus_metrics.generate_latest().decode('ascii'))
def test_update(self): registry = prometheus_client.CollectorRegistry() metric = prometheus_client.Gauge('a_gauge', 'A Gauge', ['foo', 'bar'], registry=registry) prometheus_metrics = PrometheusMetrics(registry=registry, metrics={'a_gauge': metric}) prometheus_metrics.update('a_gauge', 'set', value=22, labels={ 'foo': 'FOO', 'bar': 'BAR' }) self.assertIn('a_gauge{bar="BAR",foo="FOO"} 22.0', prometheus_metrics.generate_latest().decode('ascii'))
def test_update(self): definitions = [ MetricDefinition("Gauge", "a_gauge", "A Gauge", ["foo", "bar"]) ] prometheus_metrics = PrometheusMetrics( definitions=definitions, registry=prometheus_client.CollectorRegistry(), ) prometheus_metrics.update("a_gauge", "set", value=22, labels={ "foo": "FOO", "bar": "BAR" }) self.assertIn( 'a_gauge{bar="BAR",foo="FOO"} 22.0', prometheus_metrics.generate_latest().decode("ascii"), )
def test_update_empty(self): prometheus_metrics = PrometheusMetrics() prometheus_metrics.update('some_metric', 'inc') self.assertIsNone(prometheus_metrics.generate_latest())
def test_empty(self): prometheus_metrics = PrometheusMetrics() self.assertEqual(prometheus_metrics.available_metrics, []) self.assertIsNone(prometheus_metrics.generate_latest())