openlit 1.31.1__py3-none-any.whl → 1.32.3__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.
- openlit/__init__.py +18 -0
- openlit/instrumentation/astra/__init__.py +179 -0
- openlit/instrumentation/astra/astra.py +226 -0
- openlit/instrumentation/astra/async_astra.py +226 -0
- openlit/instrumentation/crewai/crewai.py +2 -0
- openlit/instrumentation/julep/__init__.py +80 -0
- openlit/instrumentation/julep/async_julep.py +111 -0
- openlit/instrumentation/julep/julep.py +112 -0
- openlit/instrumentation/mem0/__init__.py +79 -0
- openlit/instrumentation/mem0/mem0.py +115 -0
- openlit/instrumentation/multion/__init__.py +80 -0
- openlit/instrumentation/multion/async_multion.py +131 -0
- openlit/instrumentation/multion/multion.py +131 -0
- openlit/instrumentation/premai/__init__.py +51 -0
- openlit/instrumentation/premai/premai.py +507 -0
- openlit/instrumentation/reka/__init__.py +54 -0
- openlit/instrumentation/reka/async_reka.py +159 -0
- openlit/instrumentation/reka/reka.py +159 -0
- openlit/semcov/__init__.py +14 -0
- {openlit-1.31.1.dist-info → openlit-1.32.3.dist-info}/METADATA +12 -6
- {openlit-1.31.1.dist-info → openlit-1.32.3.dist-info}/RECORD +23 -7
- {openlit-1.31.1.dist-info → openlit-1.32.3.dist-info}/LICENSE +0 -0
- {openlit-1.31.1.dist-info → openlit-1.32.3.dist-info}/WHEEL +0 -0
@@ -0,0 +1,226 @@
|
|
1
|
+
# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, possibly-used-before-assignment, too-many-branches
|
2
|
+
"""
|
3
|
+
Module for monitoring AstraDB.
|
4
|
+
"""
|
5
|
+
|
6
|
+
import logging
|
7
|
+
from opentelemetry.trace import SpanKind, Status, StatusCode
|
8
|
+
from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
|
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
|
+
"""
|
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,
|
23
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics):
|
24
|
+
"""
|
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
|
+
- trace_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.
|
45
|
+
"""
|
46
|
+
|
47
|
+
async def wrapper(wrapped, instance, args, kwargs):
|
48
|
+
"""
|
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.
|
63
|
+
"""
|
64
|
+
|
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(SemanticConvetion.GEN_AI_ENVIRONMENT,
|
73
|
+
environment)
|
74
|
+
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
|
75
|
+
application_name)
|
76
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
|
77
|
+
SemanticConvetion.GEN_AI_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
|
+
kwargs.get("metric", ""))
|
91
|
+
span.set_attribute(SemanticConvetion.DB_OPERATION_API_ENDPOINT,
|
92
|
+
instance.api_endpoint)
|
93
|
+
|
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)
|
102
|
+
|
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
|
+
SemanticConvetion.GEN_AI_APPLICATION_NAME:
|
203
|
+
application_name,
|
204
|
+
SemanticConvetion.DB_SYSTEM:
|
205
|
+
SemanticConvetion.DB_SYSTEM_ASTRA,
|
206
|
+
SemanticConvetion.GEN_AI_ENVIRONMENT:
|
207
|
+
environment,
|
208
|
+
SemanticConvetion.GEN_AI_TYPE:
|
209
|
+
SemanticConvetion.GEN_AI_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
|
225
|
+
|
226
|
+
return wrapper
|
@@ -75,6 +75,8 @@ def crew_wrap(gen_ai_endpoint, version, environment, application_name,
|
|
75
75
|
SemanticConvetion.GEN_AI_TYPE_AGENT)
|
76
76
|
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
|
77
77
|
gen_ai_endpoint)
|
78
|
+
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
|
79
|
+
application_name)
|
78
80
|
|
79
81
|
instance_class = instance.__class__.__name__
|
80
82
|
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
|
2
|
+
"""Initializer of Auto Instrumentation of Juelp Functions"""
|
3
|
+
|
4
|
+
from typing import Collection
|
5
|
+
import importlib.metadata
|
6
|
+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
7
|
+
from wrapt import wrap_function_wrapper
|
8
|
+
|
9
|
+
from openlit.instrumentation.julep.julep import (
|
10
|
+
wrap_julep
|
11
|
+
)
|
12
|
+
|
13
|
+
from openlit.instrumentation.julep.async_julep import (
|
14
|
+
async_wrap_julep
|
15
|
+
)
|
16
|
+
|
17
|
+
_instruments = ("julep >= 1.42.0",)
|
18
|
+
|
19
|
+
class JulepInstrumentor(BaseInstrumentor):
|
20
|
+
"""
|
21
|
+
An instrumentor for Julep's client library.
|
22
|
+
"""
|
23
|
+
|
24
|
+
def instrumentation_dependencies(self) -> Collection[str]:
|
25
|
+
return _instruments
|
26
|
+
|
27
|
+
def _instrument(self, **kwargs):
|
28
|
+
application_name = kwargs.get("application_name", "default_application")
|
29
|
+
environment = kwargs.get("environment", "default_environment")
|
30
|
+
tracer = kwargs.get("tracer")
|
31
|
+
metrics = kwargs.get("metrics_dict")
|
32
|
+
pricing_info = kwargs.get("pricing_info", {})
|
33
|
+
trace_content = kwargs.get("trace_content", False)
|
34
|
+
disable_metrics = kwargs.get("disable_metrics")
|
35
|
+
version = importlib.metadata.version("julep")
|
36
|
+
|
37
|
+
# sync
|
38
|
+
wrap_function_wrapper(
|
39
|
+
"julep.resources.agents.agents",
|
40
|
+
"AgentsResource.create",
|
41
|
+
wrap_julep("julep.agents_create", version, environment, application_name,
|
42
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
43
|
+
)
|
44
|
+
wrap_function_wrapper(
|
45
|
+
"julep.resources.tasks",
|
46
|
+
"TasksResource.create",
|
47
|
+
wrap_julep("julep.task_create", version, environment, application_name,
|
48
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
49
|
+
)
|
50
|
+
wrap_function_wrapper(
|
51
|
+
"julep.resources.executions.executions",
|
52
|
+
"ExecutionsResource.create",
|
53
|
+
wrap_julep("julep.execution_create", version, environment, application_name,
|
54
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
55
|
+
)
|
56
|
+
|
57
|
+
# async
|
58
|
+
wrap_function_wrapper(
|
59
|
+
"julep.resources.agents.agents",
|
60
|
+
"AsyncAgentsResource.create",
|
61
|
+
async_wrap_julep("julep.agents_create", version, environment, application_name,
|
62
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
63
|
+
)
|
64
|
+
wrap_function_wrapper(
|
65
|
+
"julep.resources.tasks",
|
66
|
+
"AsyncTasksResource.create",
|
67
|
+
async_wrap_julep("julep.task_create", version, environment, application_name,
|
68
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
69
|
+
)
|
70
|
+
wrap_function_wrapper(
|
71
|
+
"julep.resources.executions.executions",
|
72
|
+
"AsyncExecutionsResource.create",
|
73
|
+
async_wrap_julep("julep.execution_create", version, environment, application_name,
|
74
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
75
|
+
)
|
76
|
+
|
77
|
+
|
78
|
+
def _uninstrument(self, **kwargs):
|
79
|
+
# Proper uninstrumentation logic to revert patched methods
|
80
|
+
pass
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument
|
2
|
+
"""
|
3
|
+
Module for monitoring Julep.
|
4
|
+
"""
|
5
|
+
|
6
|
+
import logging
|
7
|
+
from opentelemetry.trace import SpanKind, Status, StatusCode
|
8
|
+
from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
|
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 async_wrap_julep(gen_ai_endpoint, version, environment, application_name,
|
16
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics):
|
17
|
+
"""
|
18
|
+
Creates a wrapper around a function call to trace and log its execution metrics.
|
19
|
+
|
20
|
+
This function wraps any given function to measure its execution time,
|
21
|
+
log its operation, and trace its execution using OpenTelemetry.
|
22
|
+
|
23
|
+
Parameters:
|
24
|
+
- gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.
|
25
|
+
- version (str): The version of the application.
|
26
|
+
- environment (str): The deployment environment (e.g., 'production', 'development').
|
27
|
+
- application_name (str): Name of the application.
|
28
|
+
- tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.
|
29
|
+
- pricing_info (dict): Information about the pricing for internal metrics (currently not used).
|
30
|
+
- trace_content (bool): Flag indicating whether to trace the content of the response.
|
31
|
+
|
32
|
+
Returns:
|
33
|
+
- function: A higher-order function that takes a function 'wrapped' and returns
|
34
|
+
a new function that wraps 'wrapped' with additional tracing and logging.
|
35
|
+
"""
|
36
|
+
|
37
|
+
async def wrapper(wrapped, instance, args, kwargs):
|
38
|
+
"""
|
39
|
+
An inner wrapper function that executes the wrapped function, measures execution
|
40
|
+
time, and records trace data using OpenTelemetry.
|
41
|
+
|
42
|
+
Parameters:
|
43
|
+
- wrapped (Callable): The original function that this wrapper will execute.
|
44
|
+
- instance (object): The instance to which the wrapped function belongs. This
|
45
|
+
is used for instance methods. For static and classmethods,
|
46
|
+
this may be None.
|
47
|
+
- args (tuple): Positional arguments passed to the wrapped function.
|
48
|
+
- kwargs (dict): Keyword arguments passed to the wrapped function.
|
49
|
+
|
50
|
+
Returns:
|
51
|
+
- The result of the wrapped function call.
|
52
|
+
|
53
|
+
The wrapper initiates a span with the provided tracer, sets various attributes
|
54
|
+
on the span based on the function's execution and response, and ensures
|
55
|
+
errors are handled and logged appropriately.
|
56
|
+
"""
|
57
|
+
|
58
|
+
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
|
59
|
+
response = await wrapped(*args, **kwargs)
|
60
|
+
|
61
|
+
try:
|
62
|
+
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
63
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
|
64
|
+
gen_ai_endpoint)
|
65
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
66
|
+
SemanticConvetion.GEN_AI_SYSTEM_JULEP)
|
67
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
|
68
|
+
environment)
|
69
|
+
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
|
70
|
+
application_name)
|
71
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
|
72
|
+
SemanticConvetion.GEN_AI_TYPE_AGENT)
|
73
|
+
|
74
|
+
if gen_ai_endpoint == "julep.agents_create":
|
75
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_ID,
|
76
|
+
response.id)
|
77
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_ROLE,
|
78
|
+
kwargs.get("name", ""))
|
79
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
|
80
|
+
kwargs.get("model", "gpt-4-turbo"))
|
81
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_CONTEXT,
|
82
|
+
kwargs.get("about", ""))
|
83
|
+
|
84
|
+
elif gen_ai_endpoint == "julep.task_create":
|
85
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_TOOLS,
|
86
|
+
str(kwargs.get("tools", "")))
|
87
|
+
|
88
|
+
elif gen_ai_endpoint == "julep.execution_create":
|
89
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_TASK_ID,
|
90
|
+
kwargs.get("task_id", ""))
|
91
|
+
if trace_content:
|
92
|
+
span.add_event(
|
93
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
94
|
+
attributes={
|
95
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT:str(kwargs.get("input", "")),
|
96
|
+
},
|
97
|
+
)
|
98
|
+
|
99
|
+
span.set_status(Status(StatusCode.OK))
|
100
|
+
|
101
|
+
# Return original response
|
102
|
+
return response
|
103
|
+
|
104
|
+
except Exception as e:
|
105
|
+
handle_exception(span, e)
|
106
|
+
logger.error("Error in trace creation: %s", e)
|
107
|
+
|
108
|
+
# Return original response
|
109
|
+
return response
|
110
|
+
|
111
|
+
return wrapper
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument
|
2
|
+
"""
|
3
|
+
Module for monitoring Julep.
|
4
|
+
"""
|
5
|
+
|
6
|
+
import logging
|
7
|
+
from opentelemetry.trace import SpanKind, Status, StatusCode
|
8
|
+
from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
|
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 wrap_julep(gen_ai_endpoint, version, environment, application_name,
|
16
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics):
|
17
|
+
"""
|
18
|
+
Creates a wrapper around a function call to trace and log its execution metrics.
|
19
|
+
|
20
|
+
This function wraps any given function to measure its execution time,
|
21
|
+
log its operation, and trace its execution using OpenTelemetry.
|
22
|
+
|
23
|
+
Parameters:
|
24
|
+
- gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.
|
25
|
+
- version (str): The version of the Langchain application.
|
26
|
+
- environment (str): The deployment environment (e.g., 'production', 'development').
|
27
|
+
- application_name (str): Name of the Langchain application.
|
28
|
+
- tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.
|
29
|
+
- pricing_info (dict): Information about the pricing for internal metrics (currently not used).
|
30
|
+
- trace_content (bool): Flag indicating whether to trace the content of the response.
|
31
|
+
|
32
|
+
Returns:
|
33
|
+
- function: A higher-order function that takes a function 'wrapped' and returns
|
34
|
+
a new function that wraps 'wrapped' with additional tracing and logging.
|
35
|
+
"""
|
36
|
+
|
37
|
+
def wrapper(wrapped, instance, args, kwargs):
|
38
|
+
"""
|
39
|
+
An inner wrapper function that executes the wrapped function, measures execution
|
40
|
+
time, and records trace data using OpenTelemetry.
|
41
|
+
|
42
|
+
Parameters:
|
43
|
+
- wrapped (Callable): The original function that this wrapper will execute.
|
44
|
+
- instance (object): The instance to which the wrapped function belongs. This
|
45
|
+
is used for instance methods. For static and classmethods,
|
46
|
+
this may be None.
|
47
|
+
- args (tuple): Positional arguments passed to the wrapped function.
|
48
|
+
- kwargs (dict): Keyword arguments passed to the wrapped function.
|
49
|
+
|
50
|
+
Returns:
|
51
|
+
- The result of the wrapped function call.
|
52
|
+
|
53
|
+
The wrapper initiates a span with the provided tracer, sets various attributes
|
54
|
+
on the span based on the function's execution and response, and ensures
|
55
|
+
errors are handled and logged appropriately.
|
56
|
+
"""
|
57
|
+
|
58
|
+
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
|
59
|
+
response = wrapped(*args, **kwargs)
|
60
|
+
|
61
|
+
try:
|
62
|
+
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
63
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
|
64
|
+
gen_ai_endpoint)
|
65
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
66
|
+
SemanticConvetion.GEN_AI_SYSTEM_JULEP)
|
67
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
|
68
|
+
environment)
|
69
|
+
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
|
70
|
+
application_name)
|
71
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
|
72
|
+
SemanticConvetion.GEN_AI_TYPE_AGENT)
|
73
|
+
|
74
|
+
if gen_ai_endpoint == "julep.agents_create":
|
75
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_ID,
|
76
|
+
response.id)
|
77
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_ROLE,
|
78
|
+
kwargs.get("name", ""))
|
79
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
|
80
|
+
kwargs.get("model", "gpt-4-turbo"))
|
81
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_CONTEXT,
|
82
|
+
kwargs.get("about", ""))
|
83
|
+
|
84
|
+
elif gen_ai_endpoint == "julep.task_create":
|
85
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_TOOLS,
|
86
|
+
str(kwargs.get("tools", "")))
|
87
|
+
|
88
|
+
elif gen_ai_endpoint == "julep.execution_create":
|
89
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_TASK_ID,
|
90
|
+
kwargs.get("task_id", ""))
|
91
|
+
if trace_content:
|
92
|
+
span.add_event(
|
93
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
94
|
+
attributes={
|
95
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT:str(kwargs.get("input", "")),
|
96
|
+
},
|
97
|
+
)
|
98
|
+
|
99
|
+
|
100
|
+
span.set_status(Status(StatusCode.OK))
|
101
|
+
|
102
|
+
# Return original response
|
103
|
+
return response
|
104
|
+
|
105
|
+
except Exception as e:
|
106
|
+
handle_exception(span, e)
|
107
|
+
logger.error("Error in trace creation: %s", e)
|
108
|
+
|
109
|
+
# Return original response
|
110
|
+
return response
|
111
|
+
|
112
|
+
return wrapper
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
|
2
|
+
"""Initializer of Auto Instrumentation of mem0 Functions"""
|
3
|
+
from typing import Collection
|
4
|
+
import importlib.metadata
|
5
|
+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
6
|
+
from wrapt import wrap_function_wrapper
|
7
|
+
|
8
|
+
from openlit.instrumentation.mem0.mem0 import mem0_wrap
|
9
|
+
|
10
|
+
_instruments = ("mem0ai >= 0.1.32",)
|
11
|
+
|
12
|
+
WRAPPED_METHODS = [
|
13
|
+
{
|
14
|
+
"package": "mem0",
|
15
|
+
"object": "Memory.add",
|
16
|
+
"endpoint": "mem0.memory_add",
|
17
|
+
"wrapper": mem0_wrap,
|
18
|
+
},
|
19
|
+
{
|
20
|
+
"package": "mem0",
|
21
|
+
"object": "Memory.get_all",
|
22
|
+
"endpoint": "mem0.memory_get_all",
|
23
|
+
"wrapper": mem0_wrap,
|
24
|
+
},
|
25
|
+
{
|
26
|
+
"package": "mem0",
|
27
|
+
"object": "Memory.get",
|
28
|
+
"endpoint": "mem0.memory_get",
|
29
|
+
"wrapper": mem0_wrap,
|
30
|
+
},
|
31
|
+
{
|
32
|
+
"package": "mem0",
|
33
|
+
"object": "Memory.search",
|
34
|
+
"endpoint": "mem0.memory_search",
|
35
|
+
"wrapper": mem0_wrap,
|
36
|
+
},
|
37
|
+
{
|
38
|
+
"package": "mem0",
|
39
|
+
"object": "Memory.update",
|
40
|
+
"endpoint": "mem0.memory_update",
|
41
|
+
"wrapper": mem0_wrap,
|
42
|
+
},
|
43
|
+
{
|
44
|
+
"package": "mem0",
|
45
|
+
"object": "Memory.update",
|
46
|
+
"endpoint": "mem0.memory_update",
|
47
|
+
"wrapper": mem0_wrap,
|
48
|
+
},
|
49
|
+
]
|
50
|
+
|
51
|
+
class Mem0Instrumentor(BaseInstrumentor):
|
52
|
+
"""An instrumentor for mem0's client library."""
|
53
|
+
|
54
|
+
def instrumentation_dependencies(self) -> Collection[str]:
|
55
|
+
return _instruments
|
56
|
+
|
57
|
+
def _instrument(self, **kwargs):
|
58
|
+
application_name = kwargs.get("application_name")
|
59
|
+
environment = kwargs.get("environment")
|
60
|
+
tracer = kwargs.get("tracer")
|
61
|
+
pricing_info = kwargs.get("pricing_info")
|
62
|
+
trace_content = kwargs.get("trace_content")
|
63
|
+
version = importlib.metadata.version("mem0ai")
|
64
|
+
|
65
|
+
for wrapped_method in WRAPPED_METHODS:
|
66
|
+
wrap_package = wrapped_method.get("package")
|
67
|
+
wrap_object = wrapped_method.get("object")
|
68
|
+
gen_ai_endpoint = wrapped_method.get("endpoint")
|
69
|
+
wrapper = wrapped_method.get("wrapper")
|
70
|
+
wrap_function_wrapper(
|
71
|
+
wrap_package,
|
72
|
+
wrap_object,
|
73
|
+
wrapper(gen_ai_endpoint, version, environment, application_name,
|
74
|
+
tracer, pricing_info, trace_content),
|
75
|
+
)
|
76
|
+
|
77
|
+
@staticmethod
|
78
|
+
def _uninstrument(self, **kwargs):
|
79
|
+
pass
|