openlit 1.33.13__py3-none-any.whl → 1.33.15__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,226 +1,45 @@
1
- # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, possibly-used-before-assignment, too-many-branches
2
1
  """
3
2
  Module for monitoring AstraDB.
4
3
  """
5
4
 
6
- import logging
7
- from opentelemetry.trace import SpanKind, Status, StatusCode
8
- from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
9
- from openlit.__helpers import handle_exception
5
+ import time
6
+ from opentelemetry.trace import SpanKind
7
+ from openlit.instrumentation.astra.utils import (
8
+ DB_OPERATION_MAP,
9
+ process_db_operations
10
+ )
10
11
  from openlit.semcov import SemanticConvetion
11
12
 
12
- # Initialize logger for logging potential issues and operations
13
- logger = logging.getLogger(__name__)
14
-
15
- def object_count(obj):
16
- """
17
- Counts Length of object if it exists, Else returns None
18
- """
19
-
20
- return len(obj) if obj else None
21
-
22
13
  def general_wrap(gen_ai_endpoint, version, environment, application_name,
23
14
  tracer, pricing_info, capture_message_content, metrics, disable_metrics):
24
15
  """
25
- Wraps a AstraDB operation to trace and log its execution metrics.
26
-
27
- This function is intended to wrap around AstraDB operations in order to
28
- measure their execution time, log relevant information, and trace the execution
29
- using OpenTelemetry. This helps in monitoring and debugging operations within
30
- the AstraDB space.
31
-
32
- Parameters:
33
- - operation (str): The specific AstraDB operation being monitored.
34
- Examples include 'create_index', 'query', 'upsert', etc.
35
- - version (str): The version of the application interfacing with AstraDB.
36
- - environment (str): The deployment environment, such as 'production' or 'development'.
37
- - application_name (str): The name of the application performing the AstraDB operation.
38
- - tracer (opentelemetry.trace.Tracer): An object used for OpenTelemetry tracing.
39
- - pricing_info (dict): Information about pricing, not used in current implementation.
40
- - capture_message_content (bool): A flag indicating whether the content of responses should be traced.
41
-
42
- Returns:
43
- - function: A decorator function that, when applied, wraps the target function with
44
- additional functionality for tracing and logging AstraDB operations.
16
+ Generates a telemetry wrapper for VectorDB function call
45
17
  """
46
18
 
47
19
  def wrapper(wrapped, instance, args, kwargs):
48
20
  """
49
- Executes the wrapped AstraDB operation, adding tracing and logging.
50
-
51
- This inner wrapper function captures the execution of AstraDB operations,
52
- annotating the operation with relevant metrics and tracing information, and
53
- ensuring any exceptions are caught and logged appropriately.
54
-
55
- Parameters:
56
- - wrapped (Callable): The AstraDB operation to be wrapped and executed.
57
- - instance (object): The instance on which the operation is called (for class methods).
58
- - args (tuple): Positional arguments for the AstraDB operation.
59
- - kwargs (dict): Keyword arguments for the AstraDB operation.
60
-
61
- Returns:
62
- - Any: The result of executing the wrapped AstraDB operation.
21
+ Wraps the VectorDB function call.
63
22
  """
64
23
 
65
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
66
- response = wrapped(*args, **kwargs)
67
-
68
- try:
69
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
70
- span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
71
- gen_ai_endpoint)
72
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
73
- environment)
74
- span.set_attribute(SERVICE_NAME,
75
- application_name)
76
- span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
77
- SemanticConvetion.GEN_AI_OPERATION_TYPE_VECTORDB)
78
- span.set_attribute(SemanticConvetion.DB_SYSTEM,
79
- SemanticConvetion.DB_SYSTEM_ASTRA)
80
-
81
- if gen_ai_endpoint == "astra.create_collection":
82
- db_operation = SemanticConvetion.DB_OPERATION_CREATE_COLLECTION
83
- span.set_attribute(SemanticConvetion.DB_OPERATION,
84
- db_operation)
85
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
86
- response.name)
87
- span.set_attribute(SemanticConvetion.DB_INDEX_DIMENSION,
88
- kwargs.get("dimension", ""))
89
- span.set_attribute(SemanticConvetion.DB_INDEX_METRIC,
90
- str(kwargs.get("metric", "")))
91
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
92
- instance.api_endpoint)
24
+ db_operation = DB_OPERATION_MAP.get(gen_ai_endpoint, "UNKNOWN")
25
+ if db_operation == SemanticConvetion.DB_OPERATION_REPLACE and kwargs.get('upsert'):
26
+ db_operation = SemanticConvetion.DB_OPERATION_UPSERT
93
27
 
