Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,42 @@ def _get_http_options():
{% endmacro %}


{% macro unary_request_interceptor_common(service) %}
logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG)
if logging_enabled: # pragma: NO COVER
request_metadata = client_call_details.metadata
if isinstance(request, proto.Message):
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2293): Investigate if we can improve this logic
or wait for next gen protobuf.
#}
request_payload = type(request).to_json(request)
elif isinstance(request, google.protobuf.message.Message):
request_payload = MessageToJson(request)
else:
request_payload = f"{type(request).__name__}: {pickle.dumps(request)}"

request_metadata = {
key: value.decode("utf-8") if isinstance(value, bytes) else value
for key, value in request_metadata
}
grpc_request = {
"payload": request_payload,
"requestMethod": "grpc",
"metadata": dict(request_metadata),
}
_LOGGER.debug(
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "{{ service.meta.address.proto }}",
"rpcName": str(client_call_details.method),
"request": grpc_request,
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2275): logging `metadata` seems repetitive and may need to be cleaned up. We're including it within "request" for consistency with REST transport. #}
"metadata": grpc_request["metadata"],
},
)
{% endmacro %}


{% macro prep_wrapped_messages_async_method(api, service) %}
def _prep_wrapped_messages(self, client_info):
""" Precompute the wrapped methods, overriding the base class method to use async wrappers."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% extends '_base.py.j2' %}

{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}

{% block content %}

import json
Expand Down Expand Up @@ -59,39 +61,7 @@ _LOGGER = std_logging.getLogger(__name__)

class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER
def intercept_unary_unary(self, continuation, client_call_details, request):
logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG)
if logging_enabled: # pragma: NO COVER
request_metadata = client_call_details.metadata
if isinstance(request, proto.Message):
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2293): Investigate if we can improve this logic
or wait for next gen protobuf.
#}
request_payload = type(request).to_json(request)
elif isinstance(request, google.protobuf.message.Message):
request_payload = MessageToJson(request)
else:
request_payload = f"{type(request).__name__}: {pickle.dumps(request)}"

request_metadata = {
key: value.decode("utf-8") if isinstance(value, bytes) else value
for key, value in request_metadata
}
grpc_request = {
"payload": request_payload,
"requestMethod": "grpc",
"metadata": dict(request_metadata),
}
_LOGGER.debug(
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "{{ service.meta.address.proto }}",
"rpcName": client_call_details.method,
"request": grpc_request,
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2275): logging `metadata` seems repetitive and may need to be cleaned up. We're including it within "request" for consistency with REST transport. #}
"metadata": grpc_request["metadata"],
},
)

{{ shared_macros.unary_request_interceptor_common(service) }}
response = continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% extends '_base.py.j2' %}

{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}

{% block content %}
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}

Expand Down Expand Up @@ -64,38 +66,7 @@ _LOGGER = std_logging.getLogger(__name__)

class _LoggingClientAIOInterceptor(grpc.aio.UnaryUnaryClientInterceptor): # pragma: NO COVER
async def intercept_unary_unary(self, continuation, client_call_details, request):
logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG)
if logging_enabled: # pragma: NO COVER
request_metadata = client_call_details.metadata
if isinstance(request, proto.Message):
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2293): Investigate if we can improve this logic
or wait for next gen protobuf.
#}
request_payload = type(request).to_json(request)
elif isinstance(request, google.protobuf.message.Message):
request_payload = MessageToJson(request)
else:
request_payload = f"{type(request).__name__}: {pickle.dumps(request)}"

request_metadata = {
key: value.decode("utf-8") if isinstance(value, bytes) else value
for key, value in request_metadata
}
grpc_request = {
"payload": request_payload,
"requestMethod": "grpc",
"metadata": dict(request_metadata),
}
_LOGGER.debug(
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "{{ service.meta.address.proto }}",
"rpcName": str(client_call_details.method),
"request": grpc_request,
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2275): logging `metadata` seems repetitive and may need to be cleaned up. We're including it within "request" for consistency with REST transport.' #}
"metadata": grpc_request["metadata"],
},
)
{{ shared_macros.unary_request_interceptor_common(service) }}
response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.cloud.asset.v1.AssetService",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove this? Given that we're just refactoring macros, I'd expect the generated code to stay the same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 107fea4

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.iam.credentials.v1.IAMCredentials",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.cloud.eventarc.v1.Eventarc",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.logging.v2.ConfigServiceV2",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.logging.v2.LoggingServiceV2",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.logging.v2.MetricsServiceV2",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.logging.v2.ConfigServiceV2",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.logging.v2.LoggingServiceV2",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.logging.v2.MetricsServiceV2",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.cloud.redis.v1.CloudRedis",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
f"Sending request for {client_call_details.method}",
extra = {
"serviceName": "google.cloud.redis.v1.CloudRedis",
"rpcName": client_call_details.method,
"rpcName": str(client_call_details.method),
"request": grpc_request,
"metadata": grpc_request["metadata"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async def intercept_unary_unary(self, continuation, client_call_details, request
"metadata": grpc_request["metadata"],
},
)

response = await continuation(client_call_details, request)
if logging_enabled: # pragma: NO COVER
response_metadata = await response.trailing_metadata()
Expand Down
Loading