1+ from langtrace_python_sdk .utils .silently_fail import silently_fail
12from opentelemetry .trace import Tracer
23from opentelemetry .trace import SpanKind
3- from langtrace_python_sdk .utils import handle_span_error
4+ from langtrace_python_sdk .utils import handle_span_error , set_span_attribute
45from langtrace_python_sdk .utils .llm import (
56 get_extra_attributes ,
67 set_span_attributes ,
78)
9+ import json
810
911
1012def generic_patch (api , version : str , tracer : Tracer ):
@@ -38,6 +40,8 @@ def traced_method(wrapped, instance, args, kwargs):
3840 if operation == "query" :
3941 set_query_response_attributes (span , result )
4042
43+ if operation == "search" :
44+ set_search_response_attributes (span , result )
4145 return result
4246 except Exception as err :
4347 handle_span_error (span , err )
@@ -46,10 +50,12 @@ def traced_method(wrapped, instance, args, kwargs):
4650 return traced_method
4751
4852
53+ @silently_fail
4954def set_create_collection_attributes (span_attributes , kwargs ):
5055 span_attributes ["db.dimension" ] = kwargs .get ("dimension" , None )
5156
5257
58+ @silently_fail
5359def set_insert_or_upsert_attributes (span_attributes , kwargs ):
5460 data = kwargs .get ("data" )
5561 timeout = kwargs .get ("timeout" )
@@ -60,6 +66,7 @@ def set_insert_or_upsert_attributes(span_attributes, kwargs):
6066 span_attributes ["db.partition_name" ] = partition_name
6167
6268
69+ @silently_fail
6370def set_search_attributes (span_attributes , kwargs ):
6471 data = kwargs .get ("data" )
6572 filter = kwargs .get ("filter" )
@@ -69,17 +76,17 @@ def set_search_attributes(span_attributes, kwargs):
6976 timeout = kwargs .get ("timeout" )
7077 partition_names = kwargs .get ("partition_names" )
7178 anns_field = kwargs .get ("anns_field" )
72-
7379 span_attributes ["db.num_queries" ] = len (data ) if data else None
7480 span_attributes ["db.filter" ] = filter
7581 span_attributes ["db.limit" ] = limit
76- span_attributes ["db.output_fields" ] = output_fields
77- span_attributes ["db.search_params" ] = search_params
78- span_attributes ["db.partition_names" ] = partition_names
82+ span_attributes ["db.output_fields" ] = json . dumps ( output_fields )
83+ span_attributes ["db.search_params" ] = json . dumps ( search_params )
84+ span_attributes ["db.partition_names" ] = json . dumps ( partition_names )
7985 span_attributes ["db.anns_field" ] = anns_field
8086 span_attributes ["db.timeout" ] = timeout
8187
8288
89+ @silently_fail
8390def set_query_attributes (span_attributes , kwargs ):
8491 filter = kwargs .get ("filter" )
8592 output_fields = kwargs .get ("output_fields" )
@@ -94,9 +101,25 @@ def set_query_attributes(span_attributes, kwargs):
94101 span_attributes ["db.ids" ] = ids
95102
96103
104+ @silently_fail
97105def set_query_response_attributes (span , result ):
106+ set_span_attribute (span , name = "db.num_matches" , value = len (result ))
98107 for match in result :
99108 span .add_event (
100109 "db.query.match" ,
101110 attributes = match ,
102111 )
112+
113+
114+ @silently_fail
115+ def set_search_response_attributes (span , result ):
116+ for res in result :
117+ for match in res :
118+ span .add_event (
119+ "db.search.match" ,
120+ attributes = {
121+ "id" : match ["id" ],
122+ "distance" : str (match ["distance" ]),
123+ "entity" : json .dumps (match ["entity" ]),
124+ },
125+ )
0 commit comments