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

feat: [Many APIs] Add REST Interceptors which support reading metadata #13502

Merged
merged 6 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.15.0" # {x-release-please-version}
__version__ = "0.0.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.15.0" # {x-release-please-version}
__version__ = "0.0.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.
#
from collections import OrderedDict
from http import HTTPStatus
import json
import logging as std_logging
import os
import re
Expand Down Expand Up @@ -466,6 +468,33 @@ def _validate_universe_domain(self):
# NOTE (b/349488459): universe validation is disabled until further notice.
return True

def _add_cred_info_for_auth_errors(
self, error: core_exceptions.GoogleAPICallError
) -> None:
"""Adds credential info string to error details for 401/403/404 errors.

Args:
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
"""
if error.code not in [
HTTPStatus.UNAUTHORIZED,
HTTPStatus.FORBIDDEN,
HTTPStatus.NOT_FOUND,
]:
return

cred = self._transport._credentials

# get_cred_info is only available in google-auth>=2.35.0
if not hasattr(cred, "get_cred_info"):
return

# ignore the type check since pypy test fails when get_cred_info
# is not available
cred_info = cred.get_cred_info() # type: ignore
if cred_info and hasattr(error._details, "append"):
error._details.append(json.dumps(cred_info))

@property
def api_endpoint(self):
"""Return the API endpoint used by the client instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,33 @@ def pre_get_trace(
def post_get_trace(self, response: trace.Trace) -> trace.Trace:
"""Post-rpc interceptor for get_trace

Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_get_trace_with_metadata`
interceptor instead.

Override in a subclass to read or manipulate the response
after it is returned by the TraceService server but before
it is returned to user code.
it is returned to user code. This `post_get_trace` interceptor runs
before the `post_get_trace_with_metadata` interceptor.
"""
return response

def post_get_trace_with_metadata(
self, response: trace.Trace, metadata: Sequence[Tuple[str, Union[str, bytes]]]
) -> Tuple[trace.Trace, Sequence[Tuple[str, Union[str, bytes]]]]:
"""Post-rpc interceptor for get_trace

Override in a subclass to read or manipulate the response or metadata after it
is returned by the TraceService server but before it is returned to user code.

We recommend only using this `post_get_trace_with_metadata`
interceptor in new development instead of the `post_get_trace` interceptor.
When both interceptors are used, this `post_get_trace_with_metadata` interceptor runs after the
`post_get_trace` interceptor. The (possibly modified) response returned by
`post_get_trace` will be passed to
`post_get_trace_with_metadata`.
"""
return response, metadata

def pre_list_traces(
self,
request: trace.ListTracesRequest,
Expand All @@ -133,12 +154,35 @@ def post_list_traces(
) -> trace.ListTracesResponse:
"""Post-rpc interceptor for list_traces

Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_list_traces_with_metadata`
interceptor instead.

Override in a subclass to read or manipulate the response
after it is returned by the TraceService server but before
it is returned to user code.
it is returned to user code. This `post_list_traces` interceptor runs
before the `post_list_traces_with_metadata` interceptor.
"""
return response

def post_list_traces_with_metadata(
self,
response: trace.ListTracesResponse,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[trace.ListTracesResponse, Sequence[Tuple[str, Union[str, bytes]]]]:
"""Post-rpc interceptor for list_traces

Override in a subclass to read or manipulate the response or metadata after it
is returned by the TraceService server but before it is returned to user code.

