From cde964271f2995a995d09c768d629cb8a7608aef Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Oct 2020 12:23:52 -0600 Subject: [PATCH] Remove resource attribute from Meter --- .../src/opentelemetry/sdk/metrics/__init__.py | 1 - .../tests/metrics/export/test_export.py | 44 ++++++++++++------- .../tests/metrics/test_metrics.py | 8 ++-- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py index 516ff33ad6c..382dbeb9b9d 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py @@ -353,7 +353,6 @@ def __init__( ): self.instrumentation_info = instrumentation_info self.processor = Processor(source.stateful, source.resource) - self.resource = source.resource self.metrics = set() self.observers = set() self.metrics_lock = threading.Lock() diff --git a/opentelemetry-sdk/tests/metrics/export/test_export.py b/opentelemetry-sdk/tests/metrics/export/test_export.py index 7c596ce9168..b0c74e7093d 100644 --- a/opentelemetry-sdk/tests/metrics/export/test_export.py +++ b/opentelemetry-sdk/tests/metrics/export/test_export.py @@ -38,7 +38,8 @@ class TestConsoleMetricsExporter(unittest.TestCase): # pylint: disable=no-self-use def test_export(self): - meter = metrics.MeterProvider().get_meter(__name__) + meter_provider = metrics.MeterProvider() + meter = meter_provider.get_meter(__name__) exporter = ConsoleMetricsExporter() metric = metrics.Counter( "available memory", @@ -50,13 +51,15 @@ def test_export(self): ) labels = {"environment": "staging"} aggregator = SumAggregator() - record = MetricRecord(metric, labels, aggregator, meter.resource) + record = MetricRecord( + metric, labels, aggregator, meter_provider.resource + ) result = '{}(data="{}", labels="{}", value={}, resource={})'.format( ConsoleMetricsExporter.__name__, metric, labels, aggregator.checkpoint, - meter.resource.attributes, + meter_provider.resource.attributes, ) with mock.patch("sys.stdout") as mock_stdout: exporter.export([record]) @@ -65,8 +68,9 @@ def test_export(self): class TestProcessor(unittest.TestCase): def test_checkpoint_set(self): - meter = metrics.MeterProvider().get_meter(__name__) - processor = Processor(True, meter.resource) + meter_provider = metrics.MeterProvider() + meter = meter_provider.get_meter(__name__) + processor = Processor(True, meter_provider.resource) aggregator = SumAggregator() metric = metrics.Counter( "available memory", "available memory", "bytes", int, meter @@ -88,8 +92,9 @@ def test_checkpoint_set_empty(self): self.assertEqual(len(records), 0) def test_finished_collection_stateless(self): - meter = metrics.MeterProvider().get_meter(__name__) - processor = Processor(False, meter.resource) + meter_provider = metrics.MeterProvider() + meter = meter_provider.get_meter(__name__) + processor = Processor(False, meter_provider.resource) aggregator = SumAggregator() metric = metrics.Counter( "available memory", "available memory", "bytes", int, meter @@ -103,8 +108,9 @@ def test_finished_collection_stateless(self): self.assertEqual(len(processor._batch_map), 0) def test_finished_collection_stateful(self): - meter = metrics.MeterProvider().get_meter(__name__) - processor = Processor(True, meter.resource) + meter_provider = metrics.MeterProvider() + meter = meter_provider.get_meter(__name__) + processor = Processor(True, meter_provider.resource) aggregator = SumAggregator() metric = metrics.Counter( "available memory", "available memory", "bytes", int, meter @@ -118,8 +124,9 @@ def test_finished_collection_stateful(self): self.assertEqual(len(processor._batch_map), 1) def test_processor_process_exists(self): - meter = metrics.MeterProvider().get_meter(__name__) - processor = Processor(True, meter.resource) + meter_provider = metrics.MeterProvider() + meter = meter_provider.get_meter(__name__) + processor = Processor(True, meter_provider.resource) aggregator = SumAggregator() aggregator2 = SumAggregator() metric = metrics.Counter( @@ -139,8 +146,9 @@ def test_processor_process_exists(self): self.assertEqual(processor._batch_map.get(batch_key).checkpoint, 1.0) def test_processor_process_not_exists(self): - meter = metrics.MeterProvider().get_meter(__name__) - processor = Processor(True, meter.resource) + meter_provider = metrics.MeterProvider() + meter = meter_provider.get_meter(__name__) + processor = Processor(True, meter_provider.resource) aggregator = SumAggregator() metric = metrics.Counter( "available memory", "available memory", "bytes", int, meter @@ -158,11 +166,15 @@ def test_processor_process_not_exists(self): self.assertEqual(processor._batch_map.get(batch_key).checkpoint, 1.0) def test_processor_process_not_stateful(self): - meter = metrics.MeterProvider().get_meter(__name__) - processor = Processor(True, meter.resource) + meter_provider = metrics.MeterProvider() + processor = Processor(True, meter_provider.resource) aggregator = SumAggregator() metric = metrics.Counter( - "available memory", "available memory", "bytes", int, meter + "available memory", + "available memory", + "bytes", + int, + meter_provider.get_meter(__name__), ) labels = () _batch_map = {} diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py index 2f833f47820..1fcb2bbda1b 100644 --- a/opentelemetry-sdk/tests/metrics/test_metrics.py +++ b/opentelemetry-sdk/tests/metrics/test_metrics.py @@ -33,14 +33,12 @@ def test_stateful(self): def test_resource(self): resource = resources.Resource.create({}) meter_provider = metrics.MeterProvider(resource=resource) - meter = meter_provider.get_meter(__name__) - self.assertIs(meter.resource, resource) + self.assertIs(meter_provider.resource, resource) def test_resource_empty(self): meter_provider = metrics.MeterProvider() - meter = meter_provider.get_meter(__name__) # pylint: disable=protected-access - self.assertEqual(meter.resource, resources._DEFAULT_RESOURCE) + self.assertEqual(meter_provider.resource, resources._DEFAULT_RESOURCE) def test_start_pipeline(self): exporter = mock.Mock() @@ -167,7 +165,7 @@ def test_create_metric(self): self.assertIsInstance(counter, metrics.Counter) self.assertEqual(counter.value_type, int) self.assertEqual(counter.name, "name") - self.assertIs(counter.meter.resource, resource) + self.assertIs(meter_provider.resource, resource) self.assertEqual(counter.meter, meter) def test_create_updowncounter(self):