예제 #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.

    Supported environments include:

    1. 'gke_container'
    - https://cloud.google.com/monitoring/api/resources#tag_gke_container
    2. 'gce_instance'
    - https://cloud.google.com/monitoring/api/resources#tag_gce_instance
    3. 'aws_ec2_instance'
    - https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance

    :rtype: :class:`opencensus.common.resource.Resource` or None
    :return: A `Resource` configured for the current environment.
    """
    if is_gke_environment():
        return resource.Resource(
            _GKE_CONTAINER,
            gcp_metadata_config.GcpMetadataConfig().get_gke_metadata())
    if is_gce_environment():
        return resource.Resource(
            _GCE_INSTANCE,
            gcp_metadata_config.GcpMetadataConfig().get_gce_metadata())
    if is_aws_environment():
        return resource.Resource(
            _AWS_EC2_INSTANCE, (aws_identity_doc_utils.
                                AwsIdentityDocumentUtils().get_aws_metadata()))

    return None
예제 #2
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)
    def test_export_with_resource(self, mock_get_instance):
        event = threading.Event()
        requests = []

        def _helper(request_iterator, context):
            for r in request_iterator:
                requests.append(r)
                event.set()
            yield

        self._add_and_start_service(GenericRpcHandler(_helper))

        mock_get_instance.return_value = resource.Resource(
            type_='gce_instance', labels={'key1': 'value1'})

        _create_rpc_handler(self._create_stub()).send(
            metrics_service_pb2.ExportMetricsServiceRequest())

        self.assertTrue(event.wait(timeout=1))
        self.assertEqual(
            requests[0].resource,
            resource_pb2.Resource(type='gce_instance',
                                  labels={'key1': 'value1'}))