Repository: census-instrumentation/opencensus-python
Pull request #511 (2019-02-20 22:33:55)

Rename monitored_resource_util package
Following #508, this PR renames `monitored_resource_util` to `monitored_resource` and does some incidental import and formatting cleanup.


Patch:
diff --git a/opencensus/common/monitored_resource_util/__init__.py b/opencensus/common/monitored_resource/__init__.py
similarity index 100%
rename from opencensus/common/monitored_resource_util/__init__.py
rename to opencensus/common/monitored_resource/__init__.py
diff --git a/opencensus/common/monitored_resource_util/aws_identity_doc_utils.py b/opencensus/common/monitored_resource/aws_identity_doc_utils.py
similarity index 100%
rename from opencensus/common/monitored_resource_util/aws_identity_doc_utils.py
rename to opencensus/common/monitored_resource/aws_identity_doc_utils.py
diff --git a/opencensus/common/monitored_resource_util/gcp_metadata_config.py b/opencensus/common/monitored_resource/gcp_metadata_config.py
similarity index 100%
rename from opencensus/common/monitored_resource_util/gcp_metadata_config.py
rename to opencensus/common/monitored_resource/gcp_metadata_config.py
diff --git a/opencensus/common/monitored_resource_util/monitored_resource.py b/opencensus/common/monitored_resource/monitored_resource.py
similarity index 94%
rename from opencensus/common/monitored_resource_util/monitored_resource.py
rename to opencensus/common/monitored_resource/monitored_resource.py
index befcf60..7dd062c 100644
--- a/opencensus/common/monitored_resource_util/monitored_resource.py
+++ b/opencensus/common/monitored_resource/monitored_resource.py
@@ -15,8 +15,8 @@
 import os
 
 from opencensus.common import resource
-from opencensus.common.monitored_resource_util import aws_identity_doc_utils
-from opencensus.common.monitored_resource_util import gcp_metadata_config
+from opencensus.common.monitored_resource import aws_identity_doc_utils
+from opencensus.common.monitored_resource import gcp_metadata_config
 
 
 # Supported environments (resource types)
diff --git a/opencensus/stats/exporters/stackdriver_exporter.py b/opencensus/stats/exporters/stackdriver_exporter.py
index 1da1593..ebcce14 100644
--- a/opencensus/stats/exporters/stackdriver_exporter.py
+++ b/opencensus/stats/exporters/stackdriver_exporter.py
@@ -24,7 +24,7 @@ from google.api_core.gapic_v1 import client_info
 from google.cloud import monitoring_v3
 
 from opencensus.common import utils
-from opencensus.common.monitored_resource_util import monitored_resource
+from opencensus.common.monitored_resource import monitored_resource
 from opencensus.common.transports import async_
 from opencensus.common.version import __version__
 from opencensus.stats import aggregation
diff --git a/opencensus/trace/exporters/stackdriver_exporter.py b/opencensus/trace/exporters/stackdriver_exporter.py
index d82da00..e787816 100644
--- a/opencensus/trace/exporters/stackdriver_exporter.py
+++ b/opencensus/trace/exporters/stackdriver_exporter.py
@@ -17,7 +17,7 @@ import os
 
 from google.cloud.trace.client import Client
 
-from opencensus.common.monitored_resource_util import monitored_resource
+from opencensus.common.monitored_resource import monitored_resource
 from opencensus.common.transports import sync
 from opencensus.common.version import __version__
 from opencensus.trace import attributes_helper


Test patch:
diff --git a/tests/unit/common/monitored_resource_util/test_aws_identity_doc_utils.py b/tests/unit/common/monitored_resource_util/test_aws_identity_doc_utils.py
index cda1c30..d744c26 100644
--- a/tests/unit/common/monitored_resource_util/test_aws_identity_doc_utils.py
+++ b/tests/unit/common/monitored_resource_util/test_aws_identity_doc_utils.py
@@ -12,17 +12,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import unittest
-import mock
 import json
