Repository: googleapis/python-logging

Issue:
Let Cloud Run Job use `StructuredLogHandler` instead of `ClougLoggingHandler`
Thanks for stopping by to let us know something could be better!

**PLEASE READ**: If you have a support contract with Google, please create an issue in the [support console](https://cloud.google.com/support/) instead of filing on GitHub. This will ensure a timely response.

 **Is your feature request related to a problem? Please describe.**
When using `Client.get_default_handler` `CloudLoggingHandler` is being used automatically for Cloud Run jobs resource. And while https://github.com/googleapis/python-logging/pull/877 fixes the missing labels we're still gonna be using background transport when simplier stdout is doing just fine.

 **Describe the solution you'd like**
If we modify this check https://github.com/googleapis/python-logging/blob/62641075042a3da9bb9c059d963bad14a1586b1c/google/cloud/logging_v2/client.py#L388 and allow `_RUN_RESOURCE_TYPE` OR `_CLOUD_RUN_JOB_RESOURCE_TYPE` resource type then a structured log handler is gonna be used automatically which should be a better default.

 **Describe alternatives you've considered**
Meanwhile I am using `handler = StructuredLogHandler(project_id=client.project)` instead of `handler = client.get_default_handler()` where client is `client = CloudLoggingClient()`


Discussion:

Patch:
diff --git a/google/cloud/logging_v2/client.py b/google/cloud/logging_v2/client.py
index 13aca9e..9a4e520 100644
--- a/google/cloud/logging_v2/client.py
+++ b/google/cloud/logging_v2/client.py
@@ -32,8 +32,14 @@ from google.cloud.logging_v2.handlers import StructuredLogHandler
 from google.cloud.logging_v2.handlers import setup_logging
 from google.cloud.logging_v2.handlers.handlers import EXCLUDED_LOGGER_DEFAULTS
 from google.cloud.logging_v2.resource import Resource
-from google.cloud.logging_v2.handlers._monitored_resources import detect_resource
-
+from google.cloud.logging_v2.handlers._monitored_resources import (
+    detect_resource,
+    _GAE_RESOURCE_TYPE,
+    _GKE_RESOURCE_TYPE,
+    _GCF_RESOURCE_TYPE,
+    _RUN_RESOURCE_TYPE,
+    _CLOUD_RUN_JOB_RESOURCE_TYPE,
+)
 
 from google.cloud.logging_v2.logger import Logger
 from google.cloud.logging_v2.metric import Metric
@@ -56,11 +62,6 @@ except ImportError:  # pragma: NO COVER
 
 _USE_GRPC = _HAVE_GRPC and not _DISABLE_GRPC
 
-_GAE_RESOURCE_TYPE = "gae_app"
-_GKE_RESOURCE_TYPE = "k8s_container"
-_GCF_RESOURCE_TYPE = "cloud_function"
-_RUN_RESOURCE_TYPE = "cloud_run_revision"
-
 
 class Client(ClientWithProject):
     """Client to bundle configuration needed for API requests."""
@@ -381,6 +382,7 @@ class Client(ClientWithProject):
             _GKE_RESOURCE_TYPE,
             _GCF_RESOURCE_TYPE,
             _RUN_RESOURCE_TYPE,
+            _CLOUD_RUN_JOB_RESOURCE_TYPE,
         ]
 
         if (
diff --git a/google/cloud/logging_v2/handlers/_monitored_resources.py b/google/cloud/logging_v2/handlers/_monitored_resources.py
index 5240fe7..c15c42d 100644
--- a/google/cloud/logging_v2/handlers/_monitored_resources.py
+++ b/google/cloud/logging_v2/handlers/_monitored_resources.py
@@ -60,6 +60,9 @@ _ZONE_ID = "instance/zone"
 _GCE_INSTANCE_ID = "instance/id"
 """Attribute in metadata server for compute region and instance."""
 
+_GKE_RESOURCE_TYPE = "k8s_container"
+"""Resource type for the GKE environment."""
+
 _GKE_CLUSTER_NAME = "instance/attributes/cluster-name"
 """Attribute in metadata server when in GKE environment."""
 
@@ -72,6 +75,12 @@ _PROJECT_NAME = "project/project-id"
 _GAE_RESOURCE_TYPE = "gae_app"
 """Resource type for App Engine environment."""
 
+_GCF_RESOURCE_TYPE = "cloud_function"
+"""Resource type for Cloud Functions environment."""
+
+_RUN_RESOURCE_TYPE = "cloud_run_revision"
+"""Resource type for Cloud Run environment."""
+
 _CLOUD_RUN_JOB_RESOURCE_TYPE = "cloud_run_job"
 """Resource type for Cloud Run Jobs."""
 


Test patch:
diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py
index dc74009..4bfd984 100644
--- a/tests/unit/test_client.py
+++ b/tests/unit/test_client.py
@@ -798,6 +798,26 @@ class TestClient(unittest.TestCase):
 
         self.assertIsInstance(handler, StructuredLogHandler)
 
+    def test_get_default_handler_cloud_run_jobs(self):
+        import os
+        from google.cloud._testing import _Monkey
+        from google.cloud.logging_v2.handlers._monitored_resources import (
+            _CLOUD_RUN_JOB_ENV_VARS,
+        )
+        from google.cloud.logging.handlers import StructuredLogHandler
+
+        credentials = _make_credentials()
+        client = self._make_one(
+            project=self.PROJECT, credentials=credentials, _use_grpc=False
+        )
+
+        cloud_run_job_env_vars = {var: "TRUE" for var in _CLOUD_RUN_JOB_ENV_VARS}
+
+        with _Monkey(os, environ=cloud_run_job_env_vars):
+            handler = client.get_default_handler()
+
+        self.assertIsInstance(handler, StructuredLogHandler)
+
     def test_get_default_handler_general(self):
         import io
         from google.cloud.logging.handlers import CloudLoggingHandler