|
| 1 | +from langtrace_python_sdk.utils.silently_fail import silently_fail |
| 2 | +from opentelemetry.trace import Tracer |
| 3 | +from opentelemetry.trace import SpanKind |
| 4 | +from langtrace_python_sdk.utils import handle_span_error, set_span_attribute |
| 5 | +from langtrace_python_sdk.utils.llm import ( |
| 6 | + get_extra_attributes, |
| 7 | + set_span_attributes, |
| 8 | +) |
| 9 | +import json |
| 10 | + |
| 11 | + |
| 12 | +def generic_patch(api, version: str, tracer: Tracer): |
| 13 | + def traced_method(wrapped, instance, args, kwargs): |
| 14 | + span_name = api["SPAN_NAME"] |
| 15 | + operation = api["OPERATION"] |
| 16 | + with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span: |
| 17 | + try: |
| 18 | + span_attributes = { |
| 19 | + "db.system": "milvus", |
| 20 | + "db.operation": operation, |
| 21 | + "db.name": kwargs.get("collection_name", None), |
| 22 | + **get_extra_attributes(), |
| 23 | + } |
| 24 | + |
| 25 | + if operation == "create_collection": |
| 26 | + set_create_collection_attributes(span_attributes, kwargs) |
| 27 | + |
| 28 | + elif operation == "insert" or operation == "upsert": |
| 29 | + set_insert_or_upsert_attributes(span_attributes, kwargs) |
| 30 | + |
| 31 | + elif operation == "search": |
| 32 | + set_search_attributes(span_attributes, kwargs) |
| 33 | + |
| 34 | + elif operation == "query": |
| 35 | + set_query_attributes(span_attributes, kwargs) |
| 36 | + |
| 37 | + set_span_attributes(span, span_attributes) |
| 38 | + result = wrapped(*args, **kwargs) |
| 39 | + |
| 40 | + if operation == "query": |
| 41 | + set_query_response_attributes(span, result) |
| 42 | + |
| 43 | + if operation == "search": |
| 44 | + set_search_response_attributes(span, result) |
| 45 | + return result |
| 46 | + except Exception as err: |
| 47 | + handle_span_error(span, err) |
| 48 | + raise |
| 49 | + |
| 50 | + return traced_method |
| 51 | + |
| 52 | + |
| 53 | +@silently_fail |
| 54 | +def set_create_collection_attributes(span_attributes, kwargs): |
| 55 | + span_attributes["db.dimension"] = kwargs.get("dimension", None) |
| 56 | + |
| 57 | + |
| 58 | +@silently_fail |
| 59 | +def set_insert_or_upsert_attributes(span_attributes, kwargs): |
| 60 | + data = kwargs.get("data") |
| 61 | + timeout = kwargs.get("timeout") |
| 62 | + partition_name = kwargs.get("partition_name") |
| 63 | + |
| 64 | + span_attributes["db.num_entities"] = len(data) if data else None |
| 65 | + span_attributes["db.timeout"] = timeout |
| 66 | + span_attributes["db.partition_name"] = partition_name |
| 67 | + |
| 68 | + |
| 69 | +@silently_fail |
| 70 | +def set_search_attributes(span_attributes, kwargs): |
| 71 | + data = kwargs.get("data") |
| 72 | + filter = kwargs.get("filter") |
| 73 | + limit = kwargs.get("limit") |
| 74 | + output_fields = kwargs.get("output_fields") |
| 75 | + search_params = kwargs.get("search_params") |
| 76 | + timeout = kwargs.get("timeout") |
| 77 | + partition_names = kwargs.get("partition_names") |
| 78 | + anns_field = kwargs.get("anns_field") |
| 79 | + span_attributes["db.num_queries"] = len(data) if data else None |
| 80 | + span_attributes["db.filter"] = filter |
| 81 | + span_attributes["db.limit"] = limit |
| 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) |
| 85 | + span_attributes["db.anns_field"] = anns_field |
| 86 | + span_attributes["db.timeout"] = timeout |
| 87 | + |
| 88 | + |
| 89 | +@silently_fail |
| 90 | +def set_query_attributes(span_attributes, kwargs): |
| 91 | + filter = kwargs.get("filter") |
| 92 | + output_fields = kwargs.get("output_fields") |
| 93 | + timeout = kwargs.get("timeout") |
| 94 | + partition_names = kwargs.get("partition_names") |
| 95 | + ids = kwargs.get("ids") |
| 96 | + |
| 97 | + span_attributes["db.filter"] = filter |
| 98 | + span_attributes["db.output_fields"] = output_fields |
| 99 | + span_attributes["db.timeout"] = timeout |
| 100 | + span_attributes["db.partition_names"] = partition_names |
| 101 | + span_attributes["db.ids"] = ids |
| 102 | + |
| 103 | + |
| 104 | +@silently_fail |
| 105 | +def set_query_response_attributes(span, result): |
| 106 | + set_span_attribute(span, name="db.num_matches", value=len(result)) |
| 107 | + for match in result: |
| 108 | + span.add_event( |
| 109 | + "db.query.match", |
| 110 | + attributes=match, |
| 111 | + ) |
| 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