94
- elif gen_ai_endpoint == "astra.drop_collection":
95
- db_operation = SemanticConvetion.DB_OPERATION_DELETE_COLLECTION
96
- span.set_attribute(SemanticConvetion.DB_OPERATION,
97
- db_operation)
98
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
99
- kwargs.get("name_or_collection", ""))
100
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
101
- instance.api_endpoint)
28
+ span_name = f"{db_operation} {instance.name}"
102
29
 
103
- elif gen_ai_endpoint == "astra.insert_one":
104
- db_operation = SemanticConvetion.DB_OPERATION_INSERT
105
- span.set_attribute(SemanticConvetion.DB_OPERATION,
106
- db_operation)
107
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
108
- instance.name)
109
- span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT,
110
- 1)
111
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
112
- instance.database.api_endpoint)
113
- span.set_attribute(SemanticConvetion.DB_OPERATION_ID,
114
- response.inserted_id)
115
-
116
- elif gen_ai_endpoint == "astra.insert_many":
117
- db_operation = SemanticConvetion.DB_OPERATION_INSERT
118
- span.set_attribute(SemanticConvetion.DB_OPERATION,
119
- db_operation)
120
- span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT,
121
- object_count(args[0]))
122
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
123
- instance.name)
124
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
125
- instance.database.api_endpoint)
126
-
127
- elif gen_ai_endpoint in ["astra.update_one", "astra.update_many"]:
128
- db_operation = SemanticConvetion.DB_OPERATION_UPDATE
129
- span.set_attribute(SemanticConvetion.DB_OPERATION,
130
- db_operation)
131
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
132
- instance.name)
133
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
134
- instance.database.api_endpoint)
135
- span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT,
136
- response.update_info.get("nModified", 0))
137
-
138
- elif gen_ai_endpoint == "astra.find_one_and_update":
139
- db_operation = SemanticConvetion.DB_OPERATION_UPDATE
140
- span.set_attribute(SemanticConvetion.DB_OPERATION,
141
- db_operation)
142
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
143
- instance.name)
144
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
145
- instance.database.api_endpoint)
146
-
147
- elif gen_ai_endpoint == "astra.find":
148
- db_operation = SemanticConvetion.DB_OPERATION_QUERY
149
- span.set_attribute(SemanticConvetion.DB_OPERATION,
150
- db_operation)
151
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
152
- instance.name)
153
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
154
- instance.database.api_endpoint)
155
- span.set_attribute(SemanticConvetion.DB_STATEMENT,
156
- str(args))
157
-
158
- elif gen_ai_endpoint == "astra.replace_one":
159
- if kwargs.get("upsert") is True:
160
- db_operation = SemanticConvetion.DB_OPERATION_UPSERT
161
- else:
162
- db_operation = SemanticConvetion.DB_OPERATION_REPLACE
163
- span.set_attribute(SemanticConvetion.DB_OPERATION,
164
- db_operation)
165
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
166
- instance.name)
167
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
168
- instance.database.api_endpoint)
169
- span.set_attribute(SemanticConvetion.DB_STATEMENT,
170
- str(args))
171
-
172
- elif gen_ai_endpoint in ["astra.delete_one", "astra.delete_many"]:
173
- db_operation = SemanticConvetion.DB_OPERATION_DELETE
174
- span.set_attribute(SemanticConvetion.DB_OPERATION,
175
- db_operation)
176
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
177
- instance.name)
178
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
179
- instance.database.api_endpoint)
180
- span.set_attribute(SemanticConvetion.DB_STATEMENT,
181
- str(args))
182
- span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT,
183
- response.deleted_count)
184
-
185
- elif gen_ai_endpoint == "astra.find_one_and_delete":
186
- db_operation = SemanticConvetion.DB_OPERATION_DELETE
187
- span.set_attribute(SemanticConvetion.DB_OPERATION,
188
- db_operation)
189
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
190
- instance.name)
191
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
192
- instance.database.api_endpoint)
193
- span.set_attribute(SemanticConvetion.DB_STATEMENT,
194
- str(args))
195
-
196
- span.set_status(Status(StatusCode.OK))
197
-
198
- if disable_metrics is False:
199
- attributes = {
200
- TELEMETRY_SDK_NAME:
201
- "openlit",
202
- SERVICE_NAME:
203
- application_name,
204
- SemanticConvetion.DB_SYSTEM:
205
- SemanticConvetion.DB_SYSTEM_ASTRA,
206
- DEPLOYMENT_ENVIRONMENT:
207
- environment,
208
- SemanticConvetion.GEN_AI_OPERATION:
209
- SemanticConvetion.GEN_AI_OPERATION_TYPE_VECTORDB,
210
- SemanticConvetion.DB_OPERATION:
211
- db_operation
212
- }
213
-
214
- metrics["db_requests"].add(1, attributes)
215
-
216
- # Return original response
217
- return response
218
-
219
- except Exception as e:
220
- handle_exception(span, e)
221
- logger.error("Error in trace creation: %s", e)
222
-
223
- # Return original response
224
- return response
30
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
31
+ start_time = time.time()
32
+ response = wrapped(*args, **kwargs)
33
+ server_address = getattr(getattr(instance, 'database', instance), 'api_endpoint', '')
34
+ server_port = 443
35
+ collection_name = instance.name
36
+ response = process_db_operations(
37
+ response, span, start_time, gen_ai_endpoint,
38
+ version, environment, application_name, capture_message_content,
39
+ metrics, disable_metrics, server_address, server_port,
40
+ collection_name, db_operation, kwargs, args
41
+ )
42
+
43
+ return response
225
44
 
