opentelemetry-instrumentation-vertexai 0.47.3__py3-none-any.whl → 2.1b0__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.
Potentially problematic release.
This version of opentelemetry-instrumentation-vertexai might be problematic. Click here for more details.
- opentelemetry/instrumentation/vertexai/__init__.py +150 -343
- opentelemetry/instrumentation/vertexai/events.py +190 -0
- opentelemetry/instrumentation/vertexai/package.py +16 -0
- opentelemetry/instrumentation/vertexai/patch.py +371 -0
- opentelemetry/instrumentation/vertexai/py.typed +0 -0
- opentelemetry/instrumentation/vertexai/utils.py +445 -29
- opentelemetry/instrumentation/vertexai/version.py +15 -1
- opentelemetry_instrumentation_vertexai-2.1b0.dist-info/METADATA +106 -0
- opentelemetry_instrumentation_vertexai-2.1b0.dist-info/RECORD +12 -0
- {opentelemetry_instrumentation_vertexai-0.47.3.dist-info → opentelemetry_instrumentation_vertexai-2.1b0.dist-info}/WHEEL +1 -1
- opentelemetry_instrumentation_vertexai-2.1b0.dist-info/entry_points.txt +2 -0
- opentelemetry_instrumentation_vertexai-2.1b0.dist-info/licenses/LICENSE +201 -0
- opentelemetry/instrumentation/vertexai/config.py +0 -9
- opentelemetry/instrumentation/vertexai/event_emitter.py +0 -164
- opentelemetry/instrumentation/vertexai/event_models.py +0 -41
- opentelemetry/instrumentation/vertexai/span_utils.py +0 -310
- opentelemetry_instrumentation_vertexai-0.47.3.dist-info/METADATA +0 -58
- opentelemetry_instrumentation_vertexai-0.47.3.dist-info/RECORD +0 -11
- opentelemetry_instrumentation_vertexai-0.47.3.dist-info/entry_points.txt +0 -3
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
import copy
|
|
2
|
-
import json
|
|
3
|
-
import base64
|
|
4
|
-
import logging
|
|
5
|
-
import asyncio
|
|
6
|
-
import threading
|
|
7
|
-
from opentelemetry.instrumentation.vertexai.utils import dont_throw, should_send_prompts
|
|
8
|
-
from opentelemetry.instrumentation.vertexai.config import Config
|
|
9
|
-
from opentelemetry.semconv_ai import SpanAttributes
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
logger = logging.getLogger(__name__)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def _set_span_attribute(span, name, value):
|
|
16
|
-
if value is not None:
|
|
17
|
-
if value != "":
|
|
18
|
-
span.set_attribute(name, value)
|
|
19
|
-
return
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def _is_base64_image_part(item):
|
|
23
|
-
"""Check if item is a VertexAI Part object containing image data"""
|
|
24
|
-
try:
|
|
25
|
-
# Check if it has the Part attributes we expect
|
|
26
|
-
if not hasattr(item, 'inline_data') or not hasattr(item, 'mime_type'):
|
|
27
|
-
return False
|
|
28
|
-
|
|
29
|
-
# Check if it's an image mime type and has inline data
|
|
30
|
-
if item.mime_type and 'image/' in item.mime_type and item.inline_data:
|
|
31
|
-
# Check if the inline_data has actual data
|
|
32
|
-
if hasattr(item.inline_data, 'data') and item.inline_data.data:
|
|
33
|
-
return True
|
|
34
|
-
|
|
35
|
-
return False
|
|
36
|
-
except Exception:
|
|
37
|
-
return False
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
async def _process_image_part(item, trace_id, span_id, content_index):
|
|
41
|
-
"""Process a VertexAI Part object containing image data"""
|
|
42
|
-
if not Config.upload_base64_image:
|
|
43
|
-
return None
|
|
44
|
-
|
|
45
|
-
try:
|
|
46
|
-
# Extract format from mime type (e.g., 'image/jpeg' -> 'jpeg')
|
|
47
|
-
image_format = item.mime_type.split('/')[1] if item.mime_type else 'unknown'
|
|
48
|
-
image_name = f"content_{content_index}.{image_format}"
|
|
49
|
-
|
|
50
|
-
# Convert binary data to base64 string for upload
|
|
51
|
-
binary_data = item.inline_data.data
|
|
52
|
-
base64_string = base64.b64encode(binary_data).decode('utf-8')
|
|
53
|
-
|
|
54
|
-
# Upload the base64 data - convert IDs to strings
|
|
55
|
-
url = await Config.upload_base64_image(str(trace_id), str(span_id), image_name, base64_string)
|
|
56
|
-
|
|
57
|
-
# Return OpenAI-compatible format for consistency across LLM providers
|
|
58
|
-
return {
|
|
59
|
-
"type": "image_url",
|
|
60
|
-
"image_url": {"url": url}
|
|
61
|
-
}
|
|
62
|
-
except Exception as e:
|
|
63
|
-
logger.warning(f"Failed to process image part: {e}")
|
|
64
|
-
# Return None to skip adding this image to the span
|
|
65
|
-
return None
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def run_async(method):
|
|
69
|
-
"""Handle async method in sync context, following OpenAI's battle-tested approach"""
|
|
70
|
-
try:
|
|
71
|
-
loop = asyncio.get_running_loop()
|
|
72
|
-
except RuntimeError:
|
|
73
|
-
loop = None
|
|
74
|
-
|
|
75
|
-
if loop and loop.is_running():
|
|
76
|
-
thread = threading.Thread(target=lambda: asyncio.run(method))
|
|
77
|
-
thread.start()
|
|
78
|
-
thread.join()
|
|
79
|
-
else:
|
|
80
|
-
asyncio.run(method)
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
def _process_image_part_sync(item, trace_id, span_id, content_index):
|
|
84
|
-
"""Synchronous version of image part processing using OpenAI's pattern"""
|
|
85
|
-
if not Config.upload_base64_image:
|
|
86
|
-
return None
|
|
87
|
-
|
|
88
|
-
try:
|
|
89
|
-
# Extract format from mime type (e.g., 'image/jpeg' -> 'jpeg')
|
|
90
|
-
image_format = item.mime_type.split('/')[1] if item.mime_type else 'unknown'
|
|
91
|
-
image_name = f"content_{content_index}.{image_format}"
|
|
92
|
-
|
|
93
|
-
# Convert binary data to base64 string for upload
|
|
94
|
-
binary_data = item.inline_data.data
|
|
95
|
-
base64_string = base64.b64encode(binary_data).decode('utf-8')
|
|
96
|
-
|
|
97
|
-
# Use OpenAI's run_async pattern to handle the async upload function
|
|
98
|
-
url = None
|
|
99
|
-
|
|
100
|
-
async def upload_task():
|
|
101
|
-
nonlocal url
|
|
102
|
-
url = await Config.upload_base64_image(str(trace_id), str(span_id), image_name, base64_string)
|
|
103
|
-
|
|
104
|
-
run_async(upload_task())
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
"type": "image_url",
|
|
108
|
-
"image_url": {"url": url}
|
|
109
|
-
}
|
|
110
|
-
except Exception as e:
|
|
111
|
-
logger.warning(f"Failed to process image part sync: {e}")
|
|
112
|
-
# Return None to skip adding this image to the span
|
|
113
|
-
return None
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
async def _process_vertexai_argument(argument, span):
|
|
117
|
-
"""Process a single argument for VertexAI, handling different types"""
|
|
118
|
-
if isinstance(argument, str):
|
|
119
|
-
# Simple text argument in OpenAI format
|
|
120
|
-
return [{"type": "text", "text": argument}]
|
|
121
|
-
|
|
122
|
-
elif isinstance(argument, list):
|
|
123
|
-
# List of mixed content (text strings and Part objects) - deep copy and process
|
|
124
|
-
content_list = copy.deepcopy(argument)
|
|
125
|
-
processed_items = []
|
|
126
|
-
|
|
127
|
-
for item_index, content_item in enumerate(content_list):
|
|
128
|
-
processed_item = await _process_content_item_vertexai(content_item, span, item_index)
|
|
129
|
-
if processed_item is not None:
|
|
130
|
-
processed_items.append(processed_item)
|
|
131
|
-
|
|
132
|
-
return processed_items
|
|
133
|
-
|
|
134
|
-
else:
|
|
135
|
-
# Single Part object - convert to OpenAI format
|
|
136
|
-
processed_item = await _process_content_item_vertexai(argument, span, 0)
|
|
137
|
-
return [processed_item] if processed_item is not None else []
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
async def _process_content_item_vertexai(content_item, span, item_index):
|
|
141
|
-
"""Process a single content item for VertexAI"""
|
|
142
|
-
if isinstance(content_item, str):
|
|
143
|
-
# Convert text to OpenAI format
|
|
144
|
-
return {"type": "text", "text": content_item}
|
|
145
|
-
|
|
146
|
-
elif _is_base64_image_part(content_item):
|
|
147
|
-
# Process image part
|
|
148
|
-
return await _process_image_part(
|
|
149
|
-
content_item, span.context.trace_id, span.context.span_id, item_index
|
|
150
|
-
)
|
|
151
|
-
|
|
152
|
-
elif hasattr(content_item, 'text'):
|
|
153
|
-
# Text part to OpenAI format
|
|
154
|
-
return {"type": "text", "text": content_item.text}
|
|
155
|
-
|
|
156
|
-
else:
|
|
157
|
-
# Other types as text
|
|
158
|
-
return {"type": "text", "text": str(content_item)}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
def _process_vertexai_argument_sync(argument, span):
|
|
162
|
-
"""Synchronous version of argument processing for VertexAI"""
|
|
163
|
-
if isinstance(argument, str):
|
|
164
|
-
# Simple text argument in OpenAI format
|
|
165
|
-
return [{"type": "text", "text": argument}]
|
|
166
|
-
|
|
167
|
-
elif isinstance(argument, list):
|
|
168
|
-
# List of mixed content (text strings and Part objects) - deep copy and process
|
|
169
|
-
content_list = copy.deepcopy(argument)
|
|
170
|
-
processed_items = []
|
|
171
|
-
|
|
172
|
-
for item_index, content_item in enumerate(content_list):
|
|
173
|
-
processed_item = _process_content_item_vertexai_sync(content_item, span, item_index)
|
|
174
|
-
if processed_item is not None:
|
|
175
|
-
processed_items.append(processed_item)
|
|
176
|
-
|
|
177
|
-
return processed_items
|
|
178
|
-
|
|
179
|
-
else:
|
|
180
|
-
# Single Part object - convert to OpenAI format
|
|
181
|
-
processed_item = _process_content_item_vertexai_sync(argument, span, 0)
|
|
182
|
-
return [processed_item] if processed_item is not None else []
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
def _process_content_item_vertexai_sync(content_item, span, item_index):
|
|
186
|
-
"""Synchronous version of content item processing for VertexAI"""
|
|
187
|
-
if isinstance(content_item, str):
|
|
188
|
-
# Convert text to OpenAI format
|
|
189
|
-
return {"type": "text", "text": content_item}
|
|
190
|
-
|
|
191
|
-
elif _is_base64_image_part(content_item):
|
|
192
|
-
# Process image part
|
|
193
|
-
return _process_image_part_sync(
|
|
194
|
-
content_item, span.context.trace_id, span.context.span_id, item_index
|
|
195
|
-
)
|
|
196
|
-
|
|
197
|
-
elif hasattr(content_item, 'text'):
|
|
198
|
-
# Text part to OpenAI format
|
|
199
|
-
return {"type": "text", "text": content_item.text}
|
|
200
|
-
|
|
201
|
-
else:
|
|
202
|
-
# Other types as text
|
|
203
|
-
return {"type": "text", "text": str(content_item)}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
@dont_throw
|
|
207
|
-
async def set_input_attributes(span, args):
|
|
208
|
-
"""Process input arguments, handling both text and image content"""
|
|
209
|
-
if not span.is_recording():
|
|
210
|
-
return
|
|
211
|
-
if should_send_prompts() and args is not None and len(args) > 0:
|
|
212
|
-
# Process each argument using extracted helper methods
|
|
213
|
-
for arg_index, argument in enumerate(args):
|
|
214
|
-
processed_content = await _process_vertexai_argument(argument, span)
|
|
215
|
-
|
|
216
|
-
if processed_content:
|
|
217
|
-
_set_span_attribute(
|
|
218
|
-
span,
|
|
219
|
-
f"{SpanAttributes.LLM_PROMPTS}.{arg_index}.role",
|
|
220
|
-
"user"
|
|
221
|
-
)
|
|
222
|
-
_set_span_attribute(
|
|
223
|
-
span,
|
|
224
|
-
f"{SpanAttributes.LLM_PROMPTS}.{arg_index}.content",
|
|
225
|
-
json.dumps(processed_content)
|
|
226
|
-
)
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
# Sync version with image processing support
|
|
230
|
-
@dont_throw
|
|
231
|
-
def set_input_attributes_sync(span, args):
|
|
232
|
-
"""Synchronous version with image processing support"""
|
|
233
|
-
if not span.is_recording():
|
|
234
|
-
return
|
|
235
|
-
if should_send_prompts() and args is not None and len(args) > 0:
|
|
236
|
-
# Process each argument using extracted helper methods
|
|
237
|
-
for arg_index, argument in enumerate(args):
|
|
238
|
-
processed_content = _process_vertexai_argument_sync(argument, span)
|
|
239
|
-
|
|
240
|
-
if processed_content:
|
|
241
|
-
_set_span_attribute(
|
|
242
|
-
span,
|
|
243
|
-
f"{SpanAttributes.LLM_PROMPTS}.{arg_index}.role",
|
|
244
|
-
"user"
|
|
245
|
-
)
|
|
246
|
-
_set_span_attribute(
|
|
247
|
-
span,
|
|
248
|
-
f"{SpanAttributes.LLM_PROMPTS}.{arg_index}.content",
|
|
249
|
-
json.dumps(processed_content)
|
|
250
|
-
)
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
@dont_throw
|
|
254
|
-
def set_model_input_attributes(span, kwargs, llm_model):
|
|
255
|
-
if not span.is_recording():
|
|
256
|
-
return
|
|
257
|
-
_set_span_attribute(span, SpanAttributes.LLM_REQUEST_MODEL, llm_model)
|
|
258
|
-
_set_span_attribute(
|
|
259
|
-
span, f"{SpanAttributes.LLM_PROMPTS}.0.user", kwargs.get("prompt")
|
|
260
|
-
)
|
|
261
|
-
_set_span_attribute(
|
|
262
|
-
span, SpanAttributes.LLM_REQUEST_TEMPERATURE, kwargs.get("temperature")
|
|
263
|
-
)
|
|
264
|
-
_set_span_attribute(
|
|
265
|
-
span, SpanAttributes.LLM_REQUEST_MAX_TOKENS, kwargs.get("max_output_tokens")
|
|
266
|
-
)
|
|
267
|
-
_set_span_attribute(span, SpanAttributes.LLM_REQUEST_TOP_P, kwargs.get("top_p"))
|
|
268
|
-
_set_span_attribute(span, SpanAttributes.LLM_TOP_K, kwargs.get("top_k"))
|
|
269
|
-
_set_span_attribute(
|
|
270
|
-
span, SpanAttributes.LLM_PRESENCE_PENALTY, kwargs.get("presence_penalty")
|
|
271
|
-
)
|
|
272
|
-
_set_span_attribute(
|
|
273
|
-
span, SpanAttributes.LLM_FREQUENCY_PENALTY, kwargs.get("frequency_penalty")
|
|
274
|
-
)
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
@dont_throw
|
|
278
|
-
def set_response_attributes(span, llm_model, generation_text):
|
|
279
|
-
if not span.is_recording() or not should_send_prompts():
|
|
280
|
-
return
|
|
281
|
-
_set_span_attribute(span, f"{SpanAttributes.LLM_COMPLETIONS}.0.role", "assistant")
|
|
282
|
-
_set_span_attribute(
|
|
283
|
-
span,
|
|
284
|
-
f"{SpanAttributes.LLM_COMPLETIONS}.0.content",
|
|
285
|
-
generation_text,
|
|
286
|
-
)
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
@dont_throw
|
|
290
|
-
def set_model_response_attributes(span, llm_model, token_usage):
|
|
291
|
-
if not span.is_recording():
|
|
292
|
-
return
|
|
293
|
-
_set_span_attribute(span, SpanAttributes.LLM_RESPONSE_MODEL, llm_model)
|
|
294
|
-
|
|
295
|
-
if token_usage:
|
|
296
|
-
_set_span_attribute(
|
|
297
|
-
span,
|
|
298
|
-
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
|
|
299
|
-
token_usage.total_token_count,
|
|
300
|
-
)
|
|
301
|
-
_set_span_attribute(
|
|
302
|
-
span,
|
|
303
|
-
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
|
|
304
|
-
token_usage.candidates_token_count,
|
|
305
|
-
)
|
|
306
|
-
_set_span_attribute(
|
|
307
|
-
span,
|
|
308
|
-
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
|
|
309
|
-
token_usage.prompt_token_count,
|
|
310
|
-
)
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: opentelemetry-instrumentation-vertexai
|
|
3
|
-
Version: 0.47.3
|
|
4
|
-
Summary: OpenTelemetry Vertex AI instrumentation
|
|
5
|
-
License: Apache-2.0
|
|
6
|
-
Author: Gal Kleinman
|
|
7
|
-
Author-email: gal@traceloop.com
|
|
8
|
-
Requires-Python: >=3.9,<4
|
|
9
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
-
Provides-Extra: instruments
|
|
18
|
-
Requires-Dist: opentelemetry-api (>=1.28.0,<2.0.0)
|
|
19
|
-
Requires-Dist: opentelemetry-instrumentation (>=0.50b0)
|
|
20
|
-
Requires-Dist: opentelemetry-semantic-conventions (>=0.50b0)
|
|
21
|
-
Requires-Dist: opentelemetry-semantic-conventions-ai (>=0.4.13,<0.5.0)
|
|
22
|
-
Project-URL: Repository, https://github.com/traceloop/openllmetry/tree/main/packages/opentelemetry-instrumentation-vertexai
|
|
23
|
-
Description-Content-Type: text/markdown
|
|
24
|
-
|
|
25
|
-
# OpenTelemetry VertexAI Instrumentation
|
|
26
|
-
|
|
27
|
-
<a href="https://pypi.org/project/opentelemetry-instrumentation-vertexai/">
|
|
28
|
-
<img src="https://badge.fury.io/py/opentelemetry-instrumentation-vertexai.svg">
|
|
29
|
-
</a>
|
|
30
|
-
|
|
31
|
-
This library allows tracing VertexAI prompts and completions sent with the official [VertexAI library](https://github.com/googleapis/python-aiplatform).
|
|
32
|
-
|
|
33
|
-
## Installation
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
pip install opentelemetry-instrumentation-vertexai
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## Example usage
|
|
40
|
-
|
|
41
|
-
```python
|
|
42
|
-
from opentelemetry.instrumentation.vertexai import VertexAIInstrumentor
|
|
43
|
-
|
|
44
|
-
VertexAIInstrumentor().instrument()
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Privacy
|
|
48
|
-
|
|
49
|
-
**By default, this instrumentation logs prompts, completions, and embeddings to span attributes**. This gives you a clear visibility into how your LLM application is working, and can make it easy to debug and evaluate the quality of the outputs.
|
|
50
|
-
|
|
51
|
-
However, you may want to disable this logging for privacy reasons, as they may contain highly sensitive data from your users. You may also simply want to reduce the size of your traces.
|
|
52
|
-
|
|
53
|
-
To disable logging, set the `TRACELOOP_TRACE_CONTENT` environment variable to `false`.
|
|
54
|
-
|
|
55
|
-
```bash
|
|
56
|
-
TRACELOOP_TRACE_CONTENT=false
|
|
57
|
-
```
|
|
58
|
-
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
opentelemetry/instrumentation/vertexai/__init__.py,sha256=c3EtiFxN8F27o4hSHktd1XCP5C3iNvTUY9NhghnbQZY,12290
|
|
2
|
-
opentelemetry/instrumentation/vertexai/config.py,sha256=LDyIH2dNsQsyFGy3otuvLWnRwf1hT_ivncioMPW8_ks,241
|
|
3
|
-
opentelemetry/instrumentation/vertexai/event_emitter.py,sha256=v-f7-oWbLi1IcRjpsoPst0XhocqCKvaEovc_0Er0PDo,5043
|
|
4
|
-
opentelemetry/instrumentation/vertexai/event_models.py,sha256=PCfCGxrrArwZqR-4wFcXrhwQq0sBMAxmSrpC4PUMtaM,876
|
|
5
|
-
opentelemetry/instrumentation/vertexai/span_utils.py,sha256=yRulZKuISbA0qugos16MD-pALx7MpXJzDMy0bswx5x4,10903
|
|
6
|
-
opentelemetry/instrumentation/vertexai/utils.py,sha256=Rj-TT_GQFhfi1F1rugvDRFxl4Xo4D-rOYJojOK8iblI,1172
|
|
7
|
-
opentelemetry/instrumentation/vertexai/version.py,sha256=cspeq_LxS-rfS8TCdvDr3f9URoflMOjJveMjR-O_cjs,23
|
|
8
|
-
opentelemetry_instrumentation_vertexai-0.47.3.dist-info/METADATA,sha256=wpnxTeF4DWDgVnjIOXlDEnjm-dzcdacg_mwBnR3UxZU,2241
|
|
9
|
-
opentelemetry_instrumentation_vertexai-0.47.3.dist-info/WHEEL,sha256=M5asmiAlL6HEcOq52Yi5mmk9KmTVjY2RDPtO4p9DMrc,88
|
|
10
|
-
opentelemetry_instrumentation_vertexai-0.47.3.dist-info/entry_points.txt,sha256=HbacwtKx_31YuUruZKYKWOiTGnRw3YaazUKF3TPbzDc,114
|
|
11
|
-
opentelemetry_instrumentation_vertexai-0.47.3.dist-info/RECORD,,
|