lmnr 0.7.0__py3-none-any.whl → 0.7.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.
- lmnr/opentelemetry_lib/decorators/__init__.py +43 -4
- lmnr/opentelemetry_lib/litellm/__init__.py +5 -2
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/anthropic/__init__.py +85 -6
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/anthropic/span_utils.py +57 -14
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/anthropic/streaming.py +106 -6
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/google_genai/__init__.py +8 -3
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/openai/shared/__init__.py +6 -0
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/openai/shared/chat_wrappers.py +139 -10
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/openai/shared/completion_wrappers.py +8 -3
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/openai/shared/embeddings_wrappers.py +6 -2
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/openai/v1/assistant_wrappers.py +6 -3
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/openai/v1/event_handler_wrapper.py +4 -1
- lmnr/opentelemetry_lib/opentelemetry/instrumentation/openai/v1/responses_wrappers.py +14 -5
- lmnr/opentelemetry_lib/tracing/context.py +18 -1
- lmnr/sdk/browser/pw_utils.py +43 -122
- lmnr/sdk/browser/recorder/record.umd.min.cjs +84 -0
- lmnr/sdk/laminar.py +51 -26
- lmnr/sdk/types.py +17 -5
- lmnr/version.py +1 -1
- {lmnr-0.7.0.dist-info → lmnr-0.7.2.dist-info}/METADATA +48 -51
- {lmnr-0.7.0.dist-info → lmnr-0.7.2.dist-info}/RECORD +23 -23
- lmnr/sdk/browser/rrweb/rrweb.umd.min.cjs +0 -98
- {lmnr-0.7.0.dist-info → lmnr-0.7.2.dist-info}/WHEEL +0 -0
- {lmnr-0.7.0.dist-info → lmnr-0.7.2.dist-info}/entry_points.txt +0 -0
@@ -6,8 +6,15 @@ import types
|
|
6
6
|
from typing import Any, AsyncGenerator, Callable, Generator, Literal
|
7
7
|
|
8
8
|
from opentelemetry import context as context_api
|
9
|
-
from opentelemetry.trace import Span
|
10
|
-
|
9
|
+
from opentelemetry.trace import Span, Status, StatusCode
|
10
|
+
|
11
|
+
from lmnr.opentelemetry_lib.tracing.context import (
|
12
|
+
CONTEXT_SESSION_ID_KEY,
|
13
|
+
CONTEXT_USER_ID_KEY,
|
14
|
+
attach_context,
|
15
|
+
detach_context,
|
16
|
+
get_event_attributes_from_context,
|
17
|
+
)
|
11
18
|
from lmnr.sdk.utils import get_input_from_func_args, is_method
|
12
19
|
from lmnr.opentelemetry_lib import MAX_MANUAL_SPAN_PAYLOAD_SIZE
|
13
20
|
from lmnr.opentelemetry_lib.tracing.tracer import get_tracer_with_context
|
@@ -180,7 +187,21 @@ def observe_base(
|
|
180
187
|
|
181
188
|
span = _setup_span(span_name, span_type, association_properties)
|
182
189
|
new_context = wrapper.push_span_context(span)
|
190
|
+
if session_id := association_properties.get("session_id"):
|
191
|
+
new_context = context_api.set_value(
|
192
|
+
CONTEXT_SESSION_ID_KEY, session_id, new_context
|
193
|
+
)
|
194
|
+
if user_id := association_properties.get("user_id"):
|
195
|
+
new_context = context_api.set_value(
|
196
|
+
CONTEXT_USER_ID_KEY, user_id, new_context
|
197
|
+
)
|
198
|
+
# Some auto-instrumentations are not under our control, so they
|
199
|
+
# don't have access to our isolated context. We attach the context
|
200
|
+
# to the OTEL global context, so that spans know their parent
|
201
|
+
# span and trace_id.
|
183
202
|
ctx_token = context_api.attach(new_context)
|
203
|
+
# update our isolated context too
|
204
|
+
isolated_ctx_token = attach_context(new_context)
|
184
205
|
|
185
206
|
_process_input(
|
186
207
|
span, fn, args, kwargs, ignore_input, ignore_inputs, input_formatter
|
@@ -195,7 +216,7 @@ def observe_base(
|
|
195
216
|
finally:
|
196
217
|
# Always restore global context
|
197
218
|
context_api.detach(ctx_token)
|
198
|
-
|
219
|
+
detach_context(isolated_ctx_token)
|
199
220
|
# span will be ended in the generator
|
200
221
|
if isinstance(res, types.GeneratorType):
|
201
222
|
return _handle_generator(span, ctx_token, res)
|
@@ -240,7 +261,21 @@ def async_observe_base(
|
|
240
261
|
|
241
262
|
span = _setup_span(span_name, span_type, association_properties)
|
242
263
|
new_context = wrapper.push_span_context(span)
|
264
|
+
if session_id := association_properties.get("session_id"):
|
265
|
+
new_context = context_api.set_value(
|
266
|
+
CONTEXT_SESSION_ID_KEY, session_id, new_context
|
267
|
+
)
|
268
|
+
if user_id := association_properties.get("user_id"):
|
269
|
+
new_context = context_api.set_value(
|
270
|
+
CONTEXT_USER_ID_KEY, user_id, new_context
|
271
|
+
)
|
272
|
+
# Some auto-instrumentations are not under our control, so they
|
273
|
+
# don't have access to our isolated context. We attach the context
|
274
|
+
# to the OTEL global context, so that spans know their parent
|
275
|
+
# span and trace_id.
|
243
276
|
ctx_token = context_api.attach(new_context)
|
277
|
+
# update our isolated context too
|
278
|
+
isolated_ctx_token = attach_context(new_context)
|
244
279
|
|
245
280
|
_process_input(
|
246
281
|
span, fn, args, kwargs, ignore_input, ignore_inputs, input_formatter
|
@@ -255,6 +290,7 @@ def async_observe_base(
|
|
255
290
|
finally:
|
256
291
|
# Always restore global context
|
257
292
|
context_api.detach(ctx_token)
|
293
|
+
detach_context(isolated_ctx_token)
|
258
294
|
|
259
295
|
# span will be ended in the generator
|
260
296
|
if isinstance(res, types.AsyncGeneratorType):
|
@@ -288,4 +324,7 @@ async def _ahandle_generator(span: Span, wrapper: TracerWrapper, res: AsyncGener
|
|
288
324
|
|
289
325
|
def _process_exception(span: Span, e: Exception):
|
290
326
|
# Note that this `escaped` is sent as a StringValue("True"), not a boolean.
|
291
|
-
span.record_exception(
|
327
|
+
span.record_exception(
|
328
|
+
e, attributes=get_event_attributes_from_context(), escaped=True
|
329
|
+
)
|
330
|
+
span.set_status(Status(StatusCode.ERROR, str(e)))
|
@@ -7,6 +7,7 @@ from opentelemetry.trace import SpanKind, Status, StatusCode, Tracer
|
|
7
7
|
from lmnr.opentelemetry_lib.litellm.utils import model_as_dict, set_span_attribute
|
8
8
|
from lmnr.opentelemetry_lib.tracing import TracerWrapper
|
9
9
|
|
10
|
+
from lmnr.opentelemetry_lib.tracing.context import get_event_attributes_from_context
|
10
11
|
from lmnr.opentelemetry_lib.utils.package_check import is_package_installed
|
11
12
|
from lmnr.sdk.log import get_default_logger
|
12
13
|
|
@@ -141,10 +142,12 @@ try:
|
|
141
142
|
else:
|
142
143
|
span.set_status(Status(StatusCode.ERROR))
|
143
144
|
if isinstance(response_obj, Exception):
|
144
|
-
|
145
|
+
attributes = get_event_attributes_from_context()
|
146
|
+
span.record_exception(response_obj, attributes=attributes)
|
145
147
|
|
146
148
|
except Exception as e:
|
147
|
-
|
149
|
+
attributes = get_event_attributes_from_context()
|
150
|
+
span.record_exception(e, attributes=attributes)
|
148
151
|
logger.error(f"Error in Laminar LiteLLM instrumentation: {e}")
|
149
152
|
finally:
|
150
153
|
span.end(int(end_time.timestamp() * 1e9))
|
@@ -29,6 +29,10 @@ from .utils import (
|
|
29
29
|
shared_metrics_attributes,
|
30
30
|
should_emit_events,
|
31
31
|
)
|
32
|
+
from .streaming import (
|
33
|
+
WrappedAsyncMessageStreamManager,
|
34
|
+
WrappedMessageStreamManager,
|
35
|
+
)
|
32
36
|
from .version import __version__
|
33
37
|
|
34
38
|
from lmnr.opentelemetry_lib.tracing.context import get_current_context
|
@@ -52,6 +56,7 @@ logger = logging.getLogger(__name__)
|
|
52
56
|
|
53
57
|
_instruments = ("anthropic >= 0.3.11",)
|
54
58
|
|
59
|
+
|
55
60
|
WRAPPED_METHODS = [
|
56
61
|
{
|
57
62
|
"package": "anthropic.resources.completions",
|
@@ -71,6 +76,15 @@ WRAPPED_METHODS = [
|
|
71
76
|
"method": "stream",
|
72
77
|
"span_name": "anthropic.chat",
|
73
78
|
},
|
79
|
+
# This method is on an async resource, but is meant to be called as
|
80
|
+
# an async context manager (async with), which we don't need to await;
|
81
|
+
# thus, we wrap it with a sync wrapper
|
82
|
+
{
|
83
|
+
"package": "anthropic.resources.messages",
|
84
|
+
"object": "AsyncMessages",
|
85
|
+
"method": "stream",
|
86
|
+
"span_name": "anthropic.chat",
|
87
|
+
},
|
74
88
|
]
|
75
89
|
|
76
90
|
WRAPPED_AMETHODS = [
|
@@ -86,12 +100,6 @@ WRAPPED_AMETHODS = [
|
|
86
100
|
"method": "create",
|
87
101
|
"span_name": "anthropic.chat",
|
88
102
|
},
|
89
|
-
{
|
90
|
-
"package": "anthropic.resources.messages",
|
91
|
-
"object": "AsyncMessages",
|
92
|
-
"method": "stream",
|
93
|
-
"span_name": "anthropic.chat",
|
94
|
-
},
|
95
103
|
]
|
96
104
|
|
97
105
|
|
@@ -99,6 +107,23 @@ def is_streaming_response(response):
|
|
99
107
|
return isinstance(response, Stream) or isinstance(response, AsyncStream)
|
100
108
|
|
101
109
|
|
110
|
+
def is_stream_manager(response):
|
111
|
+
"""Check if response is a MessageStreamManager or AsyncMessageStreamManager"""
|
112
|
+
try:
|
113
|
+
from anthropic.lib.streaming._messages import (
|
114
|
+
MessageStreamManager,
|
115
|
+
AsyncMessageStreamManager,
|
116
|
+
)
|
117
|
+
|
118
|
+
return isinstance(response, (MessageStreamManager, AsyncMessageStreamManager))
|
119
|
+
except ImportError:
|
120
|
+
# Check by class name as fallback
|
121
|
+
return (
|
122
|
+
response.__class__.__name__ == "MessageStreamManager"
|
123
|
+
or response.__class__.__name__ == "AsyncMessageStreamManager"
|
124
|
+
)
|
125
|
+
|
126
|
+
|
102
127
|
@dont_throw
|
103
128
|
async def _aset_token_usage(
|
104
129
|
span,
|
@@ -437,6 +462,33 @@ def _wrap(
|
|
437
462
|
event_logger,
|
438
463
|
kwargs,
|
439
464
|
)
|
465
|
+
elif is_stream_manager(response):
|
466
|
+
if response.__class__.__name__ == "AsyncMessageStreamManager":
|
467
|
+
return WrappedAsyncMessageStreamManager(
|
468
|
+
response,
|
469
|
+
span,
|
470
|
+
instance._client,
|
471
|
+
start_time,
|
472
|
+
token_histogram,
|
473
|
+
choice_counter,
|
474
|
+
duration_histogram,
|
475
|
+
exception_counter,
|
476
|
+
event_logger,
|
477
|
+
kwargs,
|
478
|
+
)
|
479
|
+
else:
|
480
|
+
return WrappedMessageStreamManager(
|
481
|
+
response,
|
482
|
+
span,
|
483
|
+
instance._client,
|
484
|
+
start_time,
|
485
|
+
token_histogram,
|
486
|
+
choice_counter,
|
487
|
+
duration_histogram,
|
488
|
+
exception_counter,
|
489
|
+
event_logger,
|
490
|
+
kwargs,
|
491
|
+
)
|
440
492
|
elif response:
|
441
493
|
try:
|
442
494
|
metric_attributes = shared_metrics_attributes(response)
|
@@ -532,6 +584,33 @@ async def _awrap(
|
|
532
584
|
event_logger,
|
533
585
|
kwargs,
|
534
586
|
)
|
587
|
+
elif is_stream_manager(response):
|
588
|
+
if response.__class__.__name__ == "AsyncMessageStreamManager":
|
589
|
+
return WrappedAsyncMessageStreamManager(
|
590
|
+
response,
|
591
|
+
span,
|
592
|
+
instance._client,
|
593
|
+
start_time,
|
594
|
+
token_histogram,
|
595
|
+
choice_counter,
|
596
|
+
duration_histogram,
|
597
|
+
exception_counter,
|
598
|
+
event_logger,
|
599
|
+
kwargs,
|
600
|
+
)
|
601
|
+
else:
|
602
|
+
return WrappedMessageStreamManager(
|
603
|
+
response,
|
604
|
+
span,
|
605
|
+
instance._client,
|
606
|
+
start_time,
|
607
|
+
token_histogram,
|
608
|
+
choice_counter,
|
609
|
+
duration_histogram,
|
610
|
+
exception_counter,
|
611
|
+
event_logger,
|
612
|
+
kwargs,
|
613
|
+
)
|
535
614
|
elif response:
|
536
615
|
metric_attributes = shared_metrics_attributes(response)
|
537
616
|
|
@@ -113,18 +113,43 @@ async def aset_input_attributes(span, kwargs):
|
|
113
113
|
)
|
114
114
|
for i, message in enumerate(kwargs.get("messages")):
|
115
115
|
prompt_index = i + (1 if has_system_message else 0)
|
116
|
+
content = message.get("content")
|
117
|
+
tool_use_blocks = []
|
118
|
+
other_blocks = []
|
119
|
+
if isinstance(content, list):
|
120
|
+
for block in content:
|
121
|
+
if dict(block).get("type") == "tool_use":
|
122
|
+
tool_use_blocks.append(dict(block))
|
123
|
+
else:
|
124
|
+
other_blocks.append(block)
|
125
|
+
content = other_blocks
|
116
126
|
set_span_attribute(
|
117
127
|
span,
|
118
128
|
f"{SpanAttributes.LLM_PROMPTS}.{prompt_index}.content",
|
119
|
-
await _dump_content(
|
120
|
-
message_index=i, span=span, content=message.get("content")
|
121
|
-
),
|
129
|
+
await _dump_content(message_index=i, span=span, content=content),
|
122
130
|
)
|
123
131
|
set_span_attribute(
|
124
132
|
span,
|
125
133
|
f"{SpanAttributes.LLM_PROMPTS}.{prompt_index}.role",
|
126
134
|
message.get("role"),
|
127
135
|
)
|
136
|
+
if tool_use_blocks:
|
137
|
+
for tool_num, tool_use_block in enumerate(tool_use_blocks):
|
138
|
+
set_span_attribute(
|
139
|
+
span,
|
140
|
+
f"{SpanAttributes.LLM_PROMPTS}.{prompt_index}.tool_calls.{tool_num}.id",
|
141
|
+
tool_use_block.get("id"),
|
142
|
+
)
|
143
|
+
set_span_attribute(
|
144
|
+
span,
|
145
|
+
f"{SpanAttributes.LLM_PROMPTS}.{prompt_index}.tool_calls.{tool_num}.name",
|
146
|
+
tool_use_block.get("name"),
|
147
|
+
)
|
148
|
+
set_span_attribute(
|
149
|
+
span,
|
150
|
+
f"{SpanAttributes.LLM_PROMPTS}.{prompt_index}.tool_calls.{tool_num}.arguments",
|
151
|
+
json.dumps(tool_use_block.get("input")),
|
152
|
+
)
|
128
153
|
|
129
154
|
if kwargs.get("tools") is not None:
|
130
155
|
for i, tool in enumerate(kwargs.get("tools")):
|
@@ -160,8 +185,8 @@ def _set_span_completions(span, response):
|
|
160
185
|
content_block_type = content.type
|
161
186
|
# usually, Antrhopic responds with just one text block,
|
162
187
|
# but the API allows for multiple text blocks, so concatenate them
|
163
|
-
if content_block_type == "text":
|
164
|
-
text += content.text
|
188
|
+
if content_block_type == "text" and hasattr(content, "text"):
|
189
|
+
text += content.text or ""
|
165
190
|
elif content_block_type == "thinking":
|
166
191
|
content = dict(content)
|
167
192
|
# override the role to thinking
|
@@ -242,15 +267,33 @@ def set_streaming_response_attributes(span, complete_response_events):
|
|
242
267
|
if not span.is_recording() or not complete_response_events:
|
243
268
|
return
|
244
269
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
270
|
+
index = 0
|
271
|
+
for event in complete_response_events:
|
272
|
+
prefix = f"{SpanAttributes.LLM_COMPLETIONS}.{index}"
|
273
|
+
set_span_attribute(span, f"{prefix}.finish_reason", event.get("finish_reason"))
|
274
|
+
role = "thinking" if event.get("type") == "thinking" else "assistant"
|
275
|
+
# Thinking is added as a separate completion, so we need to increment the index
|
276
|
+
if event.get("type") == "thinking":
|
277
|
+
index += 1
|
278
|
+
set_span_attribute(span, f"{prefix}.role", role)
|
279
|
+
if event.get("type") == "tool_use":
|
280
|
+
set_span_attribute(
|
281
|
+
span,
|
282
|
+
f"{prefix}.tool_calls.0.id",
|
283
|
+
event.get("id"),
|
284
|
+
)
|
249
285
|
set_span_attribute(
|
250
|
-
span,
|
286
|
+
span,
|
287
|
+
f"{prefix}.tool_calls.0.name",
|
288
|
+
event.get("name"),
|
251
289
|
)
|
252
|
-
|
253
|
-
|
290
|
+
tool_arguments = event.get("input")
|
291
|
+
if tool_arguments is not None:
|
292
|
+
set_span_attribute(
|
293
|
+
span,
|
294
|
+
f"{prefix}.tool_calls.0.arguments",
|
295
|
+
# already stringified
|
296
|
+
tool_arguments,
|
297
|
+
)
|
298
|
+
else:
|
254
299
|
set_span_attribute(span, f"{prefix}.content", event.get("text"))
|
255
|
-
except Exception as e:
|
256
|
-
logger.warning("Failed to set completion attributes, error: %s", str(e))
|
@@ -40,15 +40,19 @@ def _process_response_item(item, complete_response):
|
|
40
40
|
complete_response["events"].append(
|
41
41
|
{"index": index, "text": "", "type": item.content_block.type}
|
42
42
|
)
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
if item.content_block.type == "tool_use":
|
44
|
+
complete_response["events"][index]["id"] = item.content_block.id
|
45
|
+
complete_response["events"][index]["name"] = item.content_block.name
|
46
|
+
complete_response["events"][index]["input"] = ""
|
47
|
+
|
48
|
+
elif item.type == "content_block_delta":
|
47
49
|
index = item.index
|
48
50
|
if item.delta.type == "thinking_delta":
|
49
|
-
complete_response["events"][index]["text"] += item.delta.thinking
|
51
|
+
complete_response["events"][index]["text"] += item.delta.thinking or ""
|
50
52
|
elif item.delta.type == "text_delta":
|
51
|
-
complete_response["events"][index]["text"] += item.delta.text
|
53
|
+
complete_response["events"][index]["text"] += item.delta.text or ""
|
54
|
+
elif item.delta.type == "input_json_delta":
|
55
|
+
complete_response["events"][index]["input"] += item.delta.partial_json
|
52
56
|
elif item.type == "message_delta":
|
53
57
|
for event in complete_response.get("events", []):
|
54
58
|
event["finish_reason"] = item.delta.stop_reason
|
@@ -293,3 +297,99 @@ async def abuild_from_streaming_response(
|
|
293
297
|
if span.is_recording():
|
294
298
|
span.set_status(Status(StatusCode.OK))
|
295
299
|
span.end()
|
300
|
+
|
301
|
+
|
302
|
+
class WrappedMessageStreamManager:
|
303
|
+
"""Wrapper for MessageStreamManager that handles instrumentation"""
|
304
|
+
|
305
|
+
def __init__(
|
306
|
+
self,
|
307
|
+
stream_manager,
|
308
|
+
span,
|
309
|
+
instance,
|
310
|
+
start_time,
|
311
|
+
token_histogram,
|
312
|
+
choice_counter,
|
313
|
+
duration_histogram,
|
314
|
+
exception_counter,
|
315
|
+
event_logger,
|
316
|
+
kwargs,
|
317
|
+
):
|
318
|
+
self._stream_manager = stream_manager
|
319
|
+
self._span = span
|
320
|
+
self._instance = instance
|
321
|
+
self._start_time = start_time
|
322
|
+
self._token_histogram = token_histogram
|
323
|
+
self._choice_counter = choice_counter
|
324
|
+
self._duration_histogram = duration_histogram
|
325
|
+
self._exception_counter = exception_counter
|
326
|
+
self._event_logger = event_logger
|
327
|
+
self._kwargs = kwargs
|
328
|
+
|
329
|
+
def __enter__(self):
|
330
|
+
# Call the original stream manager's __enter__ to get the actual stream
|
331
|
+
stream = self._stream_manager.__enter__()
|
332
|
+
# Return the wrapped stream
|
333
|
+
return build_from_streaming_response(
|
334
|
+
self._span,
|
335
|
+
stream,
|
336
|
+
self._instance,
|
337
|
+
self._start_time,
|
338
|
+
self._token_histogram,
|
339
|
+
self._choice_counter,
|
340
|
+
self._duration_histogram,
|
341
|
+
self._exception_counter,
|
342
|
+
self._event_logger,
|
343
|
+
self._kwargs,
|
344
|
+
)
|
345
|
+
|
346
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
347
|
+
return self._stream_manager.__exit__(exc_type, exc_val, exc_tb)
|
348
|
+
|
349
|
+
|
350
|
+
class WrappedAsyncMessageStreamManager:
|
351
|
+
"""Wrapper for AsyncMessageStreamManager that handles instrumentation"""
|
352
|
+
|
353
|
+
def __init__(
|
354
|
+
self,
|
355
|
+
stream_manager,
|
356
|
+
span,
|
357
|
+
instance,
|
358
|
+
start_time,
|
359
|
+
token_histogram,
|
360
|
+
choice_counter,
|
361
|
+
duration_histogram,
|
362
|
+
exception_counter,
|
363
|
+
event_logger,
|
364
|
+
kwargs,
|
365
|
+
):
|
366
|
+
self._stream_manager = stream_manager
|
367
|
+
self._span = span
|
368
|
+
self._instance = instance
|
369
|
+
self._start_time = start_time
|
370
|
+
self._token_histogram = token_histogram
|
371
|
+
self._choice_counter = choice_counter
|
372
|
+
self._duration_histogram = duration_histogram
|
373
|
+
self._exception_counter = exception_counter
|
374
|
+
self._event_logger = event_logger
|
375
|
+
self._kwargs = kwargs
|
376
|
+
|
377
|
+
async def __aenter__(self):
|
378
|
+
# Call the original stream manager's __aenter__ to get the actual stream
|
379
|
+
stream = await self._stream_manager.__aenter__()
|
380
|
+
# Return the wrapped stream
|
381
|
+
return abuild_from_streaming_response(
|
382
|
+
self._span,
|
383
|
+
stream,
|
384
|
+
self._instance,
|
385
|
+
self._start_time,
|
386
|
+
self._token_histogram,
|
387
|
+
self._choice_counter,
|
388
|
+
self._duration_histogram,
|
389
|
+
self._exception_counter,
|
390
|
+
self._event_logger,
|
391
|
+
self._kwargs,
|
392
|
+
)
|
393
|
+
|
394
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
395
|
+
return await self._stream_manager.__aexit__(exc_type, exc_val, exc_tb)
|
@@ -8,7 +8,10 @@ from typing import AsyncGenerator, Callable, Collection, Generator
|
|
8
8
|
|
9
9
|
from google.genai import types
|
10
10
|
|
11
|
-
from lmnr.opentelemetry_lib.tracing.context import
|
11
|
+
from lmnr.opentelemetry_lib.tracing.context import (
|
12
|
+
get_current_context,
|
13
|
+
get_event_attributes_from_context,
|
14
|
+
)
|
12
15
|
|
13
16
|
from .config import (
|
14
17
|
Config,
|
@@ -491,8 +494,9 @@ def _wrap(tracer: Tracer, to_wrap, wrapped, instance, args, kwargs):
|
|
491
494
|
span.end()
|
492
495
|
return response
|
493
496
|
except Exception as e:
|
497
|
+
attributes = get_event_attributes_from_context()
|
494
498
|
span.set_attribute(ERROR_TYPE, e.__class__.__name__)
|
495
|
-
span.record_exception(e)
|
499
|
+
span.record_exception(e, attributes=attributes)
|
496
500
|
span.set_status(Status(StatusCode.ERROR, str(e)))
|
497
501
|
span.end()
|
498
502
|
raise e
|
@@ -529,8 +533,9 @@ async def _awrap(tracer: Tracer, to_wrap, wrapped, instance, args, kwargs):
|
|
529
533
|
span.end()
|
530
534
|
return response
|
531
535
|
except Exception as e:
|
536
|
+
attributes = get_event_attributes_from_context()
|
532
537
|
span.set_attribute(ERROR_TYPE, e.__class__.__name__)
|
533
|
-
span.record_exception(e)
|
538
|
+
span.record_exception(e, attributes=attributes)
|
534
539
|
span.set_status(Status(StatusCode.ERROR, str(e)))
|
535
540
|
span.end()
|
536
541
|
raise e
|
@@ -395,6 +395,12 @@ def get_token_count_from_string(string: str, model_name: str):
|
|
395
395
|
f"Failed to get tiktoken encoding for model_name {model_name}, error: {str(ex)}"
|
396
396
|
)
|
397
397
|
return None
|
398
|
+
except Exception as ex:
|
399
|
+
# Other exceptions in tiktoken
|
400
|
+
logger.warning(
|
401
|
+
f"Failed to get tiktoken encoding for model_name {model_name}, error: {str(ex)}"
|
402
|
+
)
|
403
|
+
return None
|
398
404
|
|
399
405
|
tiktoken_encodings[model_name] = encoding
|
400
406
|
else:
|