Skip to content

Commit 1305503

Browse files
committed
PYCBC-1762: Observability Code Structure Improvements
Changes ------- * For no-op tracing AND metrics, allow collection logic to skip ObservabilityHandler * Update *Request dataclasses to use cached properties when using req_to_dict() method Change-Id: Ia388e36ceda38c64095d203ff584ba4e809c7488 Reviewed-on: https://review.couchbase.org/c/couchbase-python-client/+/243592 Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Dimitris Christodoulou <dimitris.christodoulou@couchbase.com>
1 parent 7f00efb commit 1305503

18 files changed

Lines changed: 583 additions & 184 deletions

acouchbase/binary_collection.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ async def increment(self,
8282
print(f'Counter value: {res.content}')
8383
8484
"""
85-
op_type = KeyValueOperationType.Increment
86-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
85+
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:
8790
req = self._impl.request_builder.build_increment_request(key, obs_handler, *opts, **kwargs)
8891
return await self._impl.increment(req, obs_handler)
8992

@@ -132,8 +135,11 @@ async def decrement(self,
132135
print(f'Counter value: {res.content}')
133136
134137
"""
135-
op_type = KeyValueOperationType.Decrement
136-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
138+
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:
137143
req = self._impl.request_builder.build_decrement_request(key, obs_handler, *opts, **kwargs)
138144
return await self._impl.decrement(req, obs_handler)
139145

@@ -190,8 +196,11 @@ async def append(self,
190196
AppendOptions(timeout=timedelta(seconds=2)))
191197
192198
"""
193-
op_type = KeyValueOperationType.Append
194-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
199+
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:
195204
req = self._impl.request_builder.build_append_request(key, value, obs_handler, *opts, **kwargs)
196205
return await self._impl.append(req, obs_handler)
197206

@@ -248,7 +257,10 @@ async def prepend(self,
248257
PrependOptions(timeout=timedelta(seconds=2)))
249258
250259
"""
251-
op_type = KeyValueOperationType.Prepend
252-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
260+
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:
253265
req = self._impl.request_builder.build_prepend_request(key, value, obs_handler, *opts, **kwargs)
254266
return await self._impl.prepend(req, obs_handler)

acouchbase/collection.py

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ async def get(self,
119119
print(f'Document value: {res.content_as[dict]}')
120120
121121
"""
122-
op_type = KeyValueOperationType.Get
123-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
122+
instruments = self._impl.observability_instruments
123+
if instruments.is_noop:
124+
req = self._impl.request_builder.build_get_request(key, None, *opts, **kwargs)
125+
return await self._impl.get(req, None)
126+
async with ObservableRequestHandler(KeyValueOperationType.Get, instruments) as obs_handler:
124127
req = self._impl.request_builder.build_get_request(key, obs_handler, *opts, **kwargs)
125128
return await self._impl.get(req, obs_handler)
126129

@@ -170,8 +173,11 @@ async def get_any_replica(self,
170173
print(f'Document value: {res.content_as[dict]}')
171174
172175
""" # noqa: E501
173-
op_type = KeyValueOperationType.GetAnyReplica
174-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
176+
instruments = self._impl.observability_instruments
177+
if instruments.is_noop:
178+
req = self._impl.request_builder.build_get_any_replica_request(key, None, *opts, **kwargs)
179+
return await self._impl.get_any_replica(req, None)
180+
async with ObservableRequestHandler(KeyValueOperationType.GetAnyReplica, instruments) as obs_handler:
175181
req = self._impl.request_builder.build_get_any_replica_request(key, obs_handler, *opts, **kwargs)
176182
return await self._impl.get_any_replica(req, obs_handler)
177183

@@ -241,8 +247,11 @@ async def get_all_replicas(self,
241247
break
242248
243249
"""
244-
op_type = KeyValueOperationType.GetAllReplicas
245-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
250+
instruments = self._impl.observability_instruments
251+
if instruments.is_noop:
252+
req = self._impl.request_builder.build_get_all_replicas_request(key, None, *opts, **kwargs)
253+
return await self._impl.get_all_replicas(req, None)
254+
async with ObservableRequestHandler(KeyValueOperationType.GetAllReplicas, instruments) as obs_handler:
246255
req = self._impl.request_builder.build_get_all_replicas_request(key, obs_handler, *opts, **kwargs)
247256
return await self._impl.get_all_replicas(req, obs_handler)
248257

@@ -287,8 +296,11 @@ async def exists(self,
287296
print(f'Document w/ key - {key} {"exists" if res.exists else "does not exist"}')
288297
289298
"""
290-
op_type = KeyValueOperationType.Exists
291-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
299+
instruments = self._impl.observability_instruments
300+
if instruments.is_noop:
301+
req = self._impl.request_builder.build_exists_request(key, None, *opts, **kwargs)
302+
return await self._impl.exists(req, None)
303+
async with ObservableRequestHandler(KeyValueOperationType.Exists, instruments) as obs_handler:
292304
req = self._impl.request_builder.build_exists_request(key, obs_handler, *opts, **kwargs)
293305
return await self._impl.exists(req, obs_handler)
294306

@@ -354,8 +366,11 @@ async def insert(self,
354366
res = await collection.insert(key, doc, InsertOptions(durability=durability))
355367
356368
"""
357-
op_type = KeyValueOperationType.Insert
358-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
369+
instruments = self._impl.observability_instruments
370+
if instruments.is_noop:
371+
req = self._impl.request_builder.build_insert_request(key, value, None, *opts, **kwargs)
372+
return await self._impl.insert(req, None)
373+
async with ObservableRequestHandler(KeyValueOperationType.Insert, instruments) as obs_handler:
359374
req = self._impl.request_builder.build_insert_request(key, value, obs_handler, *opts, **kwargs)
360375
return await self._impl.insert(req, obs_handler)
361376

@@ -417,8 +432,11 @@ async def upsert(self,
417432
res = await collection.upsert(key, doc, InsertOptions(durability=durability))
418433
419434
"""
420-
op_type = KeyValueOperationType.Upsert
421-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
435+
instruments = self._impl.observability_instruments
436+
if instruments.is_noop:
437+
req = self._impl.request_builder.build_upsert_request(key, value, None, *opts, **kwargs)
438+
return await self._impl.upsert(req, None)
439+
async with ObservableRequestHandler(KeyValueOperationType.Upsert, instruments) as obs_handler:
422440
req = self._impl.request_builder.build_upsert_request(key, value, obs_handler, *opts, **kwargs)
423441
return await self._impl.upsert(req, obs_handler)
424442

@@ -474,8 +492,11 @@ async def replace(self,
474492
res = await collection.replace(key, doc, InsertOptions(durability=durability))
475493
476494
"""
477-
op_type = KeyValueOperationType.Replace
478-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
495+
instruments = self._impl.observability_instruments
496+
if instruments.is_noop:
497+
req = self._impl.request_builder.build_replace_request(key, value, None, *opts, **kwargs)
498+
return await self._impl.replace(req, None)
499+
async with ObservableRequestHandler(KeyValueOperationType.Replace, instruments) as obs_handler:
479500
req = self._impl.request_builder.build_replace_request(key, value, obs_handler, *opts, **kwargs)
480501
return await self._impl.replace(req, obs_handler)
481502

@@ -521,8 +542,11 @@ async def remove(self,
521542
res = collection.remove('airline_10', RemoveOptions(durability=durability))
522543
523544
"""
524-
op_type = KeyValueOperationType.Remove
525-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
545+
instruments = self._impl.observability_instruments
546+
if instruments.is_noop:
547+
req = self._impl.request_builder.build_remove_request(key, None, *opts, **kwargs)
548+
return await self._impl.remove(req, None)
549+
async with ObservableRequestHandler(KeyValueOperationType.Remove, instruments) as obs_handler:
526550
req = self._impl.request_builder.build_remove_request(key, obs_handler, *opts, **kwargs)
527551
return await self._impl.remove(req, obs_handler)
528552

@@ -576,8 +600,11 @@ async def touch(self,
576600
TouchOptions(timeout=timedelta(seconds=2)))
577601
578602
"""
579-
op_type = KeyValueOperationType.Touch
580-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
603+
instruments = self._impl.observability_instruments
604+
if instruments.is_noop:
605+
req = self._impl.request_builder.build_touch_request(key, expiry, None, *opts, **kwargs)
606+
return await self._impl.touch(req, None)
607+
async with ObservableRequestHandler(KeyValueOperationType.Touch, instruments) as obs_handler:
581608
req = self._impl.request_builder.build_touch_request(key, expiry, obs_handler, *opts, **kwargs)
582609
return await self._impl.touch(req, obs_handler)
583610

@@ -634,8 +661,11 @@ async def get_and_touch(self,
634661
print(f'Document w/ updated expiry: {res.content_as[dict]}')
635662
636663
"""
637-
op_type = KeyValueOperationType.GetAndTouch
638-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
664+
instruments = self._impl.observability_instruments
665+
if instruments.is_noop:
666+
req = self._impl.request_builder.build_get_and_touch_request(key, expiry, None, *opts, **kwargs)
667+
return await self._impl.get_and_touch(req, None)
668+
async with ObservableRequestHandler(KeyValueOperationType.GetAndTouch, instruments) as obs_handler:
639669
req = self._impl.request_builder.build_get_and_touch_request(key, expiry, obs_handler, *opts, **kwargs)
640670
return await self._impl.get_and_touch(req, obs_handler)
641671

@@ -692,8 +722,11 @@ async def get_and_lock(self,
692722
print(f'Locked document: {res.content_as[dict]}')
693723
694724
"""
695-
op_type = KeyValueOperationType.GetAndLock
696-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
725+
instruments = self._impl.observability_instruments
726+
if instruments.is_noop:
727+
req = self._impl.request_builder.build_get_and_lock_request(key, lock_time, None, *opts, **kwargs)
728+
return await self._impl.get_and_lock(req, None)
729+
async with ObservableRequestHandler(KeyValueOperationType.GetAndLock, instruments) as obs_handler:
697730
req = self._impl.request_builder.build_get_and_lock_request(key, lock_time, obs_handler, *opts, **kwargs)
698731
return await self._impl.get_and_lock(req, obs_handler)
699732

@@ -739,8 +772,11 @@ async def unlock(self,
739772
await collection.upsert(key, res.content_as[dict])
740773
741774
"""
742-
op_type = KeyValueOperationType.Unlock
743-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
775+
instruments = self._impl.observability_instruments
776+
if instruments.is_noop:
777+
req = self._impl.request_builder.build_unlock_request(key, cas, None, *opts, **kwargs)
778+
return await self._impl.unlock(req, None)
779+
async with ObservableRequestHandler(KeyValueOperationType.Unlock, instruments) as obs_handler:
744780
req = self._impl.request_builder.build_unlock_request(key, cas, obs_handler, *opts, **kwargs)
745781
await self._impl.unlock(req, obs_handler)
746782

@@ -801,8 +837,11 @@ async def lookup_in(self,
801837
print(f'Hotel {key} coordinates: {res.content_as[dict](0)}')
802838
803839
"""
804-
op_type = KeyValueOperationType.LookupIn
805-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
840+
instruments = self._impl.observability_instruments
841+
if instruments.is_noop:
842+
req = self._impl.request_builder.build_lookup_in_request(key, spec, None, *opts, **kwargs)
843+
return await self._impl.lookup_in(req, None)
844+
async with ObservableRequestHandler(KeyValueOperationType.LookupIn, instruments) as obs_handler:
806845
req = self._impl.request_builder.build_lookup_in_request(key, spec, obs_handler, *opts, **kwargs)
807846
return await self._impl.lookup_in(req, obs_handler)
808847

@@ -865,8 +904,12 @@ async def lookup_in_any_replica(self,
865904
print(f'Hotel {key} coordinates: {res.content_as[dict](0)}')
866905
867906
"""
868-
op_type = KeyValueOperationType.LookupInAnyReplica
869-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
907+
instruments = self._impl.observability_instruments
908+
if instruments.is_noop:
909+
req = self._impl.request_builder.build_lookup_in_any_replica_request(
910+
key, spec, None, *opts, **kwargs)
911+
return await self._impl.lookup_in_any_replica(req, None)
912+
async with ObservableRequestHandler(KeyValueOperationType.LookupInAnyReplica, instruments) as obs_handler:
870913
req = self._impl.request_builder.build_lookup_in_any_replica_request(
871914
key, spec, obs_handler, *opts, **kwargs)
872915
return await self._impl.lookup_in_any_replica(req, obs_handler)
@@ -953,8 +996,12 @@ async def lookup_in_all_replicas(self,
953996
break
954997
955998
""" # noqa: E501
956-
op_type = KeyValueOperationType.LookupInAllReplicas
957-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
999+
instruments = self._impl.observability_instruments
1000+
if instruments.is_noop:
1001+
req = self._impl.request_builder.build_lookup_in_all_replicas_request(
1002+
key, spec, None, *opts, **kwargs)
1003+
return await self._impl.lookup_in_all_replicas(req, None)
1004+
async with ObservableRequestHandler(KeyValueOperationType.LookupInAllReplicas, instruments) as obs_handler:
9581005
req = self._impl.request_builder.build_lookup_in_all_replicas_request(
9591006
key, spec, obs_handler, *opts, **kwargs)
9601007
return await self._impl.lookup_in_all_replicas(req, obs_handler)
@@ -1014,8 +1061,11 @@ async def mutate_in(self,
10141061
MutateInOptions(timeout=timedelta(seconds=2)))
10151062
10161063
"""
1017-
op_type = KeyValueOperationType.MutateIn
1018-
async with ObservableRequestHandler(op_type, self._impl.observability_instruments) as obs_handler:
1064+
instruments = self._impl.observability_instruments
1065+
if instruments.is_noop:
1066+
req = self._impl.request_builder.build_mutate_in_request(key, spec, None, *opts, **kwargs)
1067+
return await self._impl.mutate_in(req, None)
1068+
async with ObservableRequestHandler(KeyValueOperationType.MutateIn, instruments) as obs_handler:
10191069
req = self._impl.request_builder.build_mutate_in_request(key, spec, obs_handler, *opts, **kwargs)
10201070
return await self._impl.mutate_in(req, obs_handler)
10211071

0 commit comments

Comments
 (0)