Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/logging_v2/handlers/_monitored_resources.py: 42%
71 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:30 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:30 +0000
1# Copyright 2021 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15import os
17from google.cloud.logging_v2.resource import Resource
18from google.cloud.logging_v2._helpers import retrieve_metadata_server
20_GAE_SERVICE_ENV = "GAE_SERVICE"
21_GAE_VERSION_ENV = "GAE_VERSION"
22_GAE_INSTANCE_ENV = "GAE_INSTANCE"
23_GAE_ENV_VARS = [_GAE_SERVICE_ENV, _GAE_VERSION_ENV, _GAE_INSTANCE_ENV]
24"""Environment variables set in App Engine environment."""
26_CLOUD_RUN_SERVICE_ID = "K_SERVICE"
27_CLOUD_RUN_REVISION_ID = "K_REVISION"
28_CLOUD_RUN_CONFIGURATION_ID = "K_CONFIGURATION"
29_CLOUD_RUN_ENV_VARS = [
30 _CLOUD_RUN_SERVICE_ID,
31 _CLOUD_RUN_REVISION_ID,
32 _CLOUD_RUN_CONFIGURATION_ID,
33]
34"""Environment variables set in Cloud Run environment."""
36_FUNCTION_TARGET = "FUNCTION_TARGET"
37_FUNCTION_SIGNATURE = "FUNCTION_SIGNATURE_TYPE"
38_FUNCTION_NAME = "FUNCTION_NAME"
39_FUNCTION_REGION = "FUNCTION_REGION"
40_FUNCTION_ENTRY = "ENTRY_POINT"
41_FUNCTION_ENV_VARS = [_FUNCTION_TARGET, _FUNCTION_SIGNATURE, _CLOUD_RUN_SERVICE_ID]
42_LEGACY_FUNCTION_ENV_VARS = [_FUNCTION_NAME, _FUNCTION_REGION, _FUNCTION_ENTRY]
43"""Environment variables set in Cloud Functions environments."""
46_REGION_ID = "instance/region"
47_ZONE_ID = "instance/zone"
48_GCE_INSTANCE_ID = "instance/id"
49"""Attribute in metadata server for compute region and instance."""
51_GKE_CLUSTER_NAME = "instance/attributes/cluster-name"
52"""Attribute in metadata server when in GKE environment."""
54_PROJECT_NAME = "project/project-id"
55"""Attribute in metadata server when in GKE environment."""
58def _create_functions_resource():
59 """Create a standardized Cloud Functions resource.
60 Returns:
61 google.cloud.logging.Resource
62 """
63 project = retrieve_metadata_server(_PROJECT_NAME)
64 region = retrieve_metadata_server(_REGION_ID)
65 if _FUNCTION_NAME in os.environ:
66 function_name = os.environ.get(_FUNCTION_NAME)
67 elif _CLOUD_RUN_SERVICE_ID in os.environ:
68 function_name = os.environ.get(_CLOUD_RUN_SERVICE_ID)
69 else:
70 function_name = ""
71 resource = Resource(
72 type="cloud_function",
73 labels={
74 "project_id": project,
75 "function_name": function_name,
76 "region": region.split("/")[-1] if region else "",
77 },
78 )
79 return resource
82def _create_kubernetes_resource():
83 """Create a standardized Kubernetes resource.
84 Returns:
85 google.cloud.logging.Resource
86 """
87 zone = retrieve_metadata_server(_ZONE_ID)
88 cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME)
89 project = retrieve_metadata_server(_PROJECT_NAME)
91 resource = Resource(
92 type="k8s_container",
93 labels={
94 "project_id": project,
95 "location": zone if zone else "",
96 "cluster_name": cluster_name if cluster_name else "",
97 },
98 )
99 return resource
102def _create_compute_resource():
103 """Create a standardized Compute Engine resource.
104 Returns:
105 google.cloud.logging.Resource
106 """
107 instance = retrieve_metadata_server(_GCE_INSTANCE_ID)
108 zone = retrieve_metadata_server(_ZONE_ID)
109 project = retrieve_metadata_server(_PROJECT_NAME)
110 resource = Resource(
111 type="gce_instance",
112 labels={
113 "project_id": project,
114 "instance_id": instance if instance else "",
115 "zone": zone if zone else "",
116 },
117 )
118 return resource
121def _create_cloud_run_resource():
122 """Create a standardized Cloud Run resource.
123 Returns:
124 google.cloud.logging.Resource
125 """
126 region = retrieve_metadata_server(_REGION_ID)
127 project = retrieve_metadata_server(_PROJECT_NAME)
128 resource = Resource(
129 type="cloud_run_revision",
130 labels={
131 "project_id": project,
132 "service_name": os.environ.get(_CLOUD_RUN_SERVICE_ID, ""),
133 "revision_name": os.environ.get(_CLOUD_RUN_REVISION_ID, ""),
134 "location": region.split("/")[-1] if region else "",
135 "configuration_name": os.environ.get(_CLOUD_RUN_CONFIGURATION_ID, ""),
136 },
137 )
138 return resource
141def _create_app_engine_resource():
142 """Create a standardized App Engine resource.
143 Returns:
144 google.cloud.logging.Resource
145 """
146 zone = retrieve_metadata_server(_ZONE_ID)
147 project = retrieve_metadata_server(_PROJECT_NAME)
148 resource = Resource(
149 type="gae_app",
150 labels={
151 "project_id": project,
152 "module_id": os.environ.get(_GAE_SERVICE_ENV, ""),
153 "version_id": os.environ.get(_GAE_VERSION_ENV, ""),
154 "zone": zone if zone else "",
155 },
156 )
157 return resource
160def _create_global_resource(project):
161 """Create a global resource.
162 Args:
163 project (str): The project ID to pass on to the resource
164 Returns:
165 google.cloud.logging.Resource
166 """
167 return Resource(type="global", labels={"project_id": project})
170def detect_resource(project=""):
171 """Return the default monitored resource based on the local environment.
172 If GCP resource not found, defaults to `global`.
174 Args:
175 project (str): The project ID to pass on to the resource (if needed)
176 Returns:
177 google.cloud.logging.Resource: The default resource based on the environment
178 """
179 gke_cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME)
180 gce_instance_name = retrieve_metadata_server(_GCE_INSTANCE_ID)
182 if all([env in os.environ for env in _GAE_ENV_VARS]):
183 # App Engine Flex or Standard
184 return _create_app_engine_resource()
185 elif gke_cluster_name is not None:
186 # Kubernetes Engine
187 return _create_kubernetes_resource()
188 elif all([env in os.environ for env in _LEGACY_FUNCTION_ENV_VARS]) or all(
189 [env in os.environ for env in _FUNCTION_ENV_VARS]
190 ):
191 # Cloud Functions
192 return _create_functions_resource()
193 elif all([env in os.environ for env in _CLOUD_RUN_ENV_VARS]):
194 # Cloud Run
195 return _create_cloud_run_resource()
196 elif gce_instance_name is not None:
197 # Compute Engine
198 return _create_compute_resource()
199 else:
200 # use generic global resource
201 return _create_global_resource(project)