Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: Use StructuredLogHandler for Cloud Run Job #898

Merged
merged 9 commits into from
Jul 30, 2024
16 changes: 9 additions & 7 deletions google/cloud/logging_v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@
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
Expand All @@ -56,11 +62,6 @@

_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."""
Expand Down Expand Up @@ -381,6 +382,7 @@ def get_default_handler(self, **kw):
_GKE_RESOURCE_TYPE,
_GCF_RESOURCE_TYPE,
_RUN_RESOURCE_TYPE,
_CLOUD_RUN_JOB_RESOURCE_TYPE
]

if (
Expand Down
9 changes: 9 additions & 0 deletions google/cloud/logging_v2/handlers/_monitored_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
_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."""

Expand All @@ -72,6 +75,12 @@
_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."""

Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,26 @@ def test_get_default_handler_container_engine(self):

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
Expand Down