226
45
  return wrapper
@@ -1,226 +1,45 @@
1
- # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, possibly-used-before-assignment, too-many-branches
2
1
  """
3
2
  Module for monitoring AstraDB.
4
3
  """
5
4
 
6
- import logging
7
- from opentelemetry.trace import SpanKind, Status, StatusCode
8
- from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
9
- from openlit.__helpers import handle_exception
5
+ import time
6
+ from opentelemetry.trace import SpanKind
7
+ from openlit.instrumentation.astra.utils import (
8
+ DB_OPERATION_MAP,
9
+ process_db_operations
10
+ )
10
11
  from openlit.semcov import SemanticConvetion
11
12
 
12
- # Initialize logger for logging potential issues and operations
13
- logger = logging.getLogger(__name__)
14
-
15
- def object_count(obj):
16
- """
17
- Counts Length of object if it exists, Else returns None
18
- """
19
-
20
- return len(obj) if obj else None
21
-
22
- def general_wrap(gen_ai_endpoint, version, environment, application_name,
13
+ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
23
14
  tracer, pricing_info, capture_message_content, metrics, disable_metrics):
24
15
  """
25
- Wraps a AstraDB operation to trace and log its execution metrics.
26
-
27
- This function is intended to wrap around AstraDB operations in order to
28
- measure their execution time, log relevant information, and trace the execution
29
- using OpenTelemetry. This helps in monitoring and debugging operations within
30
- the AstraDB space.
31
-
32
- Parameters:
33
- - operation (str): The specific AstraDB operation being monitored.
34
- Examples include 'create_index', 'query', 'upsert', etc.
35
- - version (str): The version of the application interfacing with AstraDB.
36
- - environment (str): The deployment environment, such as 'production' or 'development'.
37
- - application_name (str): The name of the application performing the AstraDB operation.
38
- - tracer (opentelemetry.trace.Tracer): An object used for OpenTelemetry tracing.
39
- - pricing_info (dict): Information about pricing, not used in current implementation.
40
- - capture_message_content (bool): A flag indicating whether the content of responses should be traced.
41
-
42
- Returns:
43
- - function: A decorator function that, when applied, wraps the target function with
44
- additional functionality for tracing and logging AstraDB operations.
16
+ Generates a telemetry wrapper for VectorDB function call
45
17
  """
46
18
 
47
19
  async def wrapper(wrapped, instance, args, kwargs):
48
20
  """
49
- Executes the wrapped AstraDB operation, adding tracing and logging.
50
-
51
- This inner wrapper function captures the execution of AstraDB operations,
52
- annotating the operation with relevant metrics and tracing information, and
53
- ensuring any exceptions are caught and logged appropriately.
54
-
55
- Parameters:
56
- - wrapped (Callable): The AstraDB operation to be wrapped and executed.
57
- - instance (object): The instance on which the operation is called (for class methods).
58
- - args (tuple): Positional arguments for the AstraDB operation.
59
- - kwargs (dict): Keyword arguments for the AstraDB operation.
60
-
61
- Returns:
62
- - Any: The result of executing the wrapped AstraDB operation.
21
+ Wraps the VectorDB function call.
63
22
  """
