Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 2bf8ab7

Browse filesBrowse files
feat: add context manager support in client (#141)
- [ ] 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: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 154ac00 commit 2bf8ab7
Copy full SHA for 2bf8ab7

15 files changed

+184-8Lines changed: 184 additions & 8 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎google/cloud/trace_v1/services/trace_service/async_client.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v1/services/trace_service/async_client.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ async def patch_traces(
414414
request, retry=retry, timeout=timeout, metadata=metadata,
415415
)
416416

417+
async def __aenter__(self):
418+
return self
419+
420+
async def __aexit__(self, exc_type, exc, tb):
421+
await self.transport.close()
422+
417423

418424
try:
419425
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Collapse file

‎google/cloud/trace_v1/services/trace_service/client.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v1/services/trace_service/client.py
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ def __init__(
332332
client_cert_source_for_mtls=client_cert_source_func,
333333
quota_project_id=client_options.quota_project_id,
334334
client_info=client_info,
335-
always_use_jwt_access=(
336-
Transport == type(self).get_transport_class("grpc")
337-
or Transport == type(self).get_transport_class("grpc_asyncio")
338-
),
335+
always_use_jwt_access=True,
339336
)
340337

341338
def list_traces(
@@ -561,6 +558,19 @@ def patch_traces(
561558
request, retry=retry, timeout=timeout, metadata=metadata,
562559
)
563560

561+
def __enter__(self):
562+
return self
563+
564+
def __exit__(self, type, value, traceback):
565+
"""Releases underlying transport's resources.
566+
567+
.. warning::
568+
ONLY use as a context manager if the transport is NOT shared
569+
with other clients! Exiting the with block will CLOSE the transport
570+
and may cause errors in other clients!
571+
"""
572+
self.transport.close()
573+
564574

565575
try:
566576
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Collapse file

‎google/cloud/trace_v1/services/trace_service/transports/base.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v1/services/trace_service/transports/base.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ def _prep_wrapped_messages(self, client_info):
204204
),
205205
}
206206

207+
def close(self):
208+
"""Closes resources associated with the transport.
209+
210+
.. warning::
211+
Only call this method if the transport is NOT shared
212+
with other clients - this may cause errors in other clients!
213+
"""
214+
raise NotImplementedError()
215+
207216
@property
208217
def list_traces(
209218
self,
Collapse file

‎google/cloud/trace_v1/services/trace_service/transports/grpc.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v1/services/trace_service/transports/grpc.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,5 +312,8 @@ def patch_traces(self) -> Callable[[trace.PatchTracesRequest], empty_pb2.Empty]:
312312
)
313313
return self._stubs["patch_traces"]
314314

315+
def close(self):
316+
self.grpc_channel.close()
317+
315318

316319
__all__ = ("TraceServiceGrpcTransport",)
Collapse file

‎google/cloud/trace_v1/services/trace_service/transports/grpc_asyncio.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v1/services/trace_service/transports/grpc_asyncio.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,8 @@ def patch_traces(
317317
)
318318
return self._stubs["patch_traces"]
319319

320+
def close(self):
321+
return self.grpc_channel.close()
322+
320323

321324
__all__ = ("TraceServiceGrpcAsyncIOTransport",)
Collapse file

‎google/cloud/trace_v1/types/trace.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v1/types/trace.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Trace(proto.Message):
5656

5757
class Traces(proto.Message):
5858
r"""List of new or updated traces.
59+
5960
Attributes:
6061
traces (Sequence[google.cloud.trace_v1.types.Trace]):
6162
List of traces.
@@ -254,6 +255,7 @@ class ViewType(proto.Enum):
254255

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

276278
class GetTraceRequest(proto.Message):
277279
r"""The request message for the ``GetTrace`` method.
280+
278281
Attributes:
279282
project_id (str):
280283
Required. ID of the Cloud project where the
@@ -289,6 +292,7 @@ class GetTraceRequest(proto.Message):
289292

290293
class PatchTracesRequest(proto.Message):
291294
r"""The request message for the ``PatchTraces`` method.
295+
292296
Attributes:
293297
project_id (str):
294298
Required. ID of the Cloud project where the
Collapse file

‎google/cloud/trace_v2/services/trace_service/async_client.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v2/services/trace_service/async_client.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ async def create_span(
315315
# Done; return the response.
316316
return response
317317

318+
async def __aenter__(self):
319+
return self
320+
321+
async def __aexit__(self, exc_type, exc, tb):
322+
await self.transport.close()
323+
318324

319325
try:
320326
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Collapse file

‎google/cloud/trace_v2/services/trace_service/client.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v2/services/trace_service/client.py
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,7 @@ def __init__(
351351
client_cert_source_for_mtls=client_cert_source_func,
352352
quota_project_id=client_options.quota_project_id,
353353
client_info=client_info,
354-
always_use_jwt_access=(
355-
Transport == type(self).get_transport_class("grpc")
356-
or Transport == type(self).get_transport_class("grpc_asyncio")
357-
),
354+
always_use_jwt_access=True,
358355
)
359356

360357
def batch_write_spans(
@@ -497,6 +494,19 @@ def create_span(
497494
# Done; return the response.
498495
return response
499496

497+
def __enter__(self):
498+
return self
499+
500+
def __exit__(self, type, value, traceback):
501+
"""Releases underlying transport's resources.
502+
503+
.. warning::
504+
ONLY use as a context manager if the transport is NOT shared
505+
with other clients! Exiting the with block will CLOSE the transport
506+
and may cause errors in other clients!
507+
"""
508+
self.transport.close()
509+
500510

501511
try:
502512
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Collapse file

‎google/cloud/trace_v2/services/trace_service/transports/base.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v2/services/trace_service/transports/base.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ def _prep_wrapped_messages(self, client_info):
177177
),
178178
}
179179

180+
def close(self):
181+
"""Closes resources associated with the transport.
182+
183+
.. warning::
184+
Only call this method if the transport is NOT shared
185+
with other clients - this may cause errors in other clients!
186+
"""
187+
raise NotImplementedError()
188+
180189
@property
181190
def batch_write_spans(
182191
self,
Collapse file

‎google/cloud/trace_v2/services/trace_service/transports/grpc.py‎

Copy file name to clipboardExpand all lines: google/cloud/trace_v2/services/trace_service/transports/grpc.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,8 @@ def create_span(self) -> Callable[[trace.Span], trace.Span]:
283283
)
284284
return self._stubs["create_span"]
285285

286+
def close(self):
287+
self.grpc_channel.close()
288+
286289

287290
__all__ = ("TraceServiceGrpcTransport",)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.