1
+ import time
1
2
from opentelemetry .instrumentation .milvus .utils import dont_throw
2
3
from opentelemetry .semconv .trace import SpanAttributes
3
4
4
5
from opentelemetry import context as context_api
6
+ from opentelemetry .trace .status import Status , StatusCode
5
7
from opentelemetry .instrumentation .utils import (
6
8
_SUPPRESS_INSTRUMENTATION_KEY ,
7
9
)
8
10
from opentelemetry .semconv_ai import Events , EventAttributes
9
- from opentelemetry .semconv_ai import SpanAttributes as AISpanAttributes
11
+ from opentelemetry .semconv_ai import SpanAttributes as AISpanAttributes , Meters
10
12
11
13
12
14
def _with_tracer_wrapper (func ):
13
15
"""Helper for providing tracer for wrapper functions."""
14
16
15
- def _with_tracer (tracer , to_wrap ):
17
+ def _with_tracer (
18
+ tracer ,
19
+ query_duration_metric ,
20
+ distance_metric ,
21
+ insert_units_metric ,
22
+ upsert_units_metric ,
23
+ delete_units_metric ,
24
+ to_wrap ):
16
25
def wrapper (wrapped , instance , args , kwargs ):
17
- return func (tracer , to_wrap , wrapped , instance , args , kwargs )
26
+ return func (
27
+ tracer ,
28
+ query_duration_metric ,
29
+ distance_metric ,
30
+ insert_units_metric ,
31
+ upsert_units_metric ,
32
+ delete_units_metric ,
33
+ to_wrap ,
34
+ wrapped ,
35
+ instance ,
36
+ args ,
37
+ kwargs )
18
38
19
39
return wrapper
20
40
@@ -29,44 +49,76 @@ def _set_span_attribute(span, name, value):
29
49
30
50
31
51
@_with_tracer_wrapper
32
- def _wrap (tracer , to_wrap , wrapped , instance , args , kwargs ):
52
+ def _wrap (
53
+ tracer ,
54
+ query_duration_metric ,
55
+ distance_metric ,
56
+ insert_units_metric ,
57
+ upsert_units_metric ,
58
+ delete_units_metric ,
59
+ to_wrap ,
60
+ wrapped ,
61
+ instance ,
62
+ args ,
63
+ kwargs
64
+ ):
33
65
"""Instruments and calls every function defined in TO_WRAP."""
34
66
if context_api .get_value (_SUPPRESS_INSTRUMENTATION_KEY ):
35
67
return wrapped (* args , ** kwargs )
36
68
69
+ method = to_wrap .get ("method" )
37
70
name = to_wrap .get ("span_name" )
38
71
with tracer .start_as_current_span (name ) as span :
39
72
span .set_attribute (SpanAttributes .DB_SYSTEM , "milvus" )
40
73
span .set_attribute (SpanAttributes .DB_OPERATION , to_wrap .get ("method" ))
41
74
42
- if to_wrap . get ( " method" ) == "insert" :
75
+ if method == "insert" :
43
76
_set_insert_attributes (span , kwargs )
44
- elif to_wrap . get ( " method" ) == "upsert" :
77
+ elif method == "upsert" :
45
78
_set_upsert_attributes (span , kwargs )
46
- elif to_wrap . get ( " method" ) == "delete" :
79
+ elif method == "delete" :
47
80
_set_delete_attributes (span , kwargs )
48
- elif to_wrap . get ( " method" ) == "search" :
81
+ elif method == "search" :
49
82
_set_search_attributes (span , kwargs )
50
- elif to_wrap . get ( " method" ) == "get" :
83
+ elif method == "get" :
51
84
_set_get_attributes (span , kwargs )
52
- elif to_wrap . get ( " method" ) == "query" :
85
+ elif method == "query" :
53
86
_set_query_attributes (span , kwargs )
54
- elif to_wrap . get ( " method" ) == "create_collection" :
87
+ elif method == "create_collection" :
55
88
_set_create_collection_attributes (span , kwargs )
56
- elif to_wrap . get ( " method" ) == "hybrid_search" :
89
+ elif method == "hybrid_search" :
57
90
_set_hybrid_search_attributes (span , kwargs )
58
91
92
+ start_time = time .time ()
59
93
return_value = wrapped (* args , ** kwargs )
94
+ end_time = time .time ()
60
95
61
- if to_wrap . get ( " method" ) == "query" :
96
+ if method == "query" :
62
97
_add_query_result_events (span , return_value )
63
98
64
- if (
65
- to_wrap .get ("method" ) == "search"
66
- or to_wrap .get ("method" ) == "hybrid_search"
67
- ):
99
+ elif (method == "search" or method == "hybrid_search" ):
68
100
_add_search_result_events (span , return_value )
69
101
102
+ shared_attributes = {SpanAttributes .DB_SYSTEM : "milvus" }
103
+ duration = end_time - start_time
104
+ if duration > 0 and query_duration_metric and method == "query" :
105
+ query_duration_metric .record (duration , shared_attributes )
106
+
107
+ if return_value and span .is_recording ():
108
+ if method == "search" or method == "hybrid_search" :
109
+ set_search_response (span , distance_metric , shared_attributes , return_value )
110
+
111
+ _set_response_attributes (
112
+ span ,
113
+ insert_units_metric ,
114
+ upsert_units_metric ,
115
+ delete_units_metric ,
116
+ shared_attributes ,
117
+ return_value ,
118
+ )
119
+
120
+ span .set_status (Status (StatusCode .OK ))
121
+
70
122
return return_value
71
123
72
124
@@ -101,6 +153,31 @@ def count_or_none(obj):
101
153
return None
102
154
103
155
156
+ def _set_response_attributes (
157
+ span ,
158
+ insert_units_metric ,
159
+ upsert_units_metric ,
160
+ delete_units_metric ,
161
+ shared_attributes ,
162
+ response
163
+ ):
164
+ print (response )
165
+ if 'upsert_count' in response :
166
+ upsert_count = response ['upsert_count' ]
167
+ upsert_units_metric .add (upsert_count , shared_attributes )
168
+ span .set_attribute (Meters .MILVUS_DB_USAGE_UPSERT_UNITS , upsert_count )
169
+
170
+ if ('insert_count' in response ):
171
+ insert_count = response ['insert_count' ]
172
+ insert_units_metric .add (insert_count , shared_attributes )
173
+ span .set_attribute (Meters .MILVUS_DB_USAGE_INSERT_UNITS , insert_count )
174
+
175
+ if ('delete_count' in response ):
176
+ delete_count = response ['delete_count' ]
177
+ delete_units_metric .add (delete_count , shared_attributes )
178
+ span .set_attribute (Meters .MILVUS_DB_USAGE_DELETE_UNITS , delete_count )
179
+
180
+
104
181
@dont_throw
105
182
def _set_create_collection_attributes (span , kwargs ):
106
183
_set_span_attribute (
@@ -425,3 +502,13 @@ def _set_delete_attributes(span, kwargs):
425
502
AISpanAttributes .MILVUS_DELETE_FILTER ,
426
503
_encode_filter (kwargs .get ("filter" )),
427
504
)
505
+
506
+
507
+ @dont_throw
508
+ def set_search_response (distance_metric , shared_attributes , response ):
509
+ for query_result in response :
510
+ for match in query_result :
511
+ distance = match .get ("distance" )
512
+
513
+ if distance_metric and distance is not None :
514
+ distance_metric .record (distance , shared_attributes )
0 commit comments