コード例 #1
0
def get_instance():
    """Get a resource based on the application environment.

    Returns a `Resource` configured for the current environment, or None if the
    environment is unknown or unsupported.

    :rtype: :class:`opencensus.common.resource.Resource` or None
    :return: A `Resource` configured for the current environment.
    """
    resources = []
    env_resource = resource.get_from_env()
    if env_resource is not None:
        resources.append(env_resource)

    if k8s_utils.is_k8s_environment():
        resources.append(resource.Resource(
            _K8S_CONTAINER, k8s_utils.get_k8s_metadata()))

    if is_gce_environment():
        resources.append(resource.Resource(
            _GCE_INSTANCE,
            gcp_metadata_config.GcpMetadataConfig().get_gce_metadata()))
    elif is_aws_environment():
        resources.append(resource.Resource(
            _AWS_EC2_INSTANCE,
            (aws_identity_doc_utils.AwsIdentityDocumentUtils()
             .get_aws_metadata())))

    if not resources:
        return None
    return resource.merge_resources(resources)
コード例 #2
0
 def test_get_from_env_no_labels(self):
     with mock.patch.dict('os.environ', {
             'OC_RESOURCE_TYPE': 'opencensus.io/example',
     }):
         resource = resource_module.get_from_env()
     self.assertEqual(resource.type, 'opencensus.io/example')
     self.assertDictEqual(resource.labels, {})
コード例 #3
0
 def test_get_from_env_quoted_chars(self):
     with mock.patch.dict(
             'os.environ', {
                 'OC_RESOURCE_TYPE': 'opencensus.io/example',
                 'OC_RESOURCE_LABELS': '"k1=\'"="v1,,,", "k2"=\'="=??\''
             }):
         resource = resource_module.get_from_env()
     self.assertDictEqual(resource.labels, {"k1='": 'v1,,,', 'k2': '="=??'})
コード例 #4
0
 def test_get_from_env_no_type(self):
     with mock.patch.dict('os.environ',
                          {'OC_RESOURCE_LABELS': 'k1=v1,k2=v2'}):
         try:
             del os.environ['OC_RESOURCE_TYPE']
         except KeyError:
             pass
         self.assertIsNone(resource_module.get_from_env())
コード例 #5
0
 def test_get_from_env_outer_spaces(self):
     with mock.patch.dict(
             'os.environ', {
                 'OC_RESOURCE_TYPE': ' opencensus.io/example  ',
                 'OC_RESOURCE_LABELS': 'k1= v1 ,  k2=v2  '
             }):
         resource = resource_module.get_from_env()
     self.assertEqual(resource.type, 'opencensus.io/example')
     self.assertDictEqual(resource.labels, {'k1': 'v1', 'k2': 'v2'})
コード例 #6
0
 def test_get_from_env_inner_spaces(self):
     # Spaces inside key/label values should be contained by quotes, refuse
     # to parse this.
     with mock.patch.dict(
             'os.environ',
         {
             'OC_RESOURCE_TYPE': 'opencensus.io / example',
             'OC_RESOURCE_LABELS': 'key one=value one,  key two= value two'
         }):
         resource = resource_module.get_from_env()
     self.assertEqual(resource.type, 'opencensus.io / example')
     self.assertDictEqual(resource.labels, {})
コード例 #7
0
 def test_get_from_env_no_type(self):
     with mock.patch.dict('os.environ',
                          {'OC_RESOURCE_LABELS': 'k1=v1,k2=v2'}):
         self.assertIsNone(resource_module.get_from_env())