Skip to content

Commit 120b775

Browse files
authored
Preserve original method args and kwargs (#105)
1 parent 4f758ae commit 120b775

File tree

3 files changed

+418
-97
lines changed

3 files changed

+418
-97
lines changed

quotientai/tracing/instrumentation/chroma.py

Lines changed: 126 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,24 @@ def _wrap_create_collection(self, original_method):
8585
instrumentor = self # Capture the instrumentor instance
8686

8787
@functools.wraps(original_method)
88-
def wrapper(self, name, **kwargs):
88+
def wrapper(*args, **kwargs):
89+
# Extract self and other parameters
90+
if not args:
91+
raise TypeError("Missing 'self' argument")
92+
self_obj = args[0]
93+
other_args = args[1:]
94+
95+
# Get collection name from first positional argument or kwargs
96+
name = other_args[0] if other_args else kwargs.get('name')
97+
8998
attributes = instrumentor._get_common_attributes("create_collection")
9099
attributes["db.collection.name"] = name
91100
attributes["db.system.name"] = "chroma"
92101

93102
with instrumentor.tracer.start_as_current_span("chroma.create_collection") as span:
94103
span.set_attributes(attributes)
95104
try:
96-
result = original_method(self, name, **kwargs)
105+
result = original_method(*args, **kwargs)
97106
span.set_attribute("db.operation.status", "completed")
98107
if hasattr(result, 'id'):
99108
span.set_attribute("db.collection.id", str(result.id))
@@ -113,18 +122,35 @@ def _wrap_get_collection(self, original_method):
113122
instrumentor = self # Capture the instrumentor instance
114123

115124
@functools.wraps(original_method)
116-
def wrapper(self, name=None, id=None):
125+
def wrapper(*args, **kwargs):
126+
# Extract self and other parameters
127+
if not args:
128+
raise TypeError("Missing 'self' argument")
129+
self_obj = args[0]
130+
other_args = args[1:]
131+
117132
attributes = instrumentor._get_common_attributes("get_collection")
118133
attributes["db.system.name"] = "chroma"
134+
135+
# Get name and id from kwargs or positional args
136+
name = kwargs.get('name')
137+
id_param = kwargs.get('id')
138+
139+
# If not in kwargs, check positional args
140+
if not name and other_args:
141+
name = other_args[0]
142+
if not id_param and len(other_args) > 1:
143+
id_param = other_args[1]
144+
119145
if name:
120146
attributes["db.collection.name"] = name
121-
if id:
122-
attributes["db.collection.id"] = str(id)
147+
if id_param:
148+
attributes["db.collection.id"] = str(id_param)
123149

124150
with instrumentor.tracer.start_as_current_span("chroma.get_collection") as span:
125151
span.set_attributes(attributes)
126152
try:
127-
result = original_method(self, name=name, id=id)
153+
result = original_method(*args, **kwargs)
128154
span.set_attribute("db.operation.status", "completed")
129155

130156
# Instrument the collection methods after retrieval
@@ -142,14 +168,14 @@ def _wrap_list_collections(self, original_method):
142168
instrumentor = self # Capture the instrumentor instance
143169

144170
@functools.wraps(original_method)
145-
def wrapper(self):
171+
def wrapper(*args, **kwargs):
146172
attributes = instrumentor._get_common_attributes("list_collections")
147173
attributes["db.system.name"] = "chroma"
148174

149175
with instrumentor.tracer.start_as_current_span("chroma.list_collections") as span:
150176
span.set_attributes(attributes)
151177
try:
152-
result = original_method(self)
178+
result = original_method(*args, **kwargs)
153179
span.set_attribute("db.operation.status", "completed")
154180
span.set_attribute("db.collections.count", len(result))
155181
return result
@@ -164,15 +190,24 @@ def _wrap_delete_collection(self, original_method):
164190
instrumentor = self # Capture the instrumentor instance
165191

166192
@functools.wraps(original_method)
167-
def wrapper(self, name):
193+
def wrapper(*args, **kwargs):
194+
# Extract self and other parameters
195+
if not args:
196+
raise TypeError("Missing 'self' argument")
197+
self_obj = args[0]
198+
other_args = args[1:]
199+
200+
# Get collection name from first positional argument or kwargs
201+
name = other_args[0] if other_args else kwargs.get('name')
202+
168203
attributes = instrumentor._get_common_attributes("delete_collection")
169204
attributes["db.collection.name"] = name
170205
attributes["db.system.name"] = "chroma"
171206

172207
with instrumentor.tracer.start_as_current_span("chroma.delete_collection") as span:
173208
span.set_attributes(attributes)
174209
try:
175-
result = original_method(self, name)
210+
result = original_method(*args, **kwargs)
176211
span.set_attribute("db.operation.status", "completed")
177212
return result
178213
except Exception as e:
@@ -210,23 +245,38 @@ def _wrap_add(self, original_method):
210245
instrumentor = self # Capture the instrumentor instance
211246

212247
@functools.wraps(original_method)
213-
def wrapper(self, documents=None, embeddings=None, metadatas=None, ids=None, **kwargs):
214-
attributes = instrumentor._get_common_attributes("add", collection_name=getattr(self, 'name', None))
248+
def wrapper(*args, **kwargs):
249+
# Extract self and other parameters
250+
if not args:
251+
raise TypeError("Missing 'self' argument")
252+
self_obj = args[0]
253+
254+
# Get collection name for attributes
255+
collection_name = getattr(self_obj, 'name', None)
256+
attributes = instrumentor._get_common_attributes("add", collection_name=collection_name)
215257
attributes["db.system.name"] = "chroma"
258+
259+
# Extract specific parameters for attributes
260+
documents = kwargs.get('documents')
216261
if documents:
217262
attributes["db.documents_count"] = len(documents)
263+
264+
ids = kwargs.get('ids')
218265
if ids:
219266
attributes["db.ids_count"] = len(ids)
267+
268+
embeddings = kwargs.get('embeddings')
220269
if embeddings:
221270
attributes["db.vector_count"] = len(embeddings)
271+
272+
metadatas = kwargs.get('metadatas')
222273
if metadatas:
223274
attributes["db.metadatas_count"] = len(metadatas)
224275

225276
with instrumentor.tracer.start_as_current_span("chroma.collection.add") as span:
226277
span.set_attributes(attributes)
227278
try:
228-
result = original_method(self, documents=documents, embeddings=embeddings,
229-
metadatas=metadatas, ids=ids, **kwargs)
279+
result = original_method(*args, **kwargs)
230280
span.set_attribute("db.operation.status", "completed")
231281
return result
232282
except Exception as e:
@@ -240,41 +290,44 @@ def _wrap_query(self, original_method):
240290
instrumentor = self # Capture the instrumentor instance
241291

242292
@functools.wraps(original_method)
243-
def wrapper(
244-
self,
245-
query_texts=None,
246-
query_embeddings=None,
247-
n_results=10,
248-
where=None,
249-
where_document=None,
250-
include=None,
251-
**kwargs,
252-
):
253-
attributes = instrumentor._get_common_attributes("query", collection_name=getattr(self, 'name', None))
293+
def wrapper(*args, **kwargs):
294+
# Extract self and other parameters
295+
if not args:
296+
raise TypeError("Missing 'self' argument")
297+
298+
breakpoint()
299+
self_obj = args[0]
300+
other_args = args[1:]
301+
302+
# Get collection name for attributes
303+
collection_name = getattr(self_obj, 'name', None)
304+
attributes = instrumentor._get_common_attributes("query", collection_name=collection_name)
254305
attributes["db.system.name"] = "chroma"
306+
307+
# Extract specific parameters for attributes
308+
n_results = kwargs.get('n_results', 10) # Default from ChromaDB
255309
attributes["db.n_results"] = n_results
310+
311+
query_texts = kwargs.get('query_texts')
256312
if query_texts:
257313
attributes["db.documents_count"] = len(query_texts)
314+
315+
query_embeddings = kwargs.get('query_embeddings')
258316
if query_embeddings:
259317
attributes["db.vector_count"] = len(query_embeddings)
318+
319+
where = kwargs.get('where')
260320
if where:
261321
attributes["db.filter"] = instrumentor._safe_json_dumps(where)
322+
323+
where_document = kwargs.get('where_document')
262324
if where_document:
263325
attributes["db.where_document"] = instrumentor._safe_json_dumps(where_document)
264326

265327
with instrumentor.tracer.start_as_current_span("chroma.collection.query") as span:
266328
span.set_attributes(attributes)
267329
try:
268-
result = original_method(
269-
self,
270-
query_texts=query_texts,
271-
query_embeddings=query_embeddings,
272-
n_results=n_results,
273-
where=where,
274-
where_document=where_document,
275-
include=include,
276-
**kwargs,
277-
)
330+
result = original_method(*args, **kwargs)
278331
span.set_attribute("db.operation.status", "completed")
279332

280333
# Add retrieved documents if available
@@ -315,22 +368,38 @@ def _wrap_update(self, original_method):
315368
instrumentor = self # Capture the instrumentor instance
316369

317370
@functools.wraps(original_method)
318-
def wrapper(self, ids, embeddings=None, metadatas=None, documents=None, **kwargs):
319-
attributes = instrumentor._get_common_attributes("update", collection_name=getattr(self, 'name', None))
371+
def wrapper(*args, **kwargs):
372+
# Extract self and other parameters
373+
if not args:
374+
raise TypeError("Missing 'self' argument")
375+
self_obj = args[0]
376+
377+
# Get collection name for attributes
378+
collection_name = getattr(self_obj, 'name', None)
379+
attributes = instrumentor._get_common_attributes("update", collection_name=collection_name)
320380
attributes["db.system.name"] = "chroma"
321-
attributes["db.ids_count"] = len(ids)
381+
382+
# Extract specific parameters for attributes
383+
ids = kwargs.get('ids')
384+
if ids:
385+
attributes["db.ids_count"] = len(ids)
386+
387+
embeddings = kwargs.get('embeddings')
322388
if embeddings:
323389
attributes["db.vector_count"] = len(embeddings)
390+
391+
metadatas = kwargs.get('metadatas')
324392
if metadatas:
325393
attributes["db.metadatas_count"] = len(metadatas)
394+
395+
documents = kwargs.get('documents')
326396
if documents:
327397
attributes["db.documents_count"] = len(documents)
328398

329399
with instrumentor.tracer.start_as_current_span("chroma.collection.update") as span:
330400
span.set_attributes(attributes)
331401
try:
332-
result = original_method(self, ids=ids, embeddings=embeddings,
333-
metadatas=metadatas, documents=documents, **kwargs)
402+
result = original_method(*args, **kwargs)
334403
span.set_attribute("db.operation.status", "completed")
335404
return result
336405
except Exception as e:
@@ -344,21 +413,34 @@ def _wrap_delete(self, original_method):
344413
instrumentor = self # Capture the instrumentor instance
345414

346415
@functools.wraps(original_method)
347-
def wrapper(self, ids=None, where=None, where_document=None, **kwargs):
348-
attributes = instrumentor._get_common_attributes("delete", collection_name=getattr(self, 'name', None))
416+
def wrapper(*args, **kwargs):
417+
# Extract self and other parameters
418+
if not args:
419+
raise TypeError("Missing 'self' argument")
420+
self_obj = args[0]
421+
422+
# Get collection name for attributes
423+
collection_name = getattr(self_obj, 'name', None)
424+
attributes = instrumentor._get_common_attributes("delete", collection_name=collection_name)
349425
attributes["db.system.name"] = "chroma"
426+
427+
# Extract specific parameters for attributes
428+
ids = kwargs.get('ids')
350429
if ids:
351430
attributes["db.ids_count"] = len(ids)
431+
432+
where = kwargs.get('where')
352433
if where:
353434
attributes["db.filter"] = instrumentor._safe_json_dumps(where)
435+
436+
where_document = kwargs.get('where_document')
354437
if where_document:
355438
attributes["db.where_document"] = instrumentor._safe_json_dumps(where_document)
356439

357440
with instrumentor.tracer.start_as_current_span("chroma.collection.delete") as span:
358441
span.set_attributes(attributes)
359442
try:
360-
result = original_method(self, ids=ids, where=where,
361-
where_document=where_document, **kwargs)
443+
result = original_method(*args, **kwargs)
362444
span.set_attribute("db.operation.status", "completed")
363445
return result
364446
except Exception as e:

0 commit comments

Comments
 (0)