forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
227 lines (181 loc) · 6.96 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from os import environ
from typing import Optional, Union
from urllib.parse import urlparse
from httpx import URL
from openai import NOT_GIVEN
from opentelemetry._events import Event
from opentelemetry.semconv._incubating.attributes import (
gen_ai_attributes as GenAIAttributes,
)
from opentelemetry.semconv._incubating.attributes import (
server_attributes as ServerAttributes,
)
from opentelemetry.semconv.attributes import (
error_attributes as ErrorAttributes,
)
from opentelemetry.trace.status import Status, StatusCode
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = (
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"
)
def is_content_enabled() -> bool:
capture_content = environ.get(
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false"
)
return capture_content.lower() == "true"
def extract_tool_calls(item, capture_content):
tool_calls = get_property_value(item, "tool_calls")
if tool_calls is None:
return None
calls = []
for tool_call in tool_calls:
tool_call_dict = {}
call_id = get_property_value(tool_call, "id")
if call_id:
tool_call_dict["id"] = call_id
tool_type = get_property_value(tool_call, "type")
if tool_type:
tool_call_dict["type"] = tool_type
func = get_property_value(tool_call, "function")
if func:
tool_call_dict["function"] = {}
name = get_property_value(func, "name")
if name:
tool_call_dict["function"]["name"] = name
arguments = get_property_value(func, "arguments")
if capture_content and arguments:
if isinstance(arguments, str):
arguments = arguments.replace("\n", "")
tool_call_dict["function"]["arguments"] = arguments
calls.append(tool_call_dict)
return calls
def set_server_address_and_port(client_instance, attributes):
base_client = getattr(client_instance, "_client", None)
base_url = getattr(base_client, "base_url", None)
if not base_url:
return
port = -1
if isinstance(base_url, URL):
attributes[ServerAttributes.SERVER_ADDRESS] = base_url.host
port = base_url.port
elif isinstance(base_url, str):
url = urlparse(base_url)
attributes[ServerAttributes.SERVER_ADDRESS] = url.hostname
port = url.port
if port and port != 443 and port > 0:
attributes[ServerAttributes.SERVER_PORT] = port
def get_property_value(obj, property_name):
if isinstance(obj, dict):
return obj.get(property_name, None)
return getattr(obj, property_name, None)
def message_to_event(message, capture_content):
attributes = {
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.OPENAI.value
}
role = get_property_value(message, "role")
content = get_property_value(message, "content")
body = {}
if capture_content and content:
body["content"] = content
if role == "assistant":
tool_calls = extract_tool_calls(message, capture_content)
if tool_calls:
body = {"tool_calls": tool_calls}
elif role == "tool":
tool_call_id = get_property_value(message, "tool_call_id")
if tool_call_id:
body["id"] = tool_call_id
return Event(
name=f"gen_ai.{role}.message",
attributes=attributes,
body=body if body else None,
)
def choice_to_event(choice, capture_content):
attributes = {
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.OPENAI.value
}
body = {
"index": choice.index,
"finish_reason": choice.finish_reason or "error",
}
if choice.message:
message = {
"role": (
choice.message.role
if choice.message and choice.message.role
else None
)
}
tool_calls = extract_tool_calls(choice.message, capture_content)
if tool_calls:
message["tool_calls"] = tool_calls
content = get_property_value(choice.message, "content")
if capture_content and content:
message["content"] = content
body["message"] = message
return Event(
name="gen_ai.choice",
attributes=attributes,
body=body,
)
def set_span_attributes(span, attributes: dict):
for field, value in attributes.model_dump(by_alias=True).items():
set_span_attribute(span, field, value)
def set_span_attribute(span, name, value):
if non_numerical_value_is_set(value) is False:
return
span.set_attribute(name, value)
def is_streaming(kwargs):
return non_numerical_value_is_set(kwargs.get("stream"))
def non_numerical_value_is_set(value: Optional[Union[bool, str]]):
return bool(value) and value != NOT_GIVEN
def get_llm_request_attributes(
kwargs,
client_instance,
operation_name=GenAIAttributes.GenAiOperationNameValues.CHAT.value,
):
attributes = {
GenAIAttributes.GEN_AI_OPERATION_NAME: operation_name,
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.OPENAI.value,
GenAIAttributes.GEN_AI_REQUEST_MODEL: kwargs.get("model"),
GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE: kwargs.get("temperature"),
GenAIAttributes.GEN_AI_REQUEST_TOP_P: kwargs.get("p")
or kwargs.get("top_p"),
GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS: kwargs.get("max_tokens"),
GenAIAttributes.GEN_AI_REQUEST_PRESENCE_PENALTY: kwargs.get(
"presence_penalty"
),
GenAIAttributes.GEN_AI_REQUEST_FREQUENCY_PENALTY: kwargs.get(
"frequency_penalty"
),
GenAIAttributes.GEN_AI_OPENAI_REQUEST_RESPONSE_FORMAT: kwargs.get(
"response_format"
),
GenAIAttributes.GEN_AI_OPENAI_REQUEST_SEED: kwargs.get("seed"),
}
set_server_address_and_port(client_instance, attributes)
service_tier = kwargs.get("service_tier")
attributes[GenAIAttributes.GEN_AI_OPENAI_RESPONSE_SERVICE_TIER] = (
service_tier if service_tier != "auto" else None
)
# filter out None values
return {k: v for k, v in attributes.items() if v is not None}
def handle_span_exception(span, error):
span.set_status(Status(StatusCode.ERROR, str(error)))
if span.is_recording():
span.set_attribute(
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
)
span.end()