def test_monitored_resource_attributes_None(self, mr_mock): trace = {'spans': [{'attributes': {}}]} expected = { 'attributes': { 'attributeMap': { 'g.co/agent': { 'string_value': { 'truncated_byte_count': 0, 'value': 'opencensus-python [{}]'.format(__version__) } } } } } mr_mock.return_value = None trace_exporter.set_attributes(trace) span = trace.get('spans')[0] self.assertEqual(span, expected) mock_resource = mock.Mock() mock_resource.get_type.return_value = mock.Mock() mock_resource.get_labels.return_value = mock.Mock() mr_mock.return_value = mock_resource trace_exporter.set_attributes(trace) span = trace.get('spans')[0] self.assertEqual(span, expected)
def test_set_attributes_gae(self, mr_mock): import os trace = {'spans': [{'attributes': {}}]} expected = { 'attributes': { 'attributeMap': { 'g.co/gae/app/module': { 'string_value': { 'truncated_byte_count': 0, 'value': 'service' } }, 'g.co/gae/app/instance': { 'string_value': { 'truncated_byte_count': 0, 'value': 'flex' } }, 'g.co/gae/app/version': { 'string_value': { 'truncated_byte_count': 0, 'value': 'version' } }, 'g.co/gae/app/project': { 'string_value': { 'truncated_byte_count': 0, 'value': 'project' } }, 'g.co/agent': { 'string_value': { 'truncated_byte_count': 0, 'value': 'opencensus-python [{}]'.format(__version__) } }, } } } with mock.patch.dict( os.environ, { trace_exporter._APPENGINE_FLEXIBLE_ENV_VM: 'vm', trace_exporter._APPENGINE_FLEXIBLE_ENV_FLEX: 'flex', 'GOOGLE_CLOUD_PROJECT': 'project', 'GAE_SERVICE': 'service', 'GAE_VERSION': 'version' }): self.assertTrue(trace_exporter.is_gae_environment()) trace_exporter.set_attributes(trace) span = trace.get('spans')[0] self.assertEqual(span, expected)
def test_monitored_resource_attributes_gce(self, gmr_mock): trace = {'spans': [{'attributes': {}}]} expected = { 'attributes': { 'attributeMap': { 'g.co/agent': { 'string_value': { 'truncated_byte_count': 0, 'value': 'opencensus-python [{}]'.format(__version__) } }, 'g.co/r/gce_instance/project_id': { 'string_value': { 'truncated_byte_count': 0, 'value': 'my_project' } }, 'g.co/r/gce_instance/instance_id': { 'string_value': { 'truncated_byte_count': 0, 'value': '12345' } }, 'g.co/r/gce_instance/zone': { 'string_value': { 'truncated_byte_count': 0, 'value': 'zone1' } }, } } } mock_resource = mock.Mock() mock_resource.get_type.return_value = 'gce_instance' mock_resource.get_labels.return_value = { 'project_id': 'my_project', 'instance_id': '12345', 'zone': 'zone1' } gmr_mock.return_value = mock_resource trace_exporter.set_attributes(trace) span = trace.get('spans')[0] self.assertEqual(span, expected)
def test_monitored_resource_attributes_aws(self, amr_mock): trace = {'spans': [{'attributes': {}}]} expected = { 'attributes': { 'attributeMap': { 'g.co/agent': { 'string_value': { 'truncated_byte_count': 0, 'value': 'opencensus-python [{}]'.format(__version__) } }, 'g.co/r/aws_ec2_instance/aws_account': { 'string_value': { 'truncated_byte_count': 0, 'value': '123456789012' } }, 'g.co/r/aws_ec2_instance/region': { 'string_value': { 'truncated_byte_count': 0, 'value': 'aws:us-west-2' } }, } } } mock_resource = mock.Mock() mock_resource.get_type.return_value = 'aws_ec2_instance' mock_resource.get_labels.return_value = { 'aws_account': '123456789012', 'region': 'us-west-2' } amr_mock.return_value = mock_resource trace_exporter.set_attributes(trace) span = trace.get('spans')[0] self.assertEqual(span, expected)
def test_monitored_resource_attributes_gke(self, gmr_mock): import os trace = {'spans': [{'attributes': {}}]} expected = { 'attributes': { 'attributeMap': { 'g.co/gae/app/module': { 'string_value': { 'truncated_byte_count': 0, 'value': 'service' } }, 'g.co/gae/app/instance': { 'string_value': { 'truncated_byte_count': 0, 'value': 'flex' } }, 'g.co/gae/app/version': { 'string_value': { 'truncated_byte_count': 0, 'value': 'version' } }, 'g.co/gae/app/project': { 'string_value': { 'truncated_byte_count': 0, 'value': 'project' } }, 'g.co/agent': { 'string_value': { 'truncated_byte_count': 0, 'value': 'opencensus-python [{}]'.format(__version__) } }, 'g.co/r/k8s_container/project_id': { 'string_value': { 'truncated_byte_count': 0, 'value': 'my_project' } }, 'g.co/r/k8s_container/location': { 'string_value': { 'truncated_byte_count': 0, 'value': 'zone1' } }, 'g.co/r/k8s_container/namespace_name': { 'string_value': { 'truncated_byte_count': 0, 'value': 'namespace' } }, 'g.co/r/k8s_container/pod_name': { 'string_value': { 'truncated_byte_count': 0, 'value': 'pod' } }, 'g.co/r/k8s_container/cluster_name': { 'string_value': { 'truncated_byte_count': 0, 'value': 'cluster' } }, 'g.co/r/k8s_container/container_name': { 'string_value': { 'truncated_byte_count': 0, 'value': 'c1' } }, } } } mock_resource = mock.Mock() mock_resource.get_type.return_value = 'gke_container' mock_resource.get_labels.return_value = { 'pod_id': 'pod', 'cluster_name': 'cluster', 'namespace_id': 'namespace', 'container_name': 'c1', 'project_id': 'my_project', 'instance_id': 'instance', 'zone': 'zone1' } gmr_mock.return_value = mock_resource with mock.patch.dict( os.environ, { trace_exporter._APPENGINE_FLEXIBLE_ENV_VM: 'vm', trace_exporter._APPENGINE_FLEXIBLE_ENV_FLEX: 'flex', 'GOOGLE_CLOUD_PROJECT': 'project', 'GAE_SERVICE': 'service', 'GAE_VERSION': 'version' }): self.assertTrue(trace_exporter.is_gae_environment()) trace_exporter.set_attributes(trace) span = trace.get('spans')[0] self.assertEqual(span, expected)