64
23
 
65
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
66
- response = await wrapped(*args, **kwargs)
67
-
68
- try:
69
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
70
- span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
71
- gen_ai_endpoint)
72
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
73
- environment)
74
- span.set_attribute(SERVICE_NAME,
75
- application_name)
76
- span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
77
- SemanticConvetion.GEN_AI_OPERATION_TYPE_VECTORDB)
78
- span.set_attribute(SemanticConvetion.DB_SYSTEM,
79
- SemanticConvetion.DB_SYSTEM_ASTRA)
80
-
81
- if gen_ai_endpoint == "astra.create_collection":
82
- db_operation = SemanticConvetion.DB_OPERATION_CREATE_COLLECTION
83
- span.set_attribute(SemanticConvetion.DB_OPERATION,
84
- db_operation)
85
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
86
- response.name)
87
- span.set_attribute(SemanticConvetion.DB_INDEX_DIMENSION,
88
- kwargs.get("dimension", ""))
89
- span.set_attribute(SemanticConvetion.DB_INDEX_METRIC,
90
- str(kwargs.get("metric", "")))
91
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
92
- instance.api_endpoint)
24
+ db_operation = DB_OPERATION_MAP.get(gen_ai_endpoint, "UNKNOWN")
25
+ if db_operation == SemanticConvetion.DB_OPERATION_REPLACE and kwargs.get('upsert'):
26
+ db_operation = SemanticConvetion.DB_OPERATION_UPSERT
93
27
 
94
- elif gen_ai_endpoint == "astra.drop_collection":
95
- db_operation = SemanticConvetion.DB_OPERATION_DELETE_COLLECTION
96
- span.set_attribute(SemanticConvetion.DB_OPERATION,
97
- db_operation)
98
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
99
- kwargs.get("name_or_collection", ""))
100
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
101
- instance.api_endpoint)
28
+ span_name = f"{db_operation} {instance.name}"
102
29
 