-from opencensus.common.monitored_resource_util.aws_identity_doc_utils \
-    import AwsIdentityDocumentUtils
-from opencensus.common.monitored_resource_util import \
-    aws_identity_doc_utils
+import mock
+import unittest
+
+from opencensus.common.monitored_resource import aws_identity_doc_utils
 
 
 class TestAwsIdentityDocumentUtils(unittest.TestCase):
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'aws_identity_doc_utils.get_request')
     def test_get_aws_metadata(self, http_request_mock):
         mocked_http_response = {
@@ -40,13 +38,15 @@ class TestAwsIdentityDocumentUtils(unittest.TestCase):
         }
 
         http_request_mock.return_value = json.dumps(mocked_http_response)
-        AwsIdentityDocumentUtils.inited = False
-        AwsIdentityDocumentUtils.is_running = False
+        aws_identity_doc_utils.AwsIdentityDocumentUtils.inited = False
+        aws_identity_doc_utils.AwsIdentityDocumentUtils.is_running = False
         aws_identity_doc_utils.aws_metadata_map = {}
 
-        self.assertTrue(AwsIdentityDocumentUtils.is_running_on_aws())
+        self.assertTrue(aws_identity_doc_utils.AwsIdentityDocumentUtils
+                        .is_running_on_aws())
 
-        labels_list = AwsIdentityDocumentUtils().get_aws_metadata()
+        labels_list = aws_identity_doc_utils.AwsIdentityDocumentUtils(
+        ).get_aws_metadata()
 
         self.assertEquals(len(labels_list), 3)
 
@@ -58,7 +58,7 @@ class TestAwsIdentityDocumentUtils(unittest.TestCase):
 
         self.assertEquals(labels_list, expected_labels)
 
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'aws_identity_doc_utils.get_request')
     def test_get_aws_metadata_none_fields(self, http_request_mock):
         mocked_http_response = {
@@ -75,13 +75,15 @@ class TestAwsIdentityDocumentUtils(unittest.TestCase):
         }
 
         http_request_mock.return_value = json.dumps(mocked_http_response)
-        AwsIdentityDocumentUtils.inited = False
-        AwsIdentityDocumentUtils.is_running = False
+        aws_identity_doc_utils.AwsIdentityDocumentUtils.inited = False
+        aws_identity_doc_utils.AwsIdentityDocumentUtils.is_running = False
         aws_identity_doc_utils.aws_metadata_map = {}
 
-        self.assertTrue(AwsIdentityDocumentUtils.is_running_on_aws())
+        self.assertTrue(aws_identity_doc_utils.AwsIdentityDocumentUtils
+                        .is_running_on_aws())
 
-        labels_list = AwsIdentityDocumentUtils().get_aws_metadata()
+        labels_list = aws_identity_doc_utils.AwsIdentityDocumentUtils(
+        ).get_aws_metadata()
 
         self.assertEquals(len(labels_list), 2)
 
@@ -92,7 +94,7 @@ class TestAwsIdentityDocumentUtils(unittest.TestCase):
 
         self.assertEquals(labels_list, expected_labels)
 
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'aws_identity_doc_utils.get_request')
     def test_aws_not_running(self, http_request_mock):
         http_request_mock.return_value = None
@@ -100,8 +102,10 @@ class TestAwsIdentityDocumentUtils(unittest.TestCase):
         aws_identity_doc_utils.is_running_on_aws = False
         aws_identity_doc_utils.aws_metadata_map = {}
 
-        self.assertFalse(AwsIdentityDocumentUtils.is_running_on_aws())
+        self.assertFalse(aws_identity_doc_utils.AwsIdentityDocumentUtils
+                         .is_running_on_aws())
 
-        labels_list = AwsIdentityDocumentUtils().get_aws_metadata()
+        labels_list = aws_identity_doc_utils.AwsIdentityDocumentUtils(
+        ).get_aws_metadata()
 
         self.assertEquals(len(labels_list), 0)
