# Path: google/cloud/logging_v2/resource.py
# class Resource(collections.namedtuple("Resource", "type labels")):
#     """A monitored resource identified by specifying values for all labels.
# 
#     Attributes:
#         type (str): The resource type name.
#         labels (dict): A mapping from label names to values for all labels
#             enumerated in the associated :class:`ResourceDescriptor`.
#     """
# 
#     __slots__ = ()
# 
#     @classmethod
#     def _from_dict(cls, info):
#         """Construct a resource object from the parsed JSON representation.
# 
#         Args:
#             info (dict): A ``dict`` parsed from the JSON wire-format representation.
# 
#         Returns:
#             Resource: A resource object.
#         """
#         return cls(type=info["type"], labels=info.get("labels", {}))
# 
#     def _to_dict(self):
#         """Build a dictionary ready to be serialized to the JSON format.
# 
#         Returns:
#             dict:
#                 A dict representation of the object that can be written to
#                 the API.
#         """
#         return {"type": self.type, "labels": self.labels}
# 
# Path: google/cloud/logging_v2/_helpers.py
# def retrieve_metadata_server(metadata_key, timeout=5):
#     """Retrieve the metadata key in the metadata server.
# 
#     See: https://cloud.google.com/compute/docs/storing-retrieving-metadata
# 
#     Args:
#         metadata_key (str):
#             Key of the metadata which will form the url. You can
#             also supply query parameters after the metadata key.
#             e.g. "tags?alt=json"
#         timeout (number): number of seconds to wait for the HTTP request
# 
#     Returns:
#         str: The value of the metadata key returned by the metadata server.
#     """
#     url = METADATA_URL + metadata_key
# 
#     try:
#         response = requests.get(url, headers=METADATA_HEADERS, timeout=timeout)
# 
#         if response.status_code == requests.codes.ok:
#             return response.text
# 
#     except requests.exceptions.RequestException:
#         # Ignore the exception, connection failed means the attribute does not
#         # exist in the metadata server.
#         pass
# 
#     return None


import os
from google.cloud.logging_v2.resource import Resource
from google.cloud.logging_v2._helpers import retrieve_metadata_server
_FUNCTION_ENV_VARS = [_FUNCTION_TARGET, _FUNCTION_SIGNATURE, _CLOUD_RUN_SERVICE_ID]
_LEGACY_FUNCTION_ENV_VARS = [_FUNCTION_NAME, _FUNCTION_REGION, _FUNCTION_ENTRY]
"""Environment variables set in Cloud Functions environments."""


_REGION_ID = "instance/region"
_ZONE_ID = "instance/zone"
_GCE_INSTANCE_ID = "instance/id"
"""Attribute in metadata server for compute region and instance."""

_GKE_CLUSTER_NAME = "instance/attributes/cluster-name"
"""Attribute in metadata server when in GKE environment."""

_PROJECT_NAME = "project/project-id"
"""Attribute in metadata server when in GKE environment."""


def _create_functions_resource():
    """Create a standardized Cloud Functions resource.
    Returns:
        google.cloud.logging.Resource
    """
    project = retrieve_metadata_server(_PROJECT_NAME)
    region = retrieve_metadata_server(_REGION_ID)
    if _FUNCTION_NAME in os.environ:
        function_name = os.environ.get(_FUNCTION_NAME)
    elif _CLOUD_RUN_SERVICE_ID in os.environ:
        function_name = os.environ.get(_CLOUD_RUN_SERVICE_ID)
    else:
        function_name = ""

# Path: google/cloud/logging_v2/resource.py
# class Resource(collections.namedtuple("Resource", "type labels")):
#     """A monitored resource identified by specifying values for all labels.
# 
#     Attributes:
#         type (str): The resource type name.
#         labels (dict): A mapping from label names to values for all labels
#             enumerated in the associated :class:`ResourceDescriptor`.
#     """
# 
#     __slots__ = ()
# 
#     @classmethod
#     def _from_dict(cls, info):
#         """Construct a resource object from the parsed JSON representation.
# 
#         Args:
#             info (dict): A ``dict`` parsed from the JSON wire-format representation.
# 
#         Returns:
#             Resource: A resource object.
#         """
#         return cls(type=info["type"], labels=info.get("labels", {}))
# 
#     def _to_dict(self):
#         """Build a dictionary ready to be serialized to the JSON format.
# 
#         Returns:
#             dict:
#                 A dict representation of the object that can be written to
#                 the API.
#         """
#         return {"type": self.type, "labels": self.labels}
# 
# Path: google/cloud/logging_v2/_helpers.py
# def retrieve_metadata_server(metadata_key, timeout=5):
#     """Retrieve the metadata key in the metadata server.
# 
#     See: https://cloud.google.com/compute/docs/storing-retrieving-metadata
# 
#     Args:
#         metadata_key (str):
#             Key of the metadata which will form the url. You can
#             also supply query parameters after the metadata key.
#             e.g. "tags?alt=json"
#         timeout (number): number of seconds to wait for the HTTP request
# 
#     Returns:
#         str: The value of the metadata key returned by the metadata server.
#     """
#     url = METADATA_URL + metadata_key
# 
#     try:
#         response = requests.get(url, headers=METADATA_HEADERS, timeout=timeout)
# 
#         if response.status_code == requests.codes.ok:
#             return response.text
# 
#     except requests.exceptions.RequestException:
#         # Ignore the exception, connection failed means the attribute does not
#         # exist in the metadata server.
#         pass
# 
#     return None


# Path: google/cloud/logging_v2/handlers/_monitored_resources.py
import os
from google.cloud.logging_v2.resource import Resource
from google.cloud.logging_v2._helpers import retrieve_metadata_server

_FUNCTION_ENV_VARS = [_FUNCTION_TARGET, _FUNCTION_SIGNATURE, _CLOUD_RUN_SERVICE_ID]
_LEGACY_FUNCTION_ENV_VARS = [_FUNCTION_NAME, _FUNCTION_REGION, _FUNCTION_ENTRY]
"""Environment variables set in Cloud Functions environments."""


_REGION_ID = "instance/region"
_ZONE_ID = "instance/zone"
_GCE_INSTANCE_ID = "instance/id"
"""Attribute in metadata server for compute region and instance."""

_GKE_CLUSTER_NAME = "instance/attributes/cluster-name"
"""Attribute in metadata server when in GKE environment."""

_PROJECT_NAME = "project/project-id"
"""Attribute in metadata server when in GKE environment."""


def _create_functions_resource():
    """Create a standardized Cloud Functions resource.
    Returns:
        google.cloud.logging.Resource
    """
    project = retrieve_metadata_server(_PROJECT_NAME)
    region = retrieve_metadata_server(_REGION_ID)
    if _FUNCTION_NAME in os.environ:
        function_name = os.environ.get(_FUNCTION_NAME)
    elif _CLOUD_RUN_SERVICE_ID in os.environ:
        function_name = os.environ.get(_CLOUD_RUN_SERVICE_ID)
    else:
        function_name = ""

    resource = Resource(