12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import functools
16
+ import logging
15
17
import os
16
18
17
19
from google .cloud .logging_v2 .resource import Resource
67
69
_PROJECT_NAME = "project/project-id"
68
70
"""Attribute in metadata server when in GKE environment."""
69
71
72
+ _GAE_RESOURCE_TYPE = "gae_app"
73
+ """Resource type for App Engine environment."""
74
+
75
+ _CLOUD_RUN_JOB_RESOURCE_TYPE = "cloud_run_job"
76
+ """Resource type for Cloud Run Jobs."""
77
+
78
+ _GAE_TRACE_ID_LABEL = "appengine.googleapis.com/trace_id"
79
+ """Extra trace label to be added on App Engine environments"""
80
+
81
+ _CLOUD_RUN_JOBS_EXECUTION_NAME_LABEL = "run.googleapis.com/execution_name"
82
+ _CLOUD_RUN_JOBS_TASK_INDEX_LABEL = "run.googleapis.com/task_index"
83
+ _CLOUD_RUN_JOBS_TASK_ATTEMPT_LABEL = "run.googleapis.com/task_attempt"
84
+ """Extra labels for Cloud Run environments to be recognized by Cloud Run Jobs web UI."""
85
+
70
86
71
87
def _create_functions_resource ():
72
88
"""Create a standardized Cloud Functions resource.
@@ -159,7 +175,7 @@ def _create_cloud_run_job_resource():
159
175
region = retrieve_metadata_server (_REGION_ID )
160
176
project = retrieve_metadata_server (_PROJECT_NAME )
161
177
resource = Resource (
162
- type = "cloud_run_job" ,
178
+ type = _CLOUD_RUN_JOB_RESOURCE_TYPE ,
163
179
labels = {
164
180
"project_id" : project if project else "" ,
165
181
"job_name" : os .environ .get (_CLOUD_RUN_JOB_ID , "" ),
@@ -177,7 +193,7 @@ def _create_app_engine_resource():
177
193
zone = retrieve_metadata_server (_ZONE_ID )
178
194
project = retrieve_metadata_server (_PROJECT_NAME )
179
195
resource = Resource (
180
- type = "gae_app" ,
196
+ type = _GAE_RESOURCE_TYPE ,
181
197
labels = {
182
198
"project_id" : project if project else "" ,
183
199
"module_id" : os .environ .get (_GAE_SERVICE_ENV , "" ),
@@ -233,3 +249,55 @@ def detect_resource(project=""):
233
249
else :
234
250
# use generic global resource
235
251
return _create_global_resource (project )
252
+
253
+
254
+ @functools .lru_cache (maxsize = None )
255
+ def _get_environmental_labels (resource_type ):
256
+ """Builds a dictionary of labels to be inserted into a LogRecord of the given resource type.
257
+ This function should only build a dict of items that are consistent across multiple logging statements
258
+ of the same resource type, such as environment variables. Th
259
+
260
+ Returns:
261
+ dict:
262
+ A dict representation of labels and the values of those labels
263
+ """
264
+ labels = {}
265
+ environ_vars = {
266
+ _CLOUD_RUN_JOB_RESOURCE_TYPE : {
267
+ _CLOUD_RUN_JOBS_EXECUTION_NAME_LABEL : _CLOUD_RUN_EXECUTION_ID ,
268
+ _CLOUD_RUN_JOBS_TASK_INDEX_LABEL : _CLOUD_RUN_TASK_INDEX ,
269
+ _CLOUD_RUN_JOBS_TASK_ATTEMPT_LABEL : _CLOUD_RUN_TASK_ATTEMPT ,
270
+ }
271
+ }
272
+
273
+ if resource_type in environ_vars :
274
+ for key , env_var in environ_vars [resource_type ].items ():
275
+ val = os .environ .get (env_var , "" )
276
+ if val :
277
+ labels [key ] = val
278
+
279
+ return labels
280
+
281
+
282
+ def add_resource_labels (resource : Resource , record : logging .LogRecord ):
283
+ """Returns additional labels to be appended on to a LogRecord object based on the
284
+ local environment. Defaults to an empty dictionary if none apply. This is only to be
285
+ used for CloudLoggingHandler, as the structured logging daemon already does this.
286
+
287
+ Args:
288
+ resource (google.cloud.logging.Resource): Resource based on the environment
289
+ record (logging.LogRecord): A LogRecord object representing a log record
290
+ Returns:
291
+ Dict[str, str]: New labels to append to the labels of the LogRecord
292
+ """
293
+ if not resource :
294
+ return None
295
+
296
+ # Get environmental labels from the resource type
297
+ labels = _get_environmental_labels (resource .type )
298
+
299
+ # Add labels from log record
300
+ if resource .type == _GAE_RESOURCE_TYPE and record ._trace is not None :
301
+ labels [_GAE_TRACE_ID_LABEL ] = record ._trace
302
+
303
+ return labels
0 commit comments