103
- elif gen_ai_endpoint == "astra.insert_one":
104
- db_operation = SemanticConvetion.DB_OPERATION_INSERT
105
- span.set_attribute(SemanticConvetion.DB_OPERATION,
106
- db_operation)
107
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
108
- instance.name)
109
- span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT,
110
- 1)
111
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
112
- instance.database.api_endpoint)
113
- span.set_attribute(SemanticConvetion.DB_OPERATION_ID,
114
- response.inserted_id)
115
-
116
- elif gen_ai_endpoint == "astra.insert_many":
117
- db_operation = SemanticConvetion.DB_OPERATION_INSERT
118
- span.set_attribute(SemanticConvetion.DB_OPERATION,
119
- db_operation)
120
- span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT,
121
- object_count(args[0]))
122
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
123
- instance.name)
124
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
125
- instance.database.api_endpoint)
126
-
127
- elif gen_ai_endpoint in ["astra.update_one", "astra.update_many"]:
128
- db_operation = SemanticConvetion.DB_OPERATION_UPDATE
129
- span.set_attribute(SemanticConvetion.DB_OPERATION,
130
- db_operation)
131
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
132
- instance.name)
133
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
134
- instance.database.api_endpoint)
135
- span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT,
136
- response.update_info.get("nModified", 0))
137
-
138
- elif gen_ai_endpoint == "astra.find_one_and_update":
139
- db_operation = SemanticConvetion.DB_OPERATION_UPDATE
140
- span.set_attribute(SemanticConvetion.DB_OPERATION,
141
- db_operation)
142
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
143
- instance.name)
144
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
145
- instance.database.api_endpoint)
146
-
147
- elif gen_ai_endpoint == "astra.find":
148
- db_operation = SemanticConvetion.DB_OPERATION_QUERY
149
- span.set_attribute(SemanticConvetion.DB_OPERATION,
150
- db_operation)
151
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
152
- instance.name)
153
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
154
- instance.database.api_endpoint)
155
- span.set_attribute(SemanticConvetion.DB_STATEMENT,
156
- str(args))
157
-
158
- elif gen_ai_endpoint == "astra.replace_one":
159
- if kwargs.get("upsert") is True:
160
- db_operation = SemanticConvetion.DB_OPERATION_UPSERT
161
- else:
162
- db_operation = SemanticConvetion.DB_OPERATION_REPLACE
163
- span.set_attribute(SemanticConvetion.DB_OPERATION,
164
- db_operation)
165
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
166
- instance.name)
167
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
168
- instance.database.api_endpoint)
169
- span.set_attribute(SemanticConvetion.DB_STATEMENT,
170
- str(args))
171
-
172
- elif gen_ai_endpoint in ["astra.delete_one", "astra.delete_many"]:
173
- db_operation = SemanticConvetion.DB_OPERATION_DELETE
174
- span.set_attribute(SemanticConvetion.DB_OPERATION,
175
- db_operation)
176
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
177
- instance.name)
178
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
179
- instance.database.api_endpoint)
180
- span.set_attribute(SemanticConvetion.DB_STATEMENT,
181
- str(args))
182
- span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT,
183
- response.deleted_count)
184
-
185
- elif gen_ai_endpoint == "astra.find_one_and_delete":
186
- db_operation = SemanticConvetion.DB_OPERATION_DELETE
187
- span.set_attribute(SemanticConvetion.DB_OPERATION,
188
- db_operation)
189
- span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
190
- instance.name)
191
- span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
192
- instance.database.api_endpoint)
193
- span.set_attribute(SemanticConvetion.DB_STATEMENT,
194
- str(args))
195
-
196
- span.set_status(Status(StatusCode.OK))
197
-
198
- if disable_metrics is False:
199
- attributes = {
200
- TELEMETRY_SDK_NAME:
201
- "openlit",
202
- SERVICE_NAME:
203
- application_name,
204
- SemanticConvetion.DB_SYSTEM:
205
- SemanticConvetion.DB_SYSTEM_ASTRA,
206
- DEPLOYMENT_ENVIRONMENT:
207
- environment,
208
- SemanticConvetion.GEN_AI_OPERATION:
209
- SemanticConvetion.GEN_AI_OPERATION_TYPE_VECTORDB,
210
- SemanticConvetion.DB_OPERATION:
211
- db_operation
212
- }
213
-
214
- metrics["db_requests"].add(1, attributes)
215
-
216
- # Return original response
217
- return response
218
-
219
- except Exception as e:
220
- handle_exception(span, e)
221
- logger.error("Error in trace creation: %s", e)
222
-
223
- # Return original response
224
- return response
30
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
31
+ start_time = time.time()
32
+ response = await wrapped(*args, **kwargs)
33
+ server_address = getattr(getattr(instance, 'database', instance), 'api_endpoint', '')
34
+ server_port = 443
35
+ collection_name = instance.name
36
+ response = process_db_operations(
37
+ response, span, start_time, gen_ai_endpoint,
38
+ version, environment, application_name, capture_message_content,
39
+ metrics, disable_metrics, server_address, server_port,
40
+ collection_name, db_operation, kwargs, args
41
+ )
42
+
43
+ return response
225
44
 
226
45
  return wrapper
