Skip to content

Commit

Permalink
feat: add debug log when creating client (#2265)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
ohmayr and parthea authored Dec 3, 2024
1 parent d874809 commit 8be95a2
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gapic/schema/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ def proto_package(self) -> str:
"""Return the proto package for this type."""
return '.'.join(self.package)

@property
def proto_package_versioned(self) -> str:
"""Return the versioned proto package for this type."""
return ".".join(self.convert_to_versioned_package())

def convert_to_versioned_package(self) -> Tuple[str, ...]:
# We need to change the import statement to use an
# underscore between the module and the version. For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% import "%namespace/%name_%version/%sub/services/%service/_client_macros.j2" as macros %}
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}

import logging as std_logging
from collections import OrderedDict
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, {% if service.any_server_streaming %}AsyncIterable, Awaitable, {% endif %}{% if service.any_client_streaming %}AsyncIterator, {% endif %}Sequence, Tuple, Type, Union
Expand Down Expand Up @@ -52,6 +53,13 @@ from .transports.base import {{ service.name }}Transport, DEFAULT_CLIENT_INFO
from .transports.grpc_asyncio import {{ service.grpc_asyncio_transport_name }}
from .client import {{ service.client_name }}

try: # pragma: NO COVER
from google.api_core import client_logging # type: ignore
CLIENT_LOGGING_SUPPORTED = True
except ImportError:
CLIENT_LOGGING_SUPPORTED = False

_LOGGER = std_logging.getLogger(__name__)

{# TODO(yon-mg): handle rest transport async client interaction #}
class {{ service.async_client_name }}:
Expand Down Expand Up @@ -243,6 +251,17 @@ class {{ service.async_client_name }}:

)

if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
_LOGGER.debug(
"Created client `{{ service.meta.address.proto_package_versioned }}.{{ service.async_client_name }}`.",
extra = {
"serviceName": "{{ service.meta.address.proto }}",
"universeDomain": getattr(self._client._transport._credentials, "universe_domain", ""),
"credentialType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}",
"credentialInfo": getattr(self.transport._credentials, "get_cred_info", lambda: None)(),
},
)

{% for method in service.methods.values() %}
{% with method_name = method.safe_name|snake_case + "_unary" if method.operation_service else method.safe_name|snake_case %}
{%+ if not method.server_streaming %}async {% endif %}def {{ method_name }}(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from collections import OrderedDict
{% if service.any_extended_operations_methods %}
import functools
{% endif %}
import logging as std_logging
import os
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union, cast
Expand Down Expand Up @@ -39,6 +40,14 @@ try:
except AttributeError: # pragma: NO COVER
OptionalRetry = Union[retries.Retry, object, None] # type: ignore

try: # pragma: NO COVER
from google.api_core import client_logging # type: ignore
CLIENT_LOGGING_SUPPORTED = True
except ImportError:
CLIENT_LOGGING_SUPPORTED = False

_LOGGER = std_logging.getLogger(__name__)

{% filter sort_lines %}
{% for method in service.methods.values() %}
{% for ref_type in method.flat_ref_types %}
Expand Down Expand Up @@ -510,6 +519,10 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
# Initialize the universe domain validation.
self._is_universe_domain_valid = False

if CLIENT_LOGGING_SUPPORTED: # pragma: NO COVER
# Setup logging.
client_logging.initialize_logging()

api_key_value = getattr(self._client_options, "api_key", None)
if api_key_value and credentials:
raise ValueError("client_options.api_key and credentials are mutually exclusive")
Expand Down Expand Up @@ -596,6 +609,18 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
always_use_jwt_access=True,
api_audience=self._client_options.api_audience,
)

if "async" not in str(self._transport):
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
_LOGGER.debug(
"Created client `{{ service.meta.address.proto_package_versioned }}.{{ service.client_name }}`.",
extra = {
"serviceName": "{{ service.meta.address.proto }}",
"universeDomain": getattr(self._transport._credentials, "universe_domain", ""),
"credentialType": f"{type(self._transport._credentials).__module__}.{type(self._transport._credentials).__qualname__}",
"credentialInfo": getattr(self.transport._credentials, "get_cred_info", lambda: None)(),
},
)


