openlit 1.31.0__py3-none-any.whl → 1.32.2__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 +21 -0
- openlit/instrumentation/crewai/crewai.py +2 -0
- openlit/instrumentation/dynamiq/__init__.py +64 -0
- openlit/instrumentation/dynamiq/dynamiq.py +108 -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/phidata/__init__.py +42 -0
- openlit/instrumentation/phidata/phidata.py +98 -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 +16 -0
- {openlit-1.31.0.dist-info → openlit-1.32.2.dist-info}/METADATA +13 -7
- {openlit-1.31.0.dist-info → openlit-1.32.2.dist-info}/RECORD +24 -7
- {openlit-1.31.0.dist-info → openlit-1.32.2.dist-info}/LICENSE +0 -0
- {openlit-1.31.0.dist-info → openlit-1.32.2.dist-info}/WHEEL +0 -0
@@ -0,0 +1,115 @@
|
|
1
|
+
# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument
|
2
|
+
"""
|
3
|
+
Module for monitoring mem0 applications.
|
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 mem0_wrap(gen_ai_endpoint, version, environment, application_name,
|
16
|
+
tracer, pricing_info, trace_content):
|
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 mem0 application.
|
26
|
+
- environment (str): The deployment environment (e.g., 'production', 'development').
|
27
|
+
- application_name (str): Name of the mem0 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
|
+
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
|
58
|
+
response = wrapped(*args, **kwargs)
|
59
|
+
|
60
|
+
try:
|
61
|
+
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
62
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
63
|
+
SemanticConvetion.GEN_AI_SYSTEM_MEM0)
|
64
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
|
65
|
+
gen_ai_endpoint)
|
66
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
|
67
|
+
environment)
|
68
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
|
69
|
+
SemanticConvetion.GEN_AI_TYPE_FRAMEWORK)
|
70
|
+
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
|
71
|
+
application_name)
|
72
|
+
|
73
|
+
if gen_ai_endpoint == "mem0.memory_add":
|
74
|
+
span.set_attribute(SemanticConvetion.DB_METADATA,
|
75
|
+
str(kwargs.get('metadata', '')))
|
76
|
+
if response:
|
77
|
+
span.set_attribute(SemanticConvetion.GEN_AI_DATA_SOURCES,
|
78
|
+
len(response))
|
79
|
+
|
80
|
+
elif gen_ai_endpoint == "mem0.memory_get_all":
|
81
|
+
if response:
|
82
|
+
span.set_attribute(SemanticConvetion.GEN_AI_DATA_SOURCES,
|
83
|
+
len(response))
|
84
|
+
|
85
|
+
elif gen_ai_endpoint == "mem0.memory_get":
|
86
|
+
if response:
|
87
|
+
span.set_attribute(SemanticConvetion.GEN_AI_DATA_SOURCES,
|
88
|
+
len(response))
|
89
|
+
|
90
|
+
elif gen_ai_endpoint == "mem0.memory_search":
|
91
|
+
span.set_attribute(SemanticConvetion.DB_STATEMENT,
|
92
|
+
kwargs.get("query", ""))
|
93
|
+
|
94
|
+
elif gen_ai_endpoint == "mem0.memory_update":
|
95
|
+
span.set_attribute(SemanticConvetion.DB_UPDATE_ID,
|
96
|
+
kwargs.get("memory_id", ""))
|
97
|
+
|
98
|
+
elif gen_ai_endpoint == "mem0.memory_delete":
|
99
|
+
span.set_attribute(SemanticConvetion.DB_DELETE_ID,
|
100
|
+
kwargs.get("memory_id", ""))
|
101
|
+
|
102
|
+
|
103
|
+
span.set_status(Status(StatusCode.OK))
|
104
|
+
|
105
|
+
# Return original response
|
106
|
+
return response
|
107
|
+
|
108
|
+
except Exception as e:
|
109
|
+
handle_exception(span, e)
|
110
|
+
logger.error("Error in trace creation: %s", e)
|
111
|
+
|
112
|
+
# Return original response
|
113
|
+
return response
|
114
|
+
|
115
|
+
return wrapper
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
|
2
|
+
"""Initializer of Auto Instrumentation of MultiOn 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.multion.multion import (
|
10
|
+
multion_wrap
|
11
|
+
)
|
12
|
+
|
13
|
+
from openlit.instrumentation.multion.async_multion import (
|
14
|
+
async_multion_wrap
|
15
|
+
)
|
16
|
+
|
17
|
+
_instruments = ("multion >= 1.3.8",)
|
18
|
+
|
19
|
+
class MultiOnInstrumentor(BaseInstrumentor):
|
20
|
+
"""
|
21
|
+
An instrumentor for multion'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("multion")
|
36
|
+
|
37
|
+
# Synchronus
|
38
|
+
wrap_function_wrapper(
|
39
|
+
"multion.client",
|
40
|
+
"MultiOn.browse",
|
41
|
+
multion_wrap("multion.browse", version, environment, application_name,
|
42
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
43
|
+
)
|
44
|
+
wrap_function_wrapper(
|
45
|
+
"multion.client",
|
46
|
+
"MultiOn.retrieve",
|
47
|
+
multion_wrap("multion.retrieve", version, environment, application_name,
|
48
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
49
|
+
)
|
50
|
+
wrap_function_wrapper(
|
51
|
+
"multion.sessions.client",
|
52
|
+
"SessionsClient.create",
|
53
|
+
multion_wrap("multion.sessions.create", version, environment, application_name,
|
54
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
55
|
+
)
|
56
|
+
|
57
|
+
# Asynchronus
|
58
|
+
wrap_function_wrapper(
|
59
|
+
"multion.client",
|
60
|
+
"AsyncMultiOn.browse",
|
61
|
+
async_multion_wrap("multion.browse", version, environment, application_name,
|
62
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
63
|
+
)
|
64
|
+
wrap_function_wrapper(
|
65
|
+
"multion.client",
|
66
|
+
"AsyncMultiOn.retrieve",
|
67
|
+
async_multion_wrap("multion.retrieve", version, environment, application_name,
|
68
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
69
|
+
)
|
70
|
+
wrap_function_wrapper(
|
71
|
+
"multion.sessions.client",
|
72
|
+
"AsyncSessionsClient.create",
|
73
|
+
async_multion_wrap("multion.sessions.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,131 @@
|
|
1
|
+
# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, too-many-branches
|
2
|
+
"""
|
3
|
+
Module for monitoring multion calls.
|
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 (
|
10
|
+
handle_exception,
|
11
|
+
)
|
12
|
+
from openlit.semcov import SemanticConvetion
|
13
|
+
|
14
|
+
# Initialize logger for logging potential issues and operations
|
15
|
+
logger = logging.getLogger(__name__)
|
16
|
+
|
17
|
+
def async_multion_wrap(gen_ai_endpoint, version, environment, application_name,
|
18
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics):
|
19
|
+
"""
|
20
|
+
Generates a telemetry wrapper for chat completions to collect metrics.
|
21
|
+
|
22
|
+
Args:
|
23
|
+
gen_ai_endpoint: Endpoint identifier for logging and tracing.
|
24
|
+
version: Version of the monitoring package.
|
25
|
+
environment: Deployment environment (e.g., production, staging).
|
26
|
+
application_name: Name of the application using the multion Agent.
|
27
|
+
tracer: OpenTelemetry tracer for creating spans.
|
28
|
+
pricing_info: Information used for calculating the cost of multion usage.
|
29
|
+
trace_content: Flag indicating whether to trace the actual content.
|
30
|
+
|
31
|
+
Returns:
|
32
|
+
A function that wraps the chat completions method to add telemetry.
|
33
|
+
"""
|
34
|
+
|
35
|
+
async def wrapper(wrapped, instance, args, kwargs):
|
36
|
+
"""
|
37
|
+
Wraps the 'chat.completions' API call to add telemetry.
|
38
|
+
|
39
|
+
This collects metrics such as execution time, cost, and token usage, and handles errors
|
40
|
+
gracefully, adding details to the trace for observability.
|
41
|
+
|
42
|
+
Args:
|
43
|
+
wrapped: The original 'chat.completions' method to be wrapped.
|
44
|
+
instance: The instance of the class where the original method is defined.
|
45
|
+
args: Positional arguments for the 'chat.completions' method.
|
46
|
+
kwargs: Keyword arguments for the 'chat.completions' method.
|
47
|
+
|
48
|
+
Returns:
|
49
|
+
The response from the original 'chat.completions' method.
|
50
|
+
"""
|
51
|
+
|
52
|
+
# pylint: disable=line-too-long
|
53
|
+
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
|
54
|
+
response = await wrapped(*args, **kwargs)
|
55
|
+
|
56
|
+
try:
|
57
|
+
# Set base span attribues
|
58
|
+
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
59
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
60
|
+
SemanticConvetion.GEN_AI_SYSTEM_MULTION)
|
61
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
|
62
|
+
SemanticConvetion.GEN_AI_TYPE_AGENT)
|
63
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
|
64
|
+
gen_ai_endpoint)
|
65
|
+
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
|
66
|
+
application_name)
|
67
|
+
|
68
|
+
if gen_ai_endpoint == "multion.browse":
|
69
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_BROWSE_URL,
|
70
|
+
kwargs.get("url", ""))
|
71
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_STEP_COUNT,
|
72
|
+
response.metadata.step_count)
|
73
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_RESPONSE_TIME,
|
74
|
+
response.metadata.processing_time)
|
75
|
+
|
76
|
+
if trace_content:
|
77
|
+
span.add_event(
|
78
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
79
|
+
attributes={
|
80
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT: kwargs.get("cmd", ""),
|
81
|
+
},
|
82
|
+
)
|
83
|
+
span.add_event(
|
84
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
85
|
+
attributes={
|
86
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.message,
|
87
|
+
},
|
88
|
+
)
|
89
|
+
elif gen_ai_endpoint == "multion.retrieve":
|
90
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_BROWSE_URL,
|
91
|
+
kwargs.get("url", ""))
|
92
|
+
|
93
|
+
if trace_content:
|
94
|
+
span.add_event(
|
95
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
96
|
+
attributes={
|
97
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT: kwargs.get("cmd", ""),
|
98
|
+
},
|
99
|
+
)
|
100
|
+
span.add_event(
|
101
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
102
|
+
attributes={
|
103
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.message,
|
104
|
+
},
|
105
|
+
)
|
106
|
+
|
107
|
+
elif gen_ai_endpoint == "multion.sessions.create":
|
108
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_BROWSE_URL,
|
109
|
+
kwargs.get("url", ""))
|
110
|
+
|
111
|
+
if trace_content:
|
112
|
+
span.add_event(
|
113
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
114
|
+
attributes={
|
115
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.message,
|
116
|
+
},
|
117
|
+
)
|
118
|
+
|
119
|
+
span.set_status(Status(StatusCode.OK))
|
120
|
+
|
121
|
+
# Return original response
|
122
|
+
return response
|
123
|
+
|
124
|
+
except Exception as e:
|
125
|
+
handle_exception(span, e)
|
126
|
+
logger.error("Error in trace creation: %s", e)
|
127
|
+
|
128
|
+
# Return original response
|
129
|
+
return response
|
130
|
+
|
131
|
+
return wrapper
|
@@ -0,0 +1,131 @@
|
|
1
|
+
# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, too-many-branches
|
2
|
+
"""
|
3
|
+
Module for monitoring multion calls.
|
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 (
|
10
|
+
handle_exception,
|
11
|
+
)
|
12
|
+
from openlit.semcov import SemanticConvetion
|
13
|
+
|
14
|
+
# Initialize logger for logging potential issues and operations
|
15
|
+
logger = logging.getLogger(__name__)
|
16
|
+
|
17
|
+
def multion_wrap(gen_ai_endpoint, version, environment, application_name,
|
18
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics):
|
19
|
+
"""
|
20
|
+
Generates a telemetry wrapper for chat completions to collect metrics.
|
21
|
+
|
22
|
+
Args:
|
23
|
+
gen_ai_endpoint: Endpoint identifier for logging and tracing.
|
24
|
+
version: Version of the monitoring package.
|
25
|
+
environment: Deployment environment (e.g., production, staging).
|
26
|
+
application_name: Name of the application using the multion Agent.
|
27
|
+
tracer: OpenTelemetry tracer for creating spans.
|
28
|
+
pricing_info: Information used for calculating the cost of multion usage.
|
29
|
+
trace_content: Flag indicating whether to trace the actual content.
|
30
|
+
|
31
|
+
Returns:
|
32
|
+
A function that wraps the chat completions method to add telemetry.
|
33
|
+
"""
|
34
|
+
|
35
|
+
def wrapper(wrapped, instance, args, kwargs):
|
36
|
+
"""
|
37
|
+
Wraps the 'chat.completions' API call to add telemetry.
|
38
|
+
|
39
|
+
This collects metrics such as execution time, cost, and token usage, and handles errors
|
40
|
+
gracefully, adding details to the trace for observability.
|
41
|
+
|
42
|
+
Args:
|
43
|
+
wrapped: The original 'chat.completions' method to be wrapped.
|
44
|
+
instance: The instance of the class where the original method is defined.
|
45
|
+
args: Positional arguments for the 'chat.completions' method.
|
46
|
+
kwargs: Keyword arguments for the 'chat.completions' method.
|
47
|
+
|
48
|
+
Returns:
|
49
|
+
The response from the original 'chat.completions' method.
|
50
|
+
"""
|
51
|
+
|
52
|
+
# pylint: disable=line-too-long
|
53
|
+
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
|
54
|
+
response = wrapped(*args, **kwargs)
|
55
|
+
|
56
|
+
try:
|
57
|
+
# Set base span attribues
|
58
|
+
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
59
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
60
|
+
SemanticConvetion.GEN_AI_SYSTEM_MULTION)
|
61
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
|
62
|
+
SemanticConvetion.GEN_AI_TYPE_AGENT)
|
63
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
|
64
|
+
gen_ai_endpoint)
|
65
|
+
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
|
66
|
+
application_name)
|
67
|
+
|
68
|
+
if gen_ai_endpoint == "multion.browse":
|
69
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_BROWSE_URL,
|
70
|
+
kwargs.get("url", ""))
|
71
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_STEP_COUNT,
|
72
|
+
response.metadata.step_count)
|
73
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_RESPONSE_TIME,
|
74
|
+
response.metadata.processing_time)
|
75
|
+
|
76
|
+
if trace_content:
|
77
|
+
span.add_event(
|
78
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
79
|
+
attributes={
|
80
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT: kwargs.get("cmd", ""),
|
81
|
+
},
|
82
|
+
)
|
83
|
+
span.add_event(
|
84
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
85
|
+
attributes={
|
86
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.message,
|
87
|
+
},
|
88
|
+
)
|
89
|
+
elif gen_ai_endpoint == "multion.retrieve":
|
90
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_BROWSE_URL,
|
91
|
+
kwargs.get("url", ""))
|
92
|
+
|
93
|
+
if trace_content:
|
94
|
+
span.add_event(
|
95
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
96
|
+
attributes={
|
97
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT: kwargs.get("cmd", ""),
|
98
|
+
},
|
99
|
+
)
|
100
|
+
span.add_event(
|
101
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
102
|
+
attributes={
|
103
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.message,
|
104
|
+
},
|
105
|
+
)
|
106
|
+
|
107
|
+
elif gen_ai_endpoint == "multion.sessions.create":
|
108
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_BROWSE_URL,
|
109
|
+
kwargs.get("url", ""))
|
110
|
+
|
111
|
+
if trace_content:
|
112
|
+
span.add_event(
|
113
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
114
|
+
attributes={
|
115
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.message,
|
116
|
+
},
|
117
|
+
)
|
118
|
+
|
119
|
+
span.set_status(Status(StatusCode.OK))
|
120
|
+
|
121
|
+
# Return original response
|
122
|
+
return response
|
123
|
+
|
124
|
+
except Exception as e:
|
125
|
+
handle_exception(span, e)
|
126
|
+
logger.error("Error in trace creation: %s", e)
|
127
|
+
|
128
|
+
# Return original response
|
129
|
+
return response
|
130
|
+
|
131
|
+
return wrapper
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
|
2
|
+
"""Initializer of Auto Instrumentation of Phidata 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.phidata.phidata import (
|
10
|
+
phidata_wrap
|
11
|
+
)
|
12
|
+
|
13
|
+
_instruments = ("phidata >= 2.5.32",)
|
14
|
+
|
15
|
+
class PhidataInstrumentor(BaseInstrumentor):
|
16
|
+
"""
|
17
|
+
An instrumentor for Phidata's client library.
|
18
|
+
"""
|
19
|
+
|
20
|
+
def instrumentation_dependencies(self) -> Collection[str]:
|
21
|
+
return _instruments
|
22
|
+
|
23
|
+
def _instrument(self, **kwargs):
|
24
|
+
application_name = kwargs.get("application_name", "default_application")
|
25
|
+
environment = kwargs.get("environment", "default_environment")
|
26
|
+
tracer = kwargs.get("tracer")
|
27
|
+
metrics = kwargs.get("metrics_dict")
|
28
|
+
pricing_info = kwargs.get("pricing_info", {})
|
29
|
+
trace_content = kwargs.get("trace_content", False)
|
30
|
+
disable_metrics = kwargs.get("disable_metrics")
|
31
|
+
version = importlib.metadata.version("phidata")
|
32
|
+
|
33
|
+
wrap_function_wrapper(
|
34
|
+
"phi.agent",
|
35
|
+
"Agent.print_response",
|
36
|
+
phidata_wrap("phidata.print_response", version, environment, application_name,
|
37
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
38
|
+
)
|
39
|
+
|
40
|
+
def _uninstrument(self, **kwargs):
|
41
|
+
# Proper uninstrumentation logic to revert patched methods
|
42
|
+
pass
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument
|
2
|
+
"""
|
3
|
+
Module for monitoring Phidata calls.
|
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 (
|
10
|
+
handle_exception,
|
11
|
+
)
|
12
|
+
from openlit.semcov import SemanticConvetion
|
13
|
+
|
14
|
+
# Initialize logger for logging potential issues and operations
|
15
|
+
logger = logging.getLogger(__name__)
|
16
|
+
|
17
|
+
def phidata_wrap(gen_ai_endpoint, version, environment, application_name,
|
18
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics):
|
19
|
+
"""
|
20
|
+
Generates a telemetry wrapper for chat completions to collect metrics.
|
21
|
+
|
22
|
+
Args:
|
23
|
+
gen_ai_endpoint: Endpoint identifier for logging and tracing.
|
24
|
+
version: Version of the monitoring package.
|
25
|
+
environment: Deployment environment (e.g., production, staging).
|
26
|
+
application_name: Name of the application using the Phidata Agent.
|
27
|
+
tracer: OpenTelemetry tracer for creating spans.
|
28
|
+
pricing_info: Information used for calculating the cost of Phidata usage.
|
29
|
+
trace_content: Flag indicating whether to trace the actual content.
|
30
|
+
|
31
|
+
Returns:
|
32
|
+
A function that wraps the chat completions method to add telemetry.
|
33
|
+
"""
|
34
|
+
|
35
|
+
def wrapper(wrapped, instance, args, kwargs):
|
36
|
+
"""
|
37
|
+
Wraps the 'chat.completions' API call to add telemetry.
|
38
|
+
|
39
|
+
This collects metrics such as execution time, cost, and token usage, and handles errors
|
40
|
+
gracefully, adding details to the trace for observability.
|
41
|
+
|
42
|
+
Args:
|
43
|
+
wrapped: The original 'chat.completions' method to be wrapped.
|
44
|
+
instance: The instance of the class where the original method is defined.
|
45
|
+
args: Positional arguments for the 'chat.completions' method.
|
46
|
+
kwargs: Keyword arguments for the 'chat.completions' method.
|
47
|
+
|
48
|
+
Returns:
|
49
|
+
The response from the original 'chat.completions' method.
|
50
|
+
"""
|
51
|
+
|
52
|
+
# pylint: disable=line-too-long
|
53
|
+
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
|
54
|
+
response = wrapped(*args, **kwargs)
|
55
|
+
|
56
|
+
try:
|
57
|
+
# Set base span attribues
|
58
|
+
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
59
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
60
|
+
SemanticConvetion.GEN_AI_SYSTEM_PHIDATA)
|
61
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
|
62
|
+
SemanticConvetion.GEN_AI_TYPE_AGENT)
|
63
|
+
span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
|
64
|
+
gen_ai_endpoint)
|
65
|
+
span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
|
66
|
+
application_name)
|
67
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_ID,
|
68
|
+
getattr(instance, 'agent_id', '') or '')
|
69
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_ROLE,
|
70
|
+
getattr(instance, 'name', '') or '')
|
71
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
|
72
|
+
getattr(getattr(instance, 'model', None), 'id', '') or '')
|
73
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_TOOLS,
|
74
|
+
str(getattr(instance, 'tools', '')) or '')
|
75
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_CONTEXT,
|
76
|
+
str(getattr(instance, 'knowledge', '')) or '')
|
77
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_TASK,
|
78
|
+
str(getattr(instance, 'task', '')) or '')
|
79
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_INSTRUCTIONS,
|
80
|
+
str(getattr(instance, 'instructions', '')) or '')
|
81
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_STORAGE,
|
82
|
+
str(getattr(instance, 'storage', '')) or '')
|
83
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_ENABLE_HISTORY,
|
84
|
+
str(getattr(instance, 'add_history_to_messages', '')) or '')
|
85
|
+
|
86
|
+
span.set_status(Status(StatusCode.OK))
|
87
|
+
|
88
|
+
# Return original response
|
89
|
+
return response
|
90
|
+
|
91
|
+
except Exception as e:
|
92
|
+
handle_exception(span, e)
|
93
|
+
logger.error("Error in trace creation: %s", e)
|
94
|
+
|
95
|
+
# Return original response
|
96
|
+
return response
|
97
|
+
|
98
|
+
return wrapper
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
|
2
|
+
"""Initializer of Auto Instrumentation of Prem AI 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.premai.premai import (
|
10
|
+
chat, embedding
|
11
|
+
)
|
12
|
+
|
13
|
+
_instruments = ("premai >= 0.3.79",)
|
14
|
+
|
15
|
+
class PremAIInstrumentor(BaseInstrumentor):
|
16
|
+
"""
|
17
|
+
An instrumentor for Prem AI's client library.
|
18
|
+
"""
|
19
|
+
|
20
|
+
def instrumentation_dependencies(self) -> Collection[str]:
|
21
|
+
return _instruments
|
22
|
+
|
23
|
+
def _instrument(self, **kwargs):
|
24
|
+
application_name = kwargs.get("application_name", "default_application")
|
25
|
+
environment = kwargs.get("environment", "default_environment")
|
26
|
+
tracer = kwargs.get("tracer")
|
27
|
+
metrics = kwargs.get("metrics_dict")
|
28
|
+
pricing_info = kwargs.get("pricing_info", {})
|
29
|
+
trace_content = kwargs.get("trace_content", False)
|
30
|
+
disable_metrics = kwargs.get("disable_metrics")
|
31
|
+
version = importlib.metadata.version("premai")
|
32
|
+
|
33
|
+
# sync chat
|
34
|
+
wrap_function_wrapper(
|
35
|
+
"premai.api",
|
36
|
+
"ChatCompletionsModule.create",
|
37
|
+
chat("premai.chat.completions", version, environment, application_name,
|
38
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
39
|
+
)
|
40
|
+
|
41
|
+
# sync embedding
|
42
|
+
wrap_function_wrapper(
|
43
|
+
"premai.api",
|
44
|
+
"EmbeddingsModule.create",
|
45
|
+
embedding("premai.embeddings", version, environment, application_name,
|
46
|
+
tracer, pricing_info, trace_content, metrics, disable_metrics),
|
47
|
+
)
|
48
|
+
|
49
|
+
def _uninstrument(self, **kwargs):
|
50
|
+
# Proper uninstrumentation logic to revert patched methods
|
51
|
+
pass
|