We recommend only using this `post_list_traces_with_metadata`
interceptor in new development instead of the `post_list_traces` interceptor.
When both interceptors are used, this `post_list_traces_with_metadata` interceptor runs after the
`post_list_traces` interceptor. The (possibly modified) response returned by
`post_list_traces` will be passed to
`post_list_traces_with_metadata`.
"""
return response, metadata

def pre_patch_traces(
self,
request: trace.PatchTracesRequest,
Expand Down Expand Up @@ -367,6 +411,10 @@ def __call__(
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)

resp = self._interceptor.post_get_trace(resp)
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
resp, _ = self._interceptor.post_get_trace_with_metadata(
resp, response_metadata
)
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
logging.DEBUG
): # pragma: NO COVER
Expand Down Expand Up @@ -512,6 +560,10 @@ def __call__(
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)

resp = self._interceptor.post_list_traces(resp)
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
resp, _ = self._interceptor.post_list_traces_with_metadata(
resp, response_metadata
)
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
logging.DEBUG
): # pragma: NO COVER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "1.15.0" # {x-release-please-version}
__version__ = "0.0.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.
#
from collections import OrderedDict
from http import HTTPStatus
import json
import logging as std_logging
import os
import re
Expand Down Expand Up @@ -493,6 +495,33 @@ def _validate_universe_domain(self):
# NOTE (b/349488459): universe validation is disabled until further notice.
return True

def _add_cred_info_for_auth_errors(
self, error: core_exceptions.GoogleAPICallError
) -> None:
"""Adds credential info string to error details for 401/403/404 errors.

Args:
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
"""
if error.code not in [
HTTPStatus.UNAUTHORIZED,
HTTPStatus.FORBIDDEN,
HTTPStatus.NOT_FOUND,
]:
return

cred = self._transport._credentials

# get_cred_info is only available in google-auth>=2.35.0
if not hasattr(cred, "get_cred_info"):
return

# ignore the type check since pypy test fails when get_cred_info
# is not available
cred_info = cred.get_cred_info() # type: ignore
if cred_info and hasattr(error._details, "append"):
error._details.append(json.dumps(cred_info))

@property
def api_endpoint(self):
"""Return the API endpoint used by the client instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,33 @@ def pre_create_span(
def post_create_span(self, response: trace.Span) -> trace.Span:
"""Post-rpc interceptor for create_span

Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_create_span_with_metadata`
interceptor instead.

Override in a subclass to read or manipulate the response
after it is returned by the TraceService server but before
it is returned to user code.
it is returned to user code. This `post_create_span` interceptor runs
before the `post_create_span_with_metadata` interceptor.
"""
return response

def post_create_span_with_metadata(
self, response: trace.Span, metadata: Sequence[Tuple[str, Union[str, bytes]]]
) -> Tuple[trace.Span, Sequence[Tuple[str, Union[str, bytes]]]]:
"""Post-rpc interceptor for create_span

Override in a subclass to read or manipulate the response or metadata after it
is returned by the TraceService server but before it is returned to user code.

We recommend only using this `post_create_span_with_metadata`
interceptor in new development instead of the `post_create_span` interceptor.
When both interceptors are used, this `post_create_span_with_metadata` interceptor runs after the
`post_create_span` interceptor. The (possibly modified) response returned by
`post_create_span` will be passed to
`post_create_span_with_metadata`.
"""
return response, metadata


@dataclasses.dataclass
class TraceServiceRestStub:
Expand Down Expand Up @@ -476,6 +497,10 @@ def __call__(
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)

resp = self._interceptor.post_create_span(resp)
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
resp, _ = self._interceptor.post_create_span_with_metadata(
resp, response_metadata
)
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
logging.DEBUG
): # pragma: NO COVER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"language": "PYTHON",
"name": "google-cloud-trace",
"version": "1.15.0"
"version": "0.1.0"
},
"snippets": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"language": "PYTHON",
"name": "google-cloud-trace",
"version": "1.15.0"
"version": "0.1.0"
},
"snippets": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
)
from google.cloud.trace_v1.types import trace

CRED_INFO_JSON = {
"credential_source": "/path/to/file",
"credential_type": "service account credentials",
"principal": "service-account@example.com",
}
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)


async def mock_async_gen(data, chunk_size=1):
for i in range(0, len(data)): # pragma: NO COVER
Expand Down Expand Up @@ -304,6 +311,49 @@ def test__get_universe_domain():
assert str(excinfo.value) == "Universe Domain cannot be an empty string."