{% for method in service.methods.values() %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging as std_logging
from collections import OrderedDict
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union
Expand Down Expand Up @@ -46,6 +47,13 @@
from .transports.grpc_asyncio import AssetServiceGrpcAsyncIOTransport
from .client import AssetServiceClient

try: # pragma: NO COVER
from google.api_core import client_logging # type: ignore
CLIENT_LOGGING_SUPPORTED = True
except ImportError:
CLIENT_LOGGING_SUPPORTED = False

_LOGGER = std_logging.getLogger(__name__)

class AssetServiceAsyncClient:
"""Asset service definition."""
Expand Down Expand Up @@ -247,6 +255,17 @@ def __init__(self, *,

)

if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
_LOGGER.debug(
"Created client `google.cloud.asset_v1.AssetServiceAsyncClient`.",
extra = {
"serviceName": "google.cloud.asset.v1.AssetService",
"universeDomain": getattr(self._client._transport._credentials, "universe_domain", ""),
"credentialType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}",
"credentialInfo": getattr(self.transport._credentials, "get_cred_info", lambda: None)(),
},
)

async def export_assets(self,
request: Optional[Union[asset_service.ExportAssetsRequest, dict]] = None,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#
from collections import OrderedDict
import logging as std_logging
import os
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union, cast
Expand All @@ -36,6 +37,14 @@
except AttributeError: # pragma: NO COVER
OptionalRetry = Union[retries.Retry, object, None] # type: ignore

try: # pragma: NO COVER
from google.api_core import client_logging # type: ignore
CLIENT_LOGGING_SUPPORTED = True
except ImportError:
CLIENT_LOGGING_SUPPORTED = False

_LOGGER = std_logging.getLogger(__name__)

from google.api_core import operation # type: ignore
from google.api_core import operation_async # type: ignore
from google.cloud.asset_v1.services.asset_service import pagers
Expand Down Expand Up @@ -566,6 +575,10 @@ def __init__(self, *,
# Initialize the universe domain validation.
self._is_universe_domain_valid = False

if CLIENT_LOGGING_SUPPORTED: # pragma: NO COVER
# Setup logging.
client_logging.initialize_logging()

api_key_value = getattr(self._client_options, "api_key", None)
if api_key_value and credentials:
raise ValueError("client_options.api_key and credentials are mutually exclusive")
Expand Down Expand Up @@ -618,6 +631,18 @@ def __init__(self, *,
api_audience=self._client_options.api_audience,
)

if "async" not in str(self._transport):
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
_LOGGER.debug(
"Created client `google.cloud.asset_v1.AssetServiceClient`.",
extra = {
"serviceName": "google.cloud.asset.v1.AssetService",
"universeDomain": getattr(self._transport._credentials, "universe_domain", ""),
"credentialType": f"{type(self._transport._credentials).__module__}.{type(self._transport._credentials).__qualname__}",
"credentialInfo": getattr(self.transport._credentials, "get_cred_info", lambda: None)(),
},
)

def export_assets(self,
request: Optional[Union[asset_service.ExportAssetsRequest, dict]] = None,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging as std_logging
from collections import OrderedDict
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union
Expand All @@ -39,6 +40,13 @@
from .transports.grpc_asyncio import IAMCredentialsGrpcAsyncIOTransport
from .client import IAMCredentialsClient

try: # pragma: NO COVER
from google.api_core import client_logging # type: ignore
CLIENT_LOGGING_SUPPORTED = True
except ImportError:
CLIENT_LOGGING_SUPPORTED = False

_LOGGER = std_logging.getLogger(__name__)

class IAMCredentialsAsyncClient:
"""A service account is a special type of Google account that
Expand Down Expand Up @@ -238,6 +246,17 @@ def __init__(self, *,

)

if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
_LOGGER.debug(
"Created client `google.iam.credentials_v1.IAMCredentialsAsyncClient`.",
extra = {
"serviceName": "google.iam.credentials.v1.IAMCredentials",
"universeDomain": getattr(self._client._transport._credentials, "universe_domain", ""),
"credentialType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}",
"credentialInfo": getattr(self.transport._credentials, "get_cred_info", lambda: None)(),
},
)

async def generate_access_token(self,
request: Optional[Union[common.GenerateAccessTokenRequest, dict]] = None,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#
from collections import OrderedDict
import logging as std_logging
import os
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union, cast
Expand All @@ -36,6 +37,14 @@
except AttributeError: # pragma: NO COVER
OptionalRetry = Union[retries.Retry, object, None] # type: ignore

try: # pragma: NO COVER
from google.api_core import client_logging # type: ignore
CLIENT_LOGGING_SUPPORTED = True
except ImportError:
CLIENT_LOGGING_SUPPORTED = False

_LOGGER = std_logging.getLogger(__name__)

from google.iam.credentials_v1.types import common
from google.protobuf import duration_pb2 # type: ignore
from google.protobuf import timestamp_pb2 # type: ignore
Expand Down Expand Up @@ -503,6 +512,10 @@ def __init__(self, *,
# Initialize the universe domain validation.
self._is_universe_domain_valid = False

if CLIENT_LOGGING_SUPPORTED: # pragma: NO COVER
# Setup logging.
client_logging.initialize_logging()

api_key_value = getattr(self._client_options, "api_key", None)
if api_key_value and credentials:
raise ValueError("client_options.api_key and credentials are mutually exclusive")
Expand Down Expand Up @@ -555,6 +568,18 @@ def __init__(self, *,
api_audience=self._client_options.api_audience,
)

if "async" not in str(self._transport):
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
_LOGGER.debug(
"Created client `google.iam.credentials_v1.IAMCredentialsClient`.",
extra = {
"serviceName": "google.iam.credentials.v1.IAMCredentials",
"universeDomain": getattr(self._transport._credentials, "universe_domain", ""),
"credentialType": f"{type(self._transport._credentials).__module__}.{type(self._transport._credentials).__qualname__}",
"credentialInfo": getattr(self.transport._credentials, "get_cred_info", lambda: None)(),
},
)

def generate_access_token(self,
request: Optional[Union[common.GenerateAccessTokenRequest, dict]] = None,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging as std_logging
from collections import OrderedDict
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union
Expand Down Expand Up @@ -55,6 +56,13 @@
from .transports.grpc_asyncio import EventarcGrpcAsyncIOTransport
from .client import EventarcClient

try: # pragma: NO COVER
from google.api_core import client_logging # type: ignore
CLIENT_LOGGING_SUPPORTED = True
except ImportError:
CLIENT_LOGGING_SUPPORTED = False

_LOGGER = std_logging.getLogger(__name__)

class EventarcAsyncClient:
"""Eventarc allows users to subscribe to various events that are
Expand Down Expand Up @@ -265,6 +273,17 @@ def __init__(self, *,

)

if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
_LOGGER.debug(
"Created client `google.cloud.eventarc_v1.EventarcAsyncClient`.",
extra = {
"serviceName": "google.cloud.eventarc.v1.Eventarc",
"universeDomain": getattr(self._client._transport._credentials, "universe_domain", ""),
"credentialType": f"{type(self._client._transport._credentials).__module__}.{type(self._client._transport._credentials).__qualname__}",
"credentialInfo": getattr(self.transport._credentials, "get_cred_info", lambda: None)(),
},
)

async def get_trigger(self,
request: Optional[Union[eventarc.GetTriggerRequest, dict]] = None,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#
from collections import OrderedDict
import logging as std_logging
import os
import re
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, Sequence, Tuple, Type, Union, cast
Expand All @@ -36,6 +37,14 @@
except AttributeError: # pragma: NO COVER
OptionalRetry = Union[retries.Retry, object, None] # type: ignore

try: # pragma: NO COVER
from google.api_core import client_logging # type: ignore
CLIENT_LOGGING_SUPPORTED = True
except ImportError:
CLIENT_LOGGING_SUPPORTED = False

_LOGGER = std_logging.getLogger(__name__)

from google.api_core import operation # type: ignore
from google.api_core import operation_async # type: ignore
from google.cloud.eventarc_v1.services.eventarc import pagers
Expand Down Expand Up @@ -611,6 +620,10 @@ def __init__(self, *,
# Initialize the universe domain validation.
self._is_universe_domain_valid = False

if CLIENT_LOGGING_SUPPORTED: # pragma: NO COVER
# Setup logging.
client_logging.initialize_logging()

api_key_value = getattr(self._client_options, "api_key", None)
if api_key_value and credentials:
raise ValueError("client_options.api_key and credentials are mutually exclusive")
Expand Down Expand Up @@ -663,6 +676,18 @@ def __init__(self, *,
api_audience=self._client_options.api_audience,
)

if "async" not in str(self._transport):
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
_LOGGER.debug(
"Created client `google.cloud.eventarc_v1.EventarcClient`.",
extra = {
"serviceName": "google.cloud.eventarc.v1.Eventarc",
"universeDomain": getattr(self._transport._credentials, "universe_domain", ""),
"credentialType": f"{type(self._transport._credentials).__module__}.{type(self._transport._credentials).__qualname__}",
"credentialInfo": getattr(self.transport._credentials, "get_cred_info", lambda: None)(),
},
)

def get_trigger(self,
request: Optional[Union[eventarc.GetTriggerRequest, dict]] = None,
*,
Expand Down
Loading

0 comments on commit 8be95a2

Please sign in to comment.