Skip to content

Commit 0260c24

Browse files
committed
PYCBC-1767: Use nullcontext when using no-op observability
Changes ------- * Add `create()` static method to `ObservableRequestHandler` that will return the observability context manager if observability is enabled, otherwise returns `nullcontext()`. The changes allows the `if instruments.is_noop:` branch in KV operations to be cleaned up to a single context manager call. * Add `_NOOP_OBS_HANDLER_CTX` module-level singleton to use instead of needing to create a `nullcontext()` on every no-op path call. Change-Id: I35643f6beef81b8578ffd7b1b1d6d16899282345 Reviewed-on: https://review.couchbase.org/c/couchbase-python-client/+/245023 Reviewed-by: Dimitris Christodoulou <dimitris.christodoulou@couchbase.com> Tested-by: Build Bot <build@couchbase.com>
1 parent 0d86ab2 commit 0260c24

5 files changed

Lines changed: 71 additions & 257 deletions

File tree

acouchbase/binary_collection.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ async def increment(self,
8383
8484
"""
8585
instruments = self._impl.observability_instruments
86-
if instruments.is_noop:
87-
req = self._impl.request_builder.build_increment_request(key, None, *opts, **kwargs)
88-
return await self._impl.increment(req, None)
89-
async with ObservableRequestHandler(KeyValueOperationType.Increment, instruments) as obs_handler:
86+
async with ObservableRequestHandler.create(KeyValueOperationType.Increment, instruments) as obs_handler:
9087
req = self._impl.request_builder.build_increment_request(key, obs_handler, *opts, **kwargs)
9188
return await self._impl.increment(req, obs_handler)
9289

@@ -136,10 +133,7 @@ async def decrement(self,
136133
137134
"""
138135
instruments = self._impl.observability_instruments
139-
if instruments.is_noop:
140-
req = self._impl.request_builder.build_decrement_request(key, None, *opts, **kwargs)
141-
return await self._impl.decrement(req, None)
142-
async with ObservableRequestHandler(KeyValueOperationType.Decrement, instruments) as obs_handler:
136+
async with ObservableRequestHandler.create(KeyValueOperationType.Decrement, instruments) as obs_handler:
143137
req = self._impl.request_builder.build_decrement_request(key, obs_handler, *opts, **kwargs)
144138
return await self._impl.decrement(req, obs_handler)
145139

@@ -197,10 +191,7 @@ async def append(self,
197191
198192
"""
199193
instruments = self._impl.observability_instruments
200-
if instruments.is_noop:
201-
req = self._impl.request_builder.build_append_request(key, value, None, *opts, **kwargs)
202-
return await self._impl.append(req, None)
203-
async with ObservableRequestHandler(KeyValueOperationType.Append, instruments) as obs_handler:
194+
async with ObservableRequestHandler.create(KeyValueOperationType.Append, instruments) as obs_handler:
204195
req = self._impl.request_builder.build_append_request(key, value, obs_handler, *opts, **kwargs)
205196
return await self._impl.append(req, obs_handler)
206197

@@ -258,9 +249,6 @@ async def prepend(self,
258249
259250
"""
260251
instruments = self._impl.observability_instruments
261-
if instruments.is_noop:
262-
req = self._impl.request_builder.build_prepend_request(key, value, None, *opts, **kwargs)
263-
return await self._impl.prepend(req, None)
264-
async with ObservableRequestHandler(KeyValueOperationType.Prepend, instruments) as obs_handler:
252+
async with ObservableRequestHandler.create(KeyValueOperationType.Prepend, instruments) as obs_handler:
265253
req = self._impl.request_builder.build_prepend_request(key, value, obs_handler, *opts, **kwargs)
266254
return await self._impl.prepend(req, obs_handler)

acouchbase/collection.py

Lines changed: 20 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ async def get(self,
120120
121121
"""
122122
instruments = self._impl.observability_instruments
123-
if instruments.is_noop:
124-
req, transcoder = self._impl.request_builder.build_get_request(key, None, *opts, **kwargs)
125-
return await self._impl.get(req, transcoder, None)
126-
async with ObservableRequestHandler(KeyValueOperationType.Get, instruments) as obs_handler:
123+
async with ObservableRequestHandler.create(KeyValueOperationType.Get, instruments) as obs_handler:
127124
req, transcoder = self._impl.request_builder.build_get_request(key, obs_handler, *opts, **kwargs)
128125
return await self._impl.get(req, transcoder, obs_handler)
129126

@@ -174,10 +171,7 @@ async def get_any_replica(self,
174171
175172
""" # noqa: E501
176173
instruments = self._impl.observability_instruments
177-
if instruments.is_noop:
178-
req, transcoder = self._impl.request_builder.build_get_any_replica_request(key, None, *opts, **kwargs)
179-
return await self._impl.get_any_replica(req, transcoder, None)
180-
async with ObservableRequestHandler(KeyValueOperationType.GetAnyReplica, instruments) as obs_handler:
174+
async with ObservableRequestHandler.create(KeyValueOperationType.GetAnyReplica, instruments) as obs_handler:
181175
req, transcoder = self._impl.request_builder.build_get_any_replica_request(
182176
key, obs_handler, *opts, **kwargs)
183177
return await self._impl.get_any_replica(req, transcoder, obs_handler)
@@ -249,10 +243,7 @@ async def get_all_replicas(self,
249243
250244
"""
251245
instruments = self._impl.observability_instruments
252-
if instruments.is_noop:
253-
req, transcoder = self._impl.request_builder.build_get_all_replicas_request(key, None, *opts, **kwargs)
254-
return await self._impl.get_all_replicas(req, transcoder, None)
255-
async with ObservableRequestHandler(KeyValueOperationType.GetAllReplicas, instruments) as obs_handler:
246+
async with ObservableRequestHandler.create(KeyValueOperationType.GetAllReplicas, instruments) as obs_handler:
256247
req, transcoder = self._impl.request_builder.build_get_all_replicas_request(
257248
key, obs_handler, *opts, **kwargs)
258249
return await self._impl.get_all_replicas(req, transcoder, obs_handler)
@@ -299,10 +290,7 @@ async def exists(self,
299290
300291
"""
301292
instruments = self._impl.observability_instruments
302-
if instruments.is_noop:
303-
req = self._impl.request_builder.build_exists_request(key, None, *opts, **kwargs)
304-
return await self._impl.exists(req, None)
305-
async with ObservableRequestHandler(KeyValueOperationType.Exists, instruments) as obs_handler:
293+
async with ObservableRequestHandler.create(KeyValueOperationType.Exists, instruments) as obs_handler:
306294
req = self._impl.request_builder.build_exists_request(key, obs_handler, *opts, **kwargs)
307295
return await self._impl.exists(req, obs_handler)
308296

@@ -369,10 +357,7 @@ async def insert(self,
369357
370358
"""
371359
instruments = self._impl.observability_instruments
372-
if instruments.is_noop:
373-
req = self._impl.request_builder.build_insert_request(key, value, None, *opts, **kwargs)
374-
return await self._impl.insert(req, None)
375-
async with ObservableRequestHandler(KeyValueOperationType.Insert, instruments) as obs_handler:
360+
async with ObservableRequestHandler.create(KeyValueOperationType.Insert, instruments) as obs_handler:
376361
req = self._impl.request_builder.build_insert_request(key, value, obs_handler, *opts, **kwargs)
377362
return await self._impl.insert(req, obs_handler)
378363

@@ -435,10 +420,7 @@ async def upsert(self,
435420
436421
"""
437422
instruments = self._impl.observability_instruments
438-
if instruments.is_noop:
439-
req = self._impl.request_builder.build_upsert_request(key, value, None, *opts, **kwargs)
440-
return await self._impl.upsert(req, None)
441-
async with ObservableRequestHandler(KeyValueOperationType.Upsert, instruments) as obs_handler:
423+
async with ObservableRequestHandler.create(KeyValueOperationType.Upsert, instruments) as obs_handler:
442424
req = self._impl.request_builder.build_upsert_request(key, value, obs_handler, *opts, **kwargs)
443425
return await self._impl.upsert(req, obs_handler)
444426

@@ -495,10 +477,7 @@ async def replace(self,
495477
496478
"""
497479
instruments = self._impl.observability_instruments
498-
if instruments.is_noop:
499-
req = self._impl.request_builder.build_replace_request(key, value, None, *opts, **kwargs)
500-
return await self._impl.replace(req, None)
501-
async with ObservableRequestHandler(KeyValueOperationType.Replace, instruments) as obs_handler:
480+
async with ObservableRequestHandler.create(KeyValueOperationType.Replace, instruments) as obs_handler:
502481
req = self._impl.request_builder.build_replace_request(key, value, obs_handler, *opts, **kwargs)
503482
return await self._impl.replace(req, obs_handler)
504483

@@ -545,10 +524,7 @@ async def remove(self,
545524
546525
"""
547526
instruments = self._impl.observability_instruments
548-
if instruments.is_noop:
549-
req = self._impl.request_builder.build_remove_request(key, None, *opts, **kwargs)
550-
return await self._impl.remove(req, None)
551-
async with ObservableRequestHandler(KeyValueOperationType.Remove, instruments) as obs_handler:
527+
async with ObservableRequestHandler.create(KeyValueOperationType.Remove, instruments) as obs_handler:
552528
req = self._impl.request_builder.build_remove_request(key, obs_handler, *opts, **kwargs)
553529
return await self._impl.remove(req, obs_handler)
554530

@@ -603,10 +579,7 @@ async def touch(self,
603579
604580
"""
605581
instruments = self._impl.observability_instruments
606-
if instruments.is_noop:
607-
req = self._impl.request_builder.build_touch_request(key, expiry, None, *opts, **kwargs)
608-
return await self._impl.touch(req, None)
609-
async with ObservableRequestHandler(KeyValueOperationType.Touch, instruments) as obs_handler:
582+
async with ObservableRequestHandler.create(KeyValueOperationType.Touch, instruments) as obs_handler:
610583
req = self._impl.request_builder.build_touch_request(key, expiry, obs_handler, *opts, **kwargs)
611584
return await self._impl.touch(req, obs_handler)
612585

@@ -664,10 +637,7 @@ async def get_and_touch(self,
664637
665638
"""
666639
instruments = self._impl.observability_instruments
667-
if instruments.is_noop:
668-
req, transcoder = self._impl.request_builder.build_get_and_touch_request(key, expiry, None, *opts, **kwargs)
669-
return await self._impl.get_and_touch(req, transcoder, None)
670-
async with ObservableRequestHandler(KeyValueOperationType.GetAndTouch, instruments) as obs_handler:
640+
async with ObservableRequestHandler.create(KeyValueOperationType.GetAndTouch, instruments) as obs_handler:
671641
req, transcoder = self._impl.request_builder.build_get_and_touch_request(
672642
key, expiry, obs_handler, *opts, **kwargs)
673643
return await self._impl.get_and_touch(req, transcoder, obs_handler)
@@ -726,11 +696,7 @@ async def get_and_lock(self,
726696
727697
"""
728698
instruments = self._impl.observability_instruments
729-
if instruments.is_noop:
730-
req, transcoder = self._impl.request_builder.build_get_and_lock_request(
731-
key, lock_time, None, *opts, **kwargs)
732-
return await self._impl.get_and_lock(req, transcoder, None)
733-
async with ObservableRequestHandler(KeyValueOperationType.GetAndLock, instruments) as obs_handler:
699+
async with ObservableRequestHandler.create(KeyValueOperationType.GetAndLock, instruments) as obs_handler:
734700
req, transcoder = self._impl.request_builder.build_get_and_lock_request(
735701
key, lock_time, obs_handler, *opts, **kwargs)
736702
return await self._impl.get_and_lock(req, transcoder, obs_handler)
@@ -778,10 +744,7 @@ async def unlock(self,
778744
779745
"""
780746
instruments = self._impl.observability_instruments
781-
if instruments.is_noop:
782-
req = self._impl.request_builder.build_unlock_request(key, cas, None, *opts, **kwargs)
783-
return await self._impl.unlock(req, None)
784-
async with ObservableRequestHandler(KeyValueOperationType.Unlock, instruments) as obs_handler:
747+
async with ObservableRequestHandler.create(KeyValueOperationType.Unlock, instruments) as obs_handler:
785748
req = self._impl.request_builder.build_unlock_request(key, cas, obs_handler, *opts, **kwargs)
786749
await self._impl.unlock(req, obs_handler)
787750

@@ -843,10 +806,7 @@ async def lookup_in(self,
843806
844807
"""
845808
instruments = self._impl.observability_instruments
846-
if instruments.is_noop:
847-
req, transcoder = self._impl.request_builder.build_lookup_in_request(key, spec, None, *opts, **kwargs)
848-
return await self._impl.lookup_in(req, transcoder, None)
849-
async with ObservableRequestHandler(KeyValueOperationType.LookupIn, instruments) as obs_handler:
809+
async with ObservableRequestHandler.create(KeyValueOperationType.LookupIn, instruments) as obs_handler:
850810
req, transcoder = self._impl.request_builder.build_lookup_in_request(
851811
key, spec, obs_handler, *opts, **kwargs)
852812
return await self._impl.lookup_in(req, transcoder, obs_handler)
@@ -911,11 +871,9 @@ async def lookup_in_any_replica(self,
911871
912872
"""
913873
instruments = self._impl.observability_instruments
914-
if instruments.is_noop:
915-
req, transcoder = self._impl.request_builder.build_lookup_in_any_replica_request(
916-
key, spec, None, *opts, **kwargs)
917-
return await self._impl.lookup_in_any_replica(req, transcoder, None)
918-
async with ObservableRequestHandler(KeyValueOperationType.LookupInAnyReplica, instruments) as obs_handler:
874+
async with ObservableRequestHandler.create(
875+
KeyValueOperationType.LookupInAnyReplica, instruments
876+
) as obs_handler:
919877
req, transcoder = self._impl.request_builder.build_lookup_in_any_replica_request(
920878
key, spec, obs_handler, *opts, **kwargs)
921879
return await self._impl.lookup_in_any_replica(req, transcoder, obs_handler)
@@ -1003,11 +961,9 @@ async def lookup_in_all_replicas(self,
1003961
1004962
""" # noqa: E501
1005963
instruments = self._impl.observability_instruments
1006-
if instruments.is_noop:
1007-
req, transcoder = self._impl.request_builder.build_lookup_in_all_replicas_request(
1008-
key, spec, None, *opts, **kwargs)
1009-
return await self._impl.lookup_in_all_replicas(req, transcoder, None)
1010-
async with ObservableRequestHandler(KeyValueOperationType.LookupInAllReplicas, instruments) as obs_handler:
964+
async with ObservableRequestHandler.create(
965+
KeyValueOperationType.LookupInAllReplicas, instruments
966+
) as obs_handler:
1011967
req, transcoder = self._impl.request_builder.build_lookup_in_all_replicas_request(
1012968
key, spec, obs_handler, *opts, **kwargs)
1013969
return await self._impl.lookup_in_all_replicas(req, transcoder, obs_handler)
@@ -1068,10 +1024,7 @@ async def mutate_in(self,
10681024
10691025
"""
10701026
instruments = self._impl.observability_instruments
1071-
if instruments.is_noop:
1072-
req = self._impl.request_builder.build_mutate_in_request(key, spec, None, *opts, **kwargs)
1073-
return await self._impl.mutate_in(req, None)
1074-
async with ObservableRequestHandler(KeyValueOperationType.MutateIn, instruments) as obs_handler:
1027+
async with ObservableRequestHandler.create(KeyValueOperationType.MutateIn, instruments) as obs_handler:
10751028
req = self._impl.request_builder.build_mutate_in_request(key, spec, obs_handler, *opts, **kwargs)
10761029
return await self._impl.mutate_in(req, obs_handler)
10771030

0 commit comments

Comments
 (0)