diff --git a/tests/unit/common/monitored_resource_util/test_gcp_metadata_config.py b/tests/unit/common/monitored_resource_util/test_gcp_metadata_config.py
index 6010cb1..3a32e83 100644
--- a/tests/unit/common/monitored_resource_util/test_gcp_metadata_config.py
+++ b/tests/unit/common/monitored_resource_util/test_gcp_metadata_config.py
@@ -12,20 +12,17 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import unittest
 import mock
 import os
-from opencensus.common.monitored_resource_util.gcp_metadata_config \
-    import GcpMetadataConfig
-from opencensus.common.monitored_resource_util import gcp_metadata_config
+import unittest
 
+from opencensus.common.monitored_resource import gcp_metadata_config
 
-class TestGcpMetadataConfig(unittest.TestCase):
 
-    @mock.patch('opencensus.common.monitored_resource_util.'
+class TestGcpMetadataConfig(unittest.TestCase):
+    @mock.patch('opencensus.common.monitored_resource.'
                 'gcp_metadata_config.get_request')
     def test_get_gce_metadata(self, http_request_mock):
-
         def assign_attribute_value(*args, **kwargs):
             attribute_uri = args[0].split('/')[-1]
             if attribute_uri == 'id':
@@ -36,13 +33,15 @@ class TestGcpMetadataConfig(unittest.TestCase):
                 return 'us-east1'
 
         http_request_mock.side_effect = assign_attribute_value
-        GcpMetadataConfig.inited = False
-        GcpMetadataConfig.is_running = False
+        gcp_metadata_config.GcpMetadataConfig.inited = False
+        gcp_metadata_config.GcpMetadataConfig.is_running = False
         gcp_metadata_config.gcp_metadata_map = {}
 
-        self.assertTrue(GcpMetadataConfig.is_running_on_gcp())
+        self.assertTrue(
+            gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp())
 
-        labels_list = GcpMetadataConfig().get_gce_metadata()
+        labels_list = gcp_metadata_config.GcpMetadataConfig().get_gce_metadata(
+        )
 
         self.assertEquals(len(labels_list), 3)
 
@@ -54,12 +53,13 @@ class TestGcpMetadataConfig(unittest.TestCase):
 
         self.assertEquals(labels_list, expected_labels)
 
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'gcp_metadata_config.get_request')
     def test_get_gce_metadata_binary_strings(self, http_request_mock):
         """
         At least in python 3 binary strings are returned from urllib
         """
+
         def assign_attribute_value(*args, **kwargs):
             attribute_uri = args[0].split('/')[-1]
             if attribute_uri == 'id':
@@ -70,13 +70,15 @@ class TestGcpMetadataConfig(unittest.TestCase):
                 return b'us-east1'
 
         http_request_mock.side_effect = assign_attribute_value
-        GcpMetadataConfig.inited = False
-        GcpMetadataConfig.is_running = False
+        gcp_metadata_config.GcpMetadataConfig.inited = False
+        gcp_metadata_config.GcpMetadataConfig.is_running = False
         gcp_metadata_config.gcp_metadata_map = {}
 
-        self.assertTrue(GcpMetadataConfig.is_running_on_gcp())
+        self.assertTrue(
+            gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp())
 
-        labels_list = GcpMetadataConfig().get_gce_metadata()
+        labels_list = gcp_metadata_config.GcpMetadataConfig().get_gce_metadata(
+        )
 
         self.assertEquals(len(labels_list), 3)
 
@@ -88,16 +90,17 @@ class TestGcpMetadataConfig(unittest.TestCase):
 
         self.assertEquals(labels_list, expected_labels)
 
