Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

Commit 758d439

Browse files
fix!: Use StructuredLogHandler for Cloud Run Job (#898)
* fix!: Use StructuredLogHandler for Cloud Run Job * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: Merge main into experimental-v4 (#906) * fix: Added missing import into logger.py (#896) * test: Fixed unsupported resource type in system test (#904) * fix: Added type hints to CloudLoggingHandler constructor (#903) * fix: Added type hints to CloudLoggingHandler constructor * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Removed Client typing due to circular imports --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * feat!: Use StructuredLogHandler on App Engine instead of CloudLoggingHandler (#908) * feat!: Use StructuredLogHandler on App Engine instead of CloudLoggingHandler * linting * fix!: Use StructuredLogHandler for Cloud Run Job * Removed print statement * Resolved all merge conflicts * linting --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent f92c9b8 commit 758d439

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

google/cloud/logging_v2/client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@
3232
from google.cloud.logging_v2.handlers import setup_logging
3333
from google.cloud.logging_v2.handlers.handlers import EXCLUDED_LOGGER_DEFAULTS
3434
from google.cloud.logging_v2.resource import Resource
35-
from google.cloud.logging_v2.handlers._monitored_resources import detect_resource
36-
35+
from google.cloud.logging_v2.handlers._monitored_resources import (
36+
detect_resource,
37+
_GAE_RESOURCE_TYPE,
38+
_GKE_RESOURCE_TYPE,
39+
_GCF_RESOURCE_TYPE,
40+
_RUN_RESOURCE_TYPE,
41+
_CLOUD_RUN_JOB_RESOURCE_TYPE,
42+
)
3743

3844
from google.cloud.logging_v2.logger import Logger
3945
from google.cloud.logging_v2.metric import Metric
@@ -56,11 +62,6 @@
5662

5763
_USE_GRPC = _HAVE_GRPC and not _DISABLE_GRPC
5864

59-
_GAE_RESOURCE_TYPE = "gae_app"
60-
_GKE_RESOURCE_TYPE = "k8s_container"
61-
_GCF_RESOURCE_TYPE = "cloud_function"
62-
_RUN_RESOURCE_TYPE = "cloud_run_revision"
63-
6465

6566
class Client(ClientWithProject):
6667
"""Client to bundle configuration needed for API requests."""
@@ -381,6 +382,7 @@ def get_default_handler(self, **kw):
381382
_GKE_RESOURCE_TYPE,
382383
_GCF_RESOURCE_TYPE,
383384
_RUN_RESOURCE_TYPE,
385+
_CLOUD_RUN_JOB_RESOURCE_TYPE,
384386
]
385387

386388
if (

google/cloud/logging_v2/handlers/_monitored_resources.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
_GCE_INSTANCE_ID = "instance/id"
6161
"""Attribute in metadata server for compute region and instance."""
6262

63+
_GKE_RESOURCE_TYPE = "k8s_container"
64+
"""Resource type for the GKE environment."""
65+
6366
_GKE_CLUSTER_NAME = "instance/attributes/cluster-name"
6467
"""Attribute in metadata server when in GKE environment."""
6568

@@ -72,6 +75,12 @@
7275
_GAE_RESOURCE_TYPE = "gae_app"
7376
"""Resource type for App Engine environment."""
7477

78+
_GCF_RESOURCE_TYPE = "cloud_function"
79+
"""Resource type for Cloud Functions environment."""
80+
81+
_RUN_RESOURCE_TYPE = "cloud_run_revision"
82+
"""Resource type for Cloud Run environment."""
83+
7584
_CLOUD_RUN_JOB_RESOURCE_TYPE = "cloud_run_job"
7685
"""Resource type for Cloud Run Jobs."""
7786

tests/unit/test_client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,26 @@ def test_get_default_handler_container_engine(self):
798798

799799
self.assertIsInstance(handler, StructuredLogHandler)
800800

801+
def test_get_default_handler_cloud_run_jobs(self):
802+
import os
803+
from google.cloud._testing import _Monkey
804+
from google.cloud.logging_v2.handlers._monitored_resources import (
805+
_CLOUD_RUN_JOB_ENV_VARS,
806+
)
807+
from google.cloud.logging.handlers import StructuredLogHandler
808+
809+
credentials = _make_credentials()
810+
client = self._make_one(
811+
project=self.PROJECT, credentials=credentials, _use_grpc=False
812+
)
813+
814+
cloud_run_job_env_vars = {var: "TRUE" for var in _CLOUD_RUN_JOB_ENV_VARS}
815+
816+
with _Monkey(os, environ=cloud_run_job_env_vars):
817+
handler = client.get_default_handler()
818+
819+
self.assertIsInstance(handler, StructuredLogHandler)
820+
801821
def test_get_default_handler_general(self):
802822
import io
803823
from google.cloud.logging.handlers import CloudLoggingHandler

0 commit comments

Comments
 (0)