@pytest.mark.parametrize(
"error_code,cred_info_json,show_cred_info",
[
(401, CRED_INFO_JSON, True),
(403, CRED_INFO_JSON, True),
(404, CRED_INFO_JSON, True),
(500, CRED_INFO_JSON, False),
(401, None, False),
(403, None, False),
(404, None, False),
(500, None, False),
],
)
def test__add_cred_info_for_auth_errors(error_code, cred_info_json, show_cred_info):
cred = mock.Mock(["get_cred_info"])
cred.get_cred_info = mock.Mock(return_value=cred_info_json)
client = TraceServiceClient(credentials=cred)
client._transport._credentials = cred

error = core_exceptions.GoogleAPICallError("message", details=["foo"])
error.code = error_code

client._add_cred_info_for_auth_errors(error)
if show_cred_info:
assert error.details == ["foo", CRED_INFO_STRING]
else:
assert error.details == ["foo"]


@pytest.mark.parametrize("error_code", [401, 403, 404, 500])
def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
cred = mock.Mock([])
assert not hasattr(cred, "get_cred_info")
client = TraceServiceClient(credentials=cred)
client._transport._credentials = cred

error = core_exceptions.GoogleAPICallError("message", details=[])
error.code = error_code

client._add_cred_info_for_auth_errors(error)
assert error.details == []


@pytest.mark.parametrize(
"client_class,transport_name",
[
Expand Down Expand Up @@ -3198,10 +3248,13 @@ def test_list_traces_rest_interceptors(null_interceptor):
) as transcode, mock.patch.object(
transports.TraceServiceRestInterceptor, "post_list_traces"
) as post, mock.patch.object(
transports.TraceServiceRestInterceptor, "post_list_traces_with_metadata"
) as post_with_metadata, mock.patch.object(
transports.TraceServiceRestInterceptor, "pre_list_traces"
) as pre:
pre.assert_not_called()
post.assert_not_called()
post_with_metadata.assert_not_called()
pb_message = trace.ListTracesRequest.pb(trace.ListTracesRequest())
transcode.return_value = {
"method": "post",
Expand All @@ -3223,6 +3276,7 @@ def test_list_traces_rest_interceptors(null_interceptor):
]
pre.return_value = request, metadata
post.return_value = trace.ListTracesResponse()
post_with_metadata.return_value = trace.ListTracesResponse(), metadata

client.list_traces(
request,
Expand All @@ -3234,6 +3288,7 @@ def test_list_traces_rest_interceptors(null_interceptor):

pre.assert_called_once()
post.assert_called_once()
post_with_metadata.assert_called_once()


def test_get_trace_rest_bad_request(request_type=trace.GetTraceRequest):
Expand Down Expand Up @@ -3318,10 +3373,13 @@ def test_get_trace_rest_interceptors(null_interceptor):
) as transcode, mock.patch.object(
transports.TraceServiceRestInterceptor, "post_get_trace"
) as post, mock.patch.object(
transports.TraceServiceRestInterceptor, "post_get_trace_with_metadata"
) as post_with_metadata, mock.patch.object(
transports.TraceServiceRestInterceptor, "pre_get_trace"
) as pre:
pre.assert_not_called()
post.assert_not_called()
post_with_metadata.assert_not_called()
pb_message = trace.GetTraceRequest.pb(trace.GetTraceRequest())
transcode.return_value = {
"method": "post",
Expand All @@ -3343,6 +3401,7 @@ def test_get_trace_rest_interceptors(null_interceptor):
]
pre.return_value = request, metadata
post.return_value = trace.Trace()
post_with_metadata.return_value = trace.Trace(), metadata

client.get_trace(
request,
Expand All @@ -3354,6 +3413,7 @@ def test_get_trace_rest_interceptors(null_interceptor):

pre.assert_called_once()
post.assert_called_once()
post_with_metadata.assert_called_once()


def test_patch_traces_rest_bad_request(request_type=trace.PatchTracesRequest):
Expand Down
Loading
Loading