-    @mock.patch.dict(os.environ,
-                     {'KUBERNETES_SERVICE_HOST': '127.0.0.1',
-                      'CONTAINER_NAME': 'container',
-                      'NAMESPACE': 'namespace',
-                      'HOSTNAME': 'localhost'}, clear=True
-                     )
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch.dict(
+        os.environ, {
+            'KUBERNETES_SERVICE_HOST': '127.0.0.1',
+            'CONTAINER_NAME': 'container',
+            'NAMESPACE': 'namespace',
+            'HOSTNAME': 'localhost'
+        },
+        clear=True)
+    @mock.patch('opencensus.common.monitored_resource.'
                 'gcp_metadata_config.get_request')
     def test_get_gke_metadata(self, http_request_mock):
-
         def assign_attribute_value(*args, **kwargs):
             attribute_uri = args[0].split('/')[-1]
             if attribute_uri == 'id':
@@ -110,13 +113,15 @@ class TestGcpMetadataConfig(unittest.TestCase):
                 return 'us-east1'
 
         http_request_mock.side_effect = assign_attribute_value
-        GcpMetadataConfig.inited = False
-        GcpMetadataConfig.is_running = False
+        gcp_metadata_config.GcpMetadataConfig.inited = False
+        gcp_metadata_config.GcpMetadataConfig.is_running = False
         gcp_metadata_config.gcp_metadata_map = {}
 
-        self.assertTrue(GcpMetadataConfig.is_running_on_gcp())
+        self.assertTrue(
+            gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp())
 
-        labels_list = GcpMetadataConfig().get_gke_metadata()
+        labels_list = gcp_metadata_config.GcpMetadataConfig().get_gke_metadata(
+        )
 
         self.assertEquals(len(labels_list), 7)
 
@@ -132,15 +137,16 @@ class TestGcpMetadataConfig(unittest.TestCase):
 
         self.assertEquals(labels_list, expected_labels)
 
-    @mock.patch.dict(os.environ,
-                     {'KUBERNETES_SERVICE_HOST': '127.0.0.1',
-                      'NAMESPACE': 'namespace',
-                      'HOSTNAME': 'localhost'}, clear=True
-                     )
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch.dict(
+        os.environ, {
+            'KUBERNETES_SERVICE_HOST': '127.0.0.1',
+            'NAMESPACE': 'namespace',
+            'HOSTNAME': 'localhost'
+        },
+        clear=True)
+    @mock.patch('opencensus.common.monitored_resource.'
                 'gcp_metadata_config.get_request')
     def test_get_gke_metadata_container_empty(self, http_request_mock):
-
         def assign_attribute_value(*args, **kwargs):
             attribute_uri = args[0].split('/')[-1]
             if attribute_uri == 'id':
@@ -151,13 +157,15 @@ class TestGcpMetadataConfig(unittest.TestCase):
                 return 'us-east1'
 
         http_request_mock.side_effect = assign_attribute_value
-        GcpMetadataConfig.inited = False
-        GcpMetadataConfig.is_running = False
+        gcp_metadata_config.GcpMetadataConfig.inited = False
+        gcp_metadata_config.GcpMetadataConfig.is_running = False
         gcp_metadata_config.gcp_metadata_map = {}
 
-        self.assertTrue(GcpMetadataConfig.is_running_on_gcp())
+        self.assertTrue(
+            gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp())
 
-        labels_list = GcpMetadataConfig().get_gke_metadata()
+        labels_list = gcp_metadata_config.GcpMetadataConfig().get_gke_metadata(
+        )
 
         self.assertEquals(len(labels_list), 5)
 
@@ -172,15 +180,18 @@ class TestGcpMetadataConfig(unittest.TestCase):
         self.assertEquals(labels_list, expected_labels)
 
     @mock.patch.dict(os.environ, clear=True)
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'gcp_metadata_config.get_request')
     def test_gcp_not_running(self, http_request_mock):
         http_request_mock.return_value = None
-        GcpMetadataConfig.inited = False
-        GcpMetadataConfig.is_running = False
+        gcp_metadata_config.GcpMetadataConfig.inited = False
+        gcp_metadata_config.GcpMetadataConfig.is_running = False
         gcp_metadata_config.gcp_metadata_map = {}
 
