def test_gcp_k8s_monitored_resource(self, gcp_md_mock): mocked_labels = { 'instance_id': 'my-instance', 'project_id': 'my-project', 'zone': 'us-east1', 'k8s.io/cluster/name': 'cluster', 'k8s.io/pod/name': 'localhost', 'k8s.io/namespace/name': 'namespace', 'k8s.io/container/name': 'container' } cluster_name_key = 'instance/attributes/cluster-name' cluster_name_val = 'cluster' gcp_md_mock.return_value = mock.Mock() gcp_md_mock.return_value.get_gce_metadata.return_value = mocked_labels gcp_md_mock.get_attribute.return_value = cluster_name_val with mock_k8s_env(): r1 = monitored_resource.get_instance() gcp_md_mock.get_attribute.assert_called_once_with(cluster_name_key) self.assertEqual(r1.get_type(), 'k8s_container') self.assertDictContainsSubset(mocked_labels, r1.get_labels()) with mock_oc_env(): with mock_k8s_env(): r2 = monitored_resource.get_instance() self.assertEqual(r1.get_type(), 'k8s_container') self.assertDictContainsSubset(mocked_labels, r1.get_labels()) self.assertDictContainsSubset({'mock_label_key': 'mock_label_value'}, r2.get_labels())
def test_non_supported_environment(self, aws_util_mock, gcp_md_mock): mr = monitored_resource.get_instance() self.assertIsNone(mr) with mock_oc_env(): mr = monitored_resource.get_instance() self.assertIsNotNone(mr) self.assertEqual(mr.get_type(), 'mock_resource_type') self.assertDictEqual(mr.get_labels(), {'mock_label_key': 'mock_label_value'})
def _get_resource(): instance = monitored_resource.get_instance() if instance is not None: return resource_pb2.Resource(type=instance.get_type(), labels=instance.get_labels()) return resource_pb2.Resource(type='global')
def set_monitored_resource(series, option_resource_type): """Set this series' monitored resource and labels. :param series: TimeSeries object based on view data :param option_resource_type: Resource is an optional field that represents the Stackdriver MonitoredResource type. """ if option_resource_type != "": series.resource.type = option_resource_type return resource = monitored_resource.get_instance() if resource is None: series.resource.type = GLOBAL_RESOURCE_TYPE return resource_type = resource.get_type() resource_labels = resource.get_labels() def set_attribute_label(attribute_key, label_key, label_value_prefix=''): """Set a label to timeseries that can be used for monitoring. :param series: TimeSeries object based on view data :param resource_labels: collection of labels :param attribute_key: actual label key :param label_key: optional exporter-specific label key :param label_value_prefix: optional exporter-specific prefix """ if attribute_key not in resource_labels: return series.resource.labels[label_key] = (label_value_prefix + resource_labels[attribute_key]) if resource_type == 'k8s_container': series.resource.type = 'k8s_container' set_attribute_label(gcp_metadata_config.PROJECT_ID_KEY, 'project_id') set_attribute_label(k8s_utils.CLUSTER_NAME_KEY, 'cluster_name') set_attribute_label(k8s_utils.CONTAINER_NAME_KEY, 'container_name') set_attribute_label(k8s_utils.NAMESPACE_NAME_KEY, 'namespace_name') set_attribute_label(k8s_utils.POD_NAME_KEY, 'pod_name') set_attribute_label(gcp_metadata_config.ZONE_KEY, 'location') elif resource_type == 'gce_instance': series.resource.type = 'gce_instance' set_attribute_label(gcp_metadata_config.PROJECT_ID_KEY, 'project_id') set_attribute_label(gcp_metadata_config.INSTANCE_ID_KEY, 'instance_id') set_attribute_label(gcp_metadata_config.ZONE_KEY, 'zone') elif resource_type == 'aws_ec2_instance': series.resource.type = 'aws_ec2_instance' set_attribute_label(aws_identity_doc_utils.ACCOUNT_ID_KEY, 'aws_account') set_attribute_label(aws_identity_doc_utils.INSTANCE_ID_KEY, 'instance_id') set_attribute_label(aws_identity_doc_utils.REGION_KEY, 'region', label_value_prefix='aws:') else: series.resource.type = GLOBAL_RESOURCE_TYPE
def test_k8s_environment(self): patch = mock.patch.dict(os.environ, {'KUBERNETES_SERVICE_HOST': '127.0.0.1'}) with patch: mr = monitored_resource.get_instance() self.assertIsNotNone(mr) self.assertEqual(mr.get_type(), "k8s_container")
def test_gce_environment(self): patch = mock.patch( 'opencensus.common.monitored_resource.' 'gcp_metadata_config.GcpMetadataConfig.' 'is_running_on_gcp', return_value=True) with patch: mr = monitored_resource.get_instance() self.assertIsNotNone(mr) self.assertEqual(mr.get_type(), "gce_instance")
def test_gcp_gce_monitored_resource(self, gcp_md_mock): mocked_labels = { 'instance_id': 'my-instance', 'project_id': 'my-project', 'zone': 'us-east1' } gcp_md_mock.return_value = mock.Mock() gcp_md_mock.return_value.get_gce_metadata.return_value = mocked_labels with mock_gce_env(): resource = monitored_resource.get_instance() self.assertEqual(resource.get_type(), 'gce_instance') self.assertEqual(resource.get_labels(), mocked_labels) with mock_oc_env(): with mock_gce_env(): resource = monitored_resource.get_instance() self.assertEqual(resource.get_type(), 'mock_resource_type') self.assertDictContainsSubset({'mock_label_key': 'mock_label_value'}, resource.get_labels()) self.assertDictContainsSubset(mocked_labels, resource.get_labels())
def test_aws_monitored_resource(self, aws_md_mock): mocked_labels = { 'instance_id': 'i-1234567890abcdef0', 'aws_account': '123456789012', 'region': 'us-west-2' } aws_md_mock.return_value = mock.Mock() aws_md_mock.return_value.get_aws_metadata.return_value = mocked_labels with mock_aws_env(): resource = monitored_resource.get_instance() self.assertEqual(resource.get_type(), 'aws_ec2_instance') self.assertEqual(resource.get_labels(), mocked_labels) with mock_oc_env(): with mock_aws_env(): resource = monitored_resource.get_instance() self.assertEqual(resource.get_type(), 'mock_resource_type') self.assertDictContainsSubset({'mock_label_key': 'mock_label_value'}, resource.get_labels()) self.assertDictContainsSubset(mocked_labels, resource.get_labels())
def set_monitored_resource_attributes(span): """Set labels to span that can be used for tracing. :param span: Span object """ resource = monitored_resource.get_instance() if resource is None: return resource_type = resource.get_type() resource_labels = resource.get_labels() def set_attribute_label(attribute_key, label_key, label_value_prefix=''): """Add the attribute to the span attribute map. Update the span attribute map (`span['attributes']['attributeMap']`) to include a given resource label. """ if attribute_key not in resource_labels: return pair = { RESOURCE_LABEL % (resource_type, label_key): label_value_prefix + resource_labels[attribute_key] } pair_attrs = Attributes(pair).format_attributes_json()\ .get('attributeMap') _update_attr_map(span, pair_attrs) if resource_type == 'k8s_container': set_attribute_label(gcp_metadata_config.PROJECT_ID_KEY, 'project_id') set_attribute_label(k8s_utils.CLUSTER_NAME_KEY, 'cluster_name') set_attribute_label(k8s_utils.CONTAINER_NAME_KEY, 'container_name') set_attribute_label(k8s_utils.NAMESPACE_NAME_KEY, 'namespace_name') set_attribute_label(k8s_utils.POD_NAME_KEY, 'pod_name') set_attribute_label(gcp_metadata_config.ZONE_KEY, 'location') elif resource_type == 'gce_instance': set_attribute_label(gcp_metadata_config.PROJECT_ID_KEY, 'project_id') set_attribute_label(gcp_metadata_config.INSTANCE_ID_KEY, 'instance_id') set_attribute_label(gcp_metadata_config.ZONE_KEY, 'zone') elif resource_type == 'aws_ec2_instance': set_attribute_label(aws_identity_doc_utils.ACCOUNT_ID_KEY, 'aws_account') set_attribute_label(aws_identity_doc_utils.INSTANCE_ID_KEY, 'instance_id') set_attribute_label(aws_identity_doc_utils.REGION_KEY, 'region', label_value_prefix='aws:')
def test_gcp_gce_monitored_resource(self, gcp_metadata_mock): mocked_labels = { 'instance_id': 'my-instance', 'project_id': 'my-project', 'zone': 'us-east1' } gcp_metadata_mock.return_value = mock.Mock() gcp_metadata_mock.return_value.get_gce_metadata.return_value =\ mocked_labels with mock_gce_env(): resource = monitored_resource.get_instance() self.assertEqual(resource.get_type(), 'gce_instance') self.assertDictEqual(resource.get_labels(), mocked_labels)
def test_aws_monitored_resource(self, aws_metadata_mock): mocked_labels = { 'instance_id': 'i-1234567890abcdef0', 'aws_account': '123456789012', 'region': 'us-west-2' } aws_metadata_mock.return_value = mock.Mock() aws_metadata_mock.return_value.get_aws_metadata.return_value =\ mocked_labels with mock_aws_env(): resource = monitored_resource.get_instance() self.assertEqual(resource.get_type(), 'aws_ec2_instance') self.assertDictEqual(resource.get_labels(), mocked_labels)
def test_gcp_k8s_monitored_resource(self): mocked_labels = { 'instance_id': 'my-instance', 'cluster_name': 'cluster', 'project_id': 'my-project', 'zone': 'us-east1', 'pod_id': 'localhost', 'namespace_id': 'namespace', 'container_name': 'container' } with mock_mr_method('k8s_utils.get_k8s_metadata', mocked_labels): with mock_k8s_env(): resource = monitored_resource.get_instance() self.assertEqual(resource.get_type(), 'k8s_container') self.assertDictEqual(resource.get_labels(), mocked_labels)
def test_gcp_gke_monitored_resource(self, gcp_metadata_mock): mocked_labels = { 'instance_id': 'my-instance', 'cluster_name': 'cluster', 'project_id': 'my-project', 'zone': 'us-east1', 'pod_id': 'localhost', 'namespace_id': 'namespace', 'container_name': 'container' } gcp_metadata_mock.return_value = mock.Mock() gcp_metadata_mock.return_value.get_gke_metadata.return_value =\ mocked_labels with mock_gke_env(): resource = monitored_resource.get_instance() self.assertEquals(resource.get_type(), 'gke_container') self.assertEquals(resource.get_labels(), mocked_labels)
def set_monitored_resource(series, option_resource_type): """Set a resource(type and labels) that can be used for monitoring. :param series: TimeSeries object based on view data :param option_resource_type: Resource is an optional field that represents the Stackdriver MonitoredResource type. """ resource_type = GLOBAL_RESOURCE_TYPE if option_resource_type == "": resource = monitored_resource.get_instance() if resource is not None: resource_labels = resource.get_labels() if resource.get_type() == 'gke_container': resource_type = 'k8s_container' set_attribute_label(series, resource_labels, 'project_id') set_attribute_label(series, resource_labels, 'cluster_name') set_attribute_label(series, resource_labels, 'container_name') set_attribute_label(series, resource_labels, 'namespace_id', 'namespace_name') set_attribute_label(series, resource_labels, 'pod_id', 'pod_name') set_attribute_label(series, resource_labels, 'zone', 'location') elif resource.get_type() == 'gce_instance': resource_type = 'gce_instance' set_attribute_label(series, resource_labels, 'project_id') set_attribute_label(series, resource_labels, 'instance_id') set_attribute_label(series, resource_labels, 'zone') elif resource.get_type() == 'aws_ec2_instance': resource_type = 'aws_ec2_instance' set_attribute_label(series, resource_labels, 'aws_account') set_attribute_label(series, resource_labels, 'instance_id') set_attribute_label(series, resource_labels, 'region', label_value_prefix='aws:') else: resource_type = option_resource_type series.resource.type = resource_type
def set_monitored_resource_attributes(span): """Set labels to span that can be used for tracing. :param span: Span object """ resource = monitored_resource.get_instance() if resource is not None: resource_type = resource.get_type() resource_labels = resource.get_labels() if resource_type == 'gke_container': resource_type = 'k8s_container' set_attribute_label(span, resource_type, resource_labels, 'project_id') set_attribute_label(span, resource_type, resource_labels, 'cluster_name') set_attribute_label(span, resource_type, resource_labels, 'container_name') set_attribute_label(span, resource_type, resource_labels, 'namespace_id', 'namespace_name') set_attribute_label(span, resource_type, resource_labels, 'pod_id', 'pod_name') set_attribute_label(span, resource_type, resource_labels, 'zone', 'location') elif resource_type == 'gce_instance': set_attribute_label(span, resource_type, resource_labels, 'project_id') set_attribute_label(span, resource_type, resource_labels, 'instance_id') set_attribute_label(span, resource_type, resource_labels, 'zone') elif resource_type == 'aws_ec2_instance': set_attribute_label(span, resource_type, resource_labels, 'aws_account') set_attribute_label(span, resource_type, resource_labels, 'instance_id') set_attribute_label(span, resource_type, resource_labels, 'region', label_value_prefix='aws:')
def test_non_supported_environment(self, aws_util_mock, gcp_metadata_mock): mr = monitored_resource.get_instance() self.assertIsNone(mr)
def test_aws_environment(self, aws_util_mock, gcp_md_mock): mr = monitored_resource.get_instance() self.assertIsNotNone(mr) self.assertEqual(mr.get_type(), "aws_ec2_instance")