langwatch 0.2.17__py3-none-any.whl → 0.2.18__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.
langwatch/__version__.py
CHANGED
langwatch/telemetry/context.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import threading
|
|
2
|
-
from typing import TYPE_CHECKING, List
|
|
2
|
+
from typing import TYPE_CHECKING, Dict, List
|
|
3
3
|
import contextvars
|
|
4
4
|
import warnings
|
|
5
5
|
from opentelemetry import trace as trace_api
|
|
6
|
+
from opentelemetry import context
|
|
6
7
|
|
|
7
8
|
from langwatch.utils.initialization import ensure_setup
|
|
8
9
|
|
|
@@ -44,7 +45,19 @@ def get_current_trace(
|
|
|
44
45
|
|
|
45
46
|
trace = LangWatchTrace()
|
|
46
47
|
if start_if_none:
|
|
47
|
-
|
|
48
|
+
otel_span = trace_api.get_current_span()
|
|
49
|
+
otel_span_id = otel_span.get_span_context().span_id
|
|
50
|
+
trace.__enter__()
|
|
51
|
+
|
|
52
|
+
# Keep the previous span in the context if we are starting a new trace not at root level
|
|
53
|
+
if otel_span_id != 0:
|
|
54
|
+
ctx = trace_api.set_span_in_context(otel_span)
|
|
55
|
+
context.attach(ctx)
|
|
56
|
+
otel_span = trace_api.get_current_span()
|
|
57
|
+
|
|
58
|
+
trace.__exit__(None, None, None)
|
|
59
|
+
|
|
60
|
+
return trace
|
|
48
61
|
return trace
|
|
49
62
|
|
|
50
63
|
|
|
@@ -57,20 +70,20 @@ def get_current_span() -> "LangWatchSpan":
|
|
|
57
70
|
"""
|
|
58
71
|
ensure_setup()
|
|
59
72
|
|
|
60
|
-
# First try getting from LangWatch context
|
|
61
|
-
span = stored_langwatch_span.get(None)
|
|
62
|
-
if span is not None:
|
|
63
|
-
return span
|
|
64
|
-
|
|
65
|
-
if _is_on_child_thread() and len(main_thread_langwatch_span) > 0:
|
|
66
|
-
return main_thread_langwatch_span[-1]
|
|
67
|
-
|
|
68
|
-
# Fall back to OpenTelemetry context
|
|
69
73
|
otel_span = trace_api.get_current_span()
|
|
70
|
-
|
|
74
|
+
otel_span_id = otel_span.get_span_context().span_id
|
|
75
|
+
|
|
76
|
+
# If on a child thread and there is no parent, try to find a parent from the main thread
|
|
77
|
+
if (
|
|
78
|
+
_is_on_child_thread()
|
|
79
|
+
and len(main_thread_langwatch_span) > 0
|
|
80
|
+
and otel_span_id == 0
|
|
81
|
+
):
|
|
82
|
+
return main_thread_langwatch_span[-1]
|
|
71
83
|
|
|
72
84
|
from langwatch.telemetry.span import LangWatchSpan
|
|
73
85
|
|
|
86
|
+
trace = get_current_trace()
|
|
74
87
|
return LangWatchSpan.wrap_otel_span(otel_span, trace)
|
|
75
88
|
|
|
76
89
|
|
|
@@ -91,11 +104,8 @@ def _set_current_span(span: "LangWatchSpan"):
|
|
|
91
104
|
if not _is_on_child_thread():
|
|
92
105
|
main_thread_langwatch_span.append(span)
|
|
93
106
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
except Exception as e:
|
|
97
|
-
warnings.warn(f"Failed to set LangWatch span context: {e}")
|
|
98
|
-
return None
|
|
107
|
+
# Dummy token, just for the main thread span list
|
|
108
|
+
return "token"
|
|
99
109
|
|
|
100
110
|
|
|
101
111
|
def _reset_current_trace(token: contextvars.Token):
|
|
@@ -112,19 +122,12 @@ def _reset_current_trace(token: contextvars.Token):
|
|
|
112
122
|
warnings.warn(f"Failed to reset LangWatch trace context: {e}")
|
|
113
123
|
|
|
114
124
|
|
|
115
|
-
def _reset_current_span(
|
|
125
|
+
def _reset_current_span(_token: str):
|
|
116
126
|
global main_thread_langwatch_span
|
|
117
127
|
if not _is_on_child_thread():
|
|
118
128
|
if len(main_thread_langwatch_span) > 0:
|
|
119
129
|
main_thread_langwatch_span.pop()
|
|
120
130
|
|
|
121
|
-
try:
|
|
122
|
-
stored_langwatch_span.reset(token)
|
|
123
|
-
except Exception as e:
|
|
124
|
-
# Only warn if it's not a context error
|
|
125
|
-
if "different Context" not in str(e):
|
|
126
|
-
warnings.warn(f"Failed to reset LangWatch span context: {e}")
|
|
127
|
-
|
|
128
131
|
|
|
129
132
|
def _is_on_child_thread() -> bool:
|
|
130
133
|
return threading.current_thread() != threading.main_thread()
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
langwatch/__init__.py,sha256=OX8vN2-VFNUqRo9mJC8LUjHGMKYSRTwmzUQj_sKAVXQ,4210
|
|
2
|
-
langwatch/__version__.py,sha256=
|
|
2
|
+
langwatch/__version__.py,sha256=HHZMUDcvD8rFyW1uyw1zA9mutRi3pSZ0g7D2KwclJ6A,65
|
|
3
3
|
langwatch/attributes.py,sha256=nXdI_G85wQQCAdAcwjCiLYdEYj3wATmfgCmhlf6dVIk,3910
|
|
4
4
|
langwatch/batch_evaluation.py,sha256=piez7TYqUZPb9NlIShTuTPmSzrZqX-vm2Grz_NGXe04,16078
|
|
5
5
|
langwatch/client.py,sha256=WTNcYSik7kZ2kH-qGDnhbMTosc8e_Xhab_lZlfh5TC8,25559
|
|
@@ -396,7 +396,7 @@ langwatch/prompts/service.py,sha256=CiVuW1y8sYti05jaYSNk40miCg2Q0bA2FSC3iVoM5_o,
|
|
|
396
396
|
langwatch/prompts/types.py,sha256=0SPb-jfcndvay53PMpuHvtCrIptT-h-eo-9EHM-grl4,654
|
|
397
397
|
langwatch/prompts/decorators/prompt_service_tracing.py,sha256=ELrQ9bd3nPurbUXX2K_CN-2zWE00nqjXObs9Gkp4eVg,2309
|
|
398
398
|
langwatch/prompts/decorators/prompt_tracing.py,sha256=Q3-RM8G-PHkBjVKntSg3c8H2o0P1bpO3UbR3uRgbke0,3182
|
|
399
|
-
langwatch/telemetry/context.py,sha256=
|
|
399
|
+
langwatch/telemetry/context.py,sha256=q0hUG9PM3aifIr6ZRuuNNbsGtcAImu9Pv2XTKUp3CGc,4029
|
|
400
400
|
langwatch/telemetry/sampling.py,sha256=XDf6ZoXiwpHaHDYd_dDszSqH8_9-CHFNsGAZWOW1VYk,1327
|
|
401
401
|
langwatch/telemetry/span.py,sha256=g-RGWfQk4Q3b2TpipiHqjEV7rwmidaUHp54q51UxQ6s,32801
|
|
402
402
|
langwatch/telemetry/tracing.py,sha256=oyCAqW-9sFFRYPWy9epZVN0aNvqToRY4_PGxQAtS-dI,27622
|
|
@@ -409,6 +409,6 @@ langwatch/utils/initialization.py,sha256=ReDpEtjenCoRfc02YuIBK8KVjAwXx3N8yf9dyPg
|
|
|
409
409
|
langwatch/utils/module.py,sha256=KLBNOK3mA9gCSifCcQX_lOtU48BJQDWvFKtF6NMvwVA,688
|
|
410
410
|
langwatch/utils/transformation.py,sha256=5XUnW7Oz8Ck9EMsKeKeoDOrIw3EXpLGMk_fMSeA0Zng,7216
|
|
411
411
|
langwatch/utils/utils.py,sha256=ZCOSie4o9LdJ7odshNfCNjmgwgQ27ojc5ENqt1rXuSs,596
|
|
412
|
-
langwatch-0.2.
|
|
413
|
-
langwatch-0.2.
|
|
414
|
-
langwatch-0.2.
|
|
412
|
+
langwatch-0.2.18.dist-info/METADATA,sha256=23XS5hHgnh3_4eEXv3v6rX7Yj6uxfBdfbjm5nLnvpD4,13124
|
|
413
|
+
langwatch-0.2.18.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
414
|
+
langwatch-0.2.18.dist-info/RECORD,,
|
|
File without changes
|