-        self.assertFalse(GcpMetadataConfig.is_running_on_gcp())
+        self.assertFalse(
+            gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp())
 
-        self.assertEquals(len(GcpMetadataConfig().get_gce_metadata()), 0)
-        self.assertEquals(len(GcpMetadataConfig().get_gke_metadata()), 0)
+        self.assertEquals(
+            len(gcp_metadata_config.GcpMetadataConfig().get_gce_metadata()), 0)
+        self.assertEquals(
+            len(gcp_metadata_config.GcpMetadataConfig().get_gke_metadata()), 0)
diff --git a/tests/unit/common/monitored_resource_util/test_monitored_resource.py b/tests/unit/common/monitored_resource_util/test_monitored_resource.py
index af42feb..4f158bd 100644
--- a/tests/unit/common/monitored_resource_util/test_monitored_resource.py
+++ b/tests/unit/common/monitored_resource_util/test_monitored_resource.py
@@ -17,7 +17,7 @@ import mock
 import os
 import unittest
 
-from opencensus.common.monitored_resource_util import monitored_resource
+from opencensus.common.monitored_resource import monitored_resource
 
 
 @contextmanager
@@ -65,7 +65,7 @@ def mock_aws_env():
 
 class TestMonitoredResource(unittest.TestCase):
 
-    @mock.patch('opencensus.common.monitored_resource_util.monitored_resource'
+    @mock.patch('opencensus.common.monitored_resource.monitored_resource'
                 '.gcp_metadata_config.GcpMetadataConfig')
     def test_gcp_gce_monitored_resource(self, gcp_metadata_mock):
         mocked_labels = {
@@ -82,7 +82,7 @@ class TestMonitoredResource(unittest.TestCase):
         self.assertEquals(resource.get_type(), 'gce_instance')
         self.assertEquals(resource.get_labels(), mocked_labels)
 
-    @mock.patch('opencensus.common.monitored_resource_util.monitored_resource'
+    @mock.patch('opencensus.common.monitored_resource.monitored_resource'
                 '.gcp_metadata_config.GcpMetadataConfig')
     def test_gcp_gke_monitored_resource(self, gcp_metadata_mock):
 
@@ -104,7 +104,7 @@ class TestMonitoredResource(unittest.TestCase):
         self.assertEquals(resource.get_type(), 'gke_container')
         self.assertEquals(resource.get_labels(), mocked_labels)
 
-    @mock.patch('opencensus.common.monitored_resource_util.monitored_resource'
+    @mock.patch('opencensus.common.monitored_resource.monitored_resource'
                 '.aws_identity_doc_utils.AwsIdentityDocumentUtils')
     def test_aws_monitored_resource(self, aws_metadata_mock):
 
@@ -135,7 +135,7 @@ class TestMonitoredResource(unittest.TestCase):
 
     def test_gce_environment(self):
         patch = mock.patch(
-            'opencensus.common.monitored_resource_util.'
+            'opencensus.common.monitored_resource.'
             'gcp_metadata_config.GcpMetadataConfig.'
             'is_running_on_gcp',
             return_value=True)
@@ -145,10 +145,10 @@ class TestMonitoredResource(unittest.TestCase):
             self.assertIsNotNone(mr)
             self.assertEqual(mr.get_type(), "gce_instance")
 
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp',
                 return_value=False)
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'aws_identity_doc_utils.AwsIdentityDocumentUtils.'
                 'is_running_on_aws',
                 return_value=True)
@@ -158,10 +158,10 @@ class TestMonitoredResource(unittest.TestCase):
         self.assertIsNotNone(mr)
         self.assertEqual(mr.get_type(), "aws_ec2_instance")
 
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp',
                 return_value=False)
-    @mock.patch('opencensus.common.monitored_resource_util.'
+    @mock.patch('opencensus.common.monitored_resource.'
                 'aws_identity_doc_utils.AwsIdentityDocumentUtils.'
                 'is_running_on_aws',
                 return_value=False)