@@ -0,0 +1,102 @@
1
+ """
2
+ Astra OpenTelemetry instrumentation utility functions
3
+ """
4
+
5
+ import time
6
+ import logging
7
+ from opentelemetry.trace import Status, StatusCode
8
+ from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
9
+ from openlit.__helpers import handle_exception
10
+ from openlit.semcov import SemanticConvetion
11
+
12
+ # Initialize logger for logging potential issues and operations
13
+ logger = logging.getLogger(__name__)
14
+
15
+ def object_count(obj):
16
+ """Counts Length of object if it exists, Else returns None."""
17
+ return len(obj) if isinstance(obj, list) else 1
18
+
19
+ DB_OPERATION_MAP = {
20
+ 'astra.create_collection': SemanticConvetion.DB_OPERATION_CREATE_COLLECTION,
21
+ 'astra.drop_collection': SemanticConvetion.DB_OPERATION_DELETE_COLLECTION,
22
+ 'astra.insert': SemanticConvetion.DB_OPERATION_INSERT,
23
+ 'astra.update': SemanticConvetion.DB_OPERATION_UPDATE,
24
+ 'astra.find': SemanticConvetion.DB_OPERATION_SELECT,
25
+ 'astra.find_one_and_update': SemanticConvetion.DB_OPERATION_REPLACE,
26
+ 'astra.replace_one': SemanticConvetion.DB_OPERATION_REPLACE,
27
+ 'astra.delete': SemanticConvetion.DB_OPERATION_DELETE,
28
+ 'astra.find_one_and_delete': SemanticConvetion.DB_OPERATION_FIND_AND_DELETE
29
+ }
30
+
31
+ def process_db_operations(response, span, start_time, gen_ai_endpoint,
32
+ version, environment, application_name,
33
+ capture_message_content, metrics, disable_metrics, server_address,
34
+ server_port, collection_name, db_operation, kwargs, args):
35
+ """
36
+ Process DB operation and generate Telemetry
37
+ """
38
+
39
+ end_time = time.time()
40
+
41
+ try:
42
+ span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
43
+ span.set_attribute(SemanticConvetion.GEN_AI_OPERATION, SemanticConvetion.GEN_AI_OPERATION_TYPE_VECTORDB)
44
+ span.set_attribute(SemanticConvetion.DB_SYSTEM_NAME, SemanticConvetion.DB_SYSTEM_ASTRA)
45
+ span.set_attribute(SemanticConvetion.DB_CLIENT_OPERATION_DURATION, end_time - start_time)
46
+ span.set_attribute(SemanticConvetion.SERVER_ADDRESS, server_address)
47
+ span.set_attribute(SemanticConvetion.SERVER_PORT, server_port)
48
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
49
+ span.set_attribute(SERVICE_NAME, application_name)
50
+ span.set_attribute(SemanticConvetion.DB_OPERATION_NAME, db_operation)
51
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME, collection_name)
52
+ span.set_attribute(SemanticConvetion.DB_SDK_VERSION, version)
53
+
54
+ if db_operation == SemanticConvetion.DB_OPERATION_CREATE_COLLECTION:
55
+ span.set_attribute(SemanticConvetion.DB_NAMESPACE, response.keyspace)
56
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME, response.name)
57
+ span.set_attribute(SemanticConvetion.DB_INDEX_DIMENSION, kwargs.get('dimension', ''))
58
+ span.set_attribute(SemanticConvetion.DB_INDEX_METRIC, str(kwargs.get('metric', '')))
59
+
60
+ if db_operation == SemanticConvetion.DB_OPERATION_INSERT:
61
+ span.set_attribute(SemanticConvetion.DB_DOCUMENTS_COUNT, object_count(args[0]))
62
+ span.set_attribute(SemanticConvetion.DB_QUERY_TEXT, str(args[0] or kwargs.get('documents', {})))
63
+
64
+ elif db_operation == SemanticConvetion.DB_OPERATION_UPDATE:
65
+ span.set_attribute(SemanticConvetion.DB_RESPONSE_RETURNED_ROWS, response.update_info.get('nModified', 0))
66
+ span.set_attribute(SemanticConvetion.DB_QUERY_TEXT, str(args[1] or kwargs.get('update', {})))
67
+
68
+ elif db_operation == SemanticConvetion.DB_OPERATION_DELETE:
69
+ span.set_attribute(SemanticConvetion.DB_RESPONSE_RETURNED_ROWS, response.deleted_count)
70
+ span.set_attribute(SemanticConvetion.DB_QUERY_TEXT, str(args[0] or kwargs.get('filter', {})))
71
+
72
+ elif db_operation in [
73
+ SemanticConvetion.DB_OPERATION_SELECT,
74
+ SemanticConvetion.DB_OPERATION_FIND_AND_DELETE,
75
+ SemanticConvetion.DB_OPERATION_REPLACE
76
+ ]:
77
+ span.set_attribute(SemanticConvetion.DB_QUERY_TEXT, str(args or kwargs.get('filter', {})))
78
+
79
+ span.set_status(Status(StatusCode.OK))
80
+
81
+ if not disable_metrics:
82
+ attributes = {
83
+ TELEMETRY_SDK_NAME: 'openlit',
84
+ SERVICE_NAME: application_name,
85
+ SemanticConvetion.DB_SYSTEM_NAME: SemanticConvetion.DB_SYSTEM_ASTRA,
86
+ DEPLOYMENT_ENVIRONMENT: environment,
87
+ SemanticConvetion.GEN_AI_OPERATION: SemanticConvetion.GEN_AI_OPERATION_TYPE_VECTORDB,
88
+ SemanticConvetion.DB_OPERATION_NAME: db_operation
89
+ }
90
+
91
+ metrics['db_requests'].add(1, attributes)
92
+ metrics['db_client_operation_duration'].record(end_time - start_time, attributes)
93
+
94
+ # Return original response
95
+ return response
96
+
97
+ except Exception as e:
98
+ handle_exception(span, e)
99
+ logger.error('Error in trace creation: %s', e)
100
+
101
+ # Return original response
102
+ return response