Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
feat: add context manager support in client (#141)
Browse files Browse the repository at this point in the history
- [ ] Regenerate this pull request now.

chore: fix docstring for first attribute of protos

committer: @busunkim96
PiperOrigin-RevId: 401271153

Source-Link: googleapis/googleapis@787f8c9

Source-Link: googleapis/googleapis-gen@81decff
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
  • Loading branch information
gcf-owl-bot[bot] committed Oct 7, 2021
1 parent 154ac00 commit 2bf8ab7
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 8 deletions.
6 changes: 6 additions & 0 deletions google/cloud/trace_v1/services/trace_service/async_client.py
Expand Up @@ -414,6 +414,12 @@ async def patch_traces(
request, retry=retry, timeout=timeout, metadata=metadata,
)

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/trace_v1/services/trace_service/client.py
Expand Up @@ -332,10 +332,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def list_traces(
Expand Down Expand Up @@ -561,6 +558,19 @@ def patch_traces(
request, retry=retry, timeout=timeout, metadata=metadata,
)

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.
.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Expand Up @@ -204,6 +204,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def list_traces(
self,
Expand Down
Expand Up @@ -312,5 +312,8 @@ def patch_traces(self) -> Callable[[trace.PatchTracesRequest], empty_pb2.Empty]:
)
return self._stubs["patch_traces"]

def close(self):
self.grpc_channel.close()


__all__ = ("TraceServiceGrpcTransport",)
Expand Up @@ -317,5 +317,8 @@ def patch_traces(
)
return self._stubs["patch_traces"]

def close(self):
return self.grpc_channel.close()


__all__ = ("TraceServiceGrpcAsyncIOTransport",)
4 changes: 4 additions & 0 deletions google/cloud/trace_v1/types/trace.py
Expand Up @@ -56,6 +56,7 @@ class Trace(proto.Message):

class Traces(proto.Message):
r"""List of new or updated traces.
Attributes:
traces (Sequence[google.cloud.trace_v1.types.Trace]):
List of traces.
Expand Down Expand Up @@ -254,6 +255,7 @@ class ViewType(proto.Enum):

class ListTracesResponse(proto.Message):
r"""The response message for the ``ListTraces`` method.
Attributes:
traces (Sequence[google.cloud.trace_v1.types.Trace]):
List of trace records as specified by the
Expand All @@ -275,6 +277,7 @@ def raw_page(self):

class GetTraceRequest(proto.Message):
r"""The request message for the ``GetTrace`` method.
Attributes:
project_id (str):
Required. ID of the Cloud project where the
Expand All @@ -289,6 +292,7 @@ class GetTraceRequest(proto.Message):

class PatchTracesRequest(proto.Message):
r"""The request message for the ``PatchTraces`` method.
Attributes:
project_id (str):
Required. ID of the Cloud project where the
Expand Down
6 changes: 6 additions & 0 deletions google/cloud/trace_v2/services/trace_service/async_client.py
Expand Up @@ -315,6 +315,12 @@ async def create_span(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/trace_v2/services/trace_service/client.py
Expand Up @@ -351,10 +351,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def batch_write_spans(
Expand Down Expand Up @@ -497,6 +494,19 @@ def create_span(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.
.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Expand Up @@ -177,6 +177,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def batch_write_spans(
self,
Expand Down
Expand Up @@ -283,5 +283,8 @@ def create_span(self) -> Callable[[trace.Span], trace.Span]:
)
return self._stubs["create_span"]

def close(self):
self.grpc_channel.close()


__all__ = ("TraceServiceGrpcTransport",)
Expand Up @@ -286,5 +286,8 @@ def create_span(self) -> Callable[[trace.Span], Awaitable[trace.Span]]:
)
return self._stubs["create_span"]

def close(self):
return self.grpc_channel.close()


__all__ = ("TraceServiceGrpcAsyncIOTransport",)
9 changes: 9 additions & 0 deletions google/cloud/trace_v2/types/trace.py
Expand Up @@ -125,6 +125,7 @@ class SpanKind(proto.Enum):

class Attributes(proto.Message):
r"""A set of attributes, each in the format ``[KEY]:[VALUE]``.
Attributes:
attribute_map (Sequence[google.cloud.trace_v2.types.Span.Attributes.AttributeMapEntry]):
The set of attributes. Each attribute's key can be up to 128
Expand Down Expand Up @@ -152,6 +153,7 @@ class Attributes(proto.Message):

class TimeEvent(proto.Message):
r"""A time-stamped annotation or message event in the Span.
Attributes:
time (google.protobuf.timestamp_pb2.Timestamp):
The timestamp indicating the time the event
Expand All @@ -165,6 +167,7 @@ class TimeEvent(proto.Message):

class Annotation(proto.Message):
r"""Text annotation with a set of attributes.
Attributes:
description (google.cloud.trace_v2.types.TruncatableString):
A user-supplied message describing the event.
Expand All @@ -184,6 +187,7 @@ class Annotation(proto.Message):

class MessageEvent(proto.Message):
r"""An event describing a message sent/received between Spans.
Attributes:
type (google.cloud.trace_v2.types.Span.TimeEvent.MessageEvent.Type):
Type of MessageEvent. Indicates whether the
Expand Down Expand Up @@ -321,6 +325,7 @@ class Links(proto.Message):

class AttributeValue(proto.Message):
r"""The allowed types for [VALUE] in a ``[KEY]:[VALUE]`` attribute.
Attributes:
string_value (google.cloud.trace_v2.types.TruncatableString):
A string up to 256 bytes long.
Expand All @@ -339,6 +344,7 @@ class AttributeValue(proto.Message):

class StackTrace(proto.Message):
r"""A call stack appearing in a trace.
Attributes:
stack_frames (google.cloud.trace_v2.types.StackTrace.StackFrames):
Stack frames in this stack trace. A maximum
Expand All @@ -357,6 +363,7 @@ class StackTrace(proto.Message):

class StackFrame(proto.Message):
r"""Represents a single stack frame in a stack trace.
Attributes:
function_name (google.cloud.trace_v2.types.TruncatableString):
The fully-qualified name that uniquely
Expand Down Expand Up @@ -400,6 +407,7 @@ class StackFrame(proto.Message):

class StackFrames(proto.Message):
r"""A collection of stack frames, which can be truncated.
Attributes:
frame (Sequence[google.cloud.trace_v2.types.StackTrace.StackFrame]):
Stack frames in this call stack.
Expand All @@ -421,6 +429,7 @@ class StackFrames(proto.Message):

class Module(proto.Message):
r"""Binary module.
Attributes:
module (google.cloud.trace_v2.types.TruncatableString):
For example: main binary, kernel modules, and
Expand Down
1 change: 1 addition & 0 deletions google/cloud/trace_v2/types/tracing.py
Expand Up @@ -25,6 +25,7 @@

class BatchWriteSpansRequest(proto.Message):
r"""The request message for the ``BatchWriteSpans`` method.
Attributes:
name (str):
Required. The name of the project where the spans belong.
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/gapic/trace_v1/test_trace_service.py
Expand Up @@ -29,6 +29,7 @@
from google.api_core import gapic_v1
from google.api_core import grpc_helpers
from google.api_core import grpc_helpers_async
from google.api_core import path_template
from google.auth import credentials as ga_credentials
from google.auth.exceptions import MutualTLSChannelError
from google.cloud.trace_v1.services.trace_service import TraceServiceAsyncClient
Expand Down Expand Up @@ -1143,6 +1144,9 @@ def test_trace_service_base_transport():
with pytest.raises(NotImplementedError):
getattr(transport, method)(request=object())

with pytest.raises(NotImplementedError):
transport.close()


@requires_google_auth_gte_1_25_0
def test_trace_service_base_transport_with_credentials_file():
Expand Down Expand Up @@ -1618,3 +1622,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
)
prep.assert_called_once_with(client_info)


@pytest.mark.asyncio
async def test_transport_close_async():
client = TraceServiceAsyncClient(
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
)
with mock.patch.object(
type(getattr(client.transport, "grpc_channel")), "close"
) as close:
async with client:
close.assert_not_called()
close.assert_called_once()


def test_transport_close():
transports = {
"grpc": "_grpc_channel",
}

for transport, close_name in transports.items():
client = TraceServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
with mock.patch.object(
type(getattr(client.transport, close_name)), "close"
) as close:
with client:
close.assert_not_called()
close.assert_called_once()


def test_client_ctx():
transports = [
"grpc",
]
for transport in transports:
client = TraceServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
# Test client calls underlying transport.
with mock.patch.object(type(client.transport), "close") as close:
close.assert_not_called()
with client:
pass
close.assert_called()

0 comments on commit 2bf8ab7

Please sign in to comment.