Skip to content

Commit 8b41bf9

Browse files
Allow DSPy span naming (#249)
* Update example * Allow span naming using langtrace.span.name * Bump version
1 parent e6d8542 commit 8b41bf9

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/examples/dspy_example/math_problems_cot_parallel.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import contextvars
12
import dspy
23
from dspy.datasets.gsm8k import GSM8K, gsm8k_metric
34
from dspy.teleprompt import BootstrapFewShot
45
from concurrent.futures import ThreadPoolExecutor
5-
from opentelemetry.context import get_current, attach, detach
66

77
# flake8: noqa
88
from langtrace_python_sdk import langtrace, with_langtrace_root_span
@@ -22,7 +22,8 @@ def __init__(self):
2222
self.prog = dspy.ChainOfThought("question -> answer")
2323

2424
def forward(self, question):
25-
return self.prog(question=question)
25+
result = inject_additional_attributes(lambda: self.prog(question=question), {'langtrace.span.name': 'MathProblemsCotParallel'})
26+
return result
2627

2728
@with_langtrace_root_span(name="parallel_example")
2829
def example():
@@ -34,21 +35,12 @@ def example():
3435
optimized_cot = teleprompter.compile(CoT(), trainset=gsm8k_trainset)
3536

3637
questions = [
37-
"What is the cosine of 0?",
38-
"What is the tangent of 0?",
38+
"What is the sine of 0?",
39+
"What is the tangent of 100?",
3940
]
4041

41-
current_context = get_current()
42-
43-
def run_with_context(context, func, *args, **kwargs):
44-
token = attach(context)
45-
try:
46-
return func(*args, **kwargs)
47-
finally:
48-
detach(token)
49-
5042
with ThreadPoolExecutor(max_workers=2) as executor:
51-
futures = [executor.submit(run_with_context, current_context, optimized_cot, question=q) for q in questions]
43+
futures = [executor.submit(contextvars.copy_context().run, optimized_cot, question=q) for q in questions]
5244

5345
for future in futures:
5446
ans = future.result()

src/langtrace_python_sdk/instrumentation/dspy/patch.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ def traced_method(wrapped, instance, args, kwargs):
6161
if config and len(config) > 0:
6262
span_attributes["dspy.optimizer.config"] = json.dumps(config)
6363

64+
# passed operation name
65+
opname = operation_name
66+
if extra_attributes is not None and "langtrace.span.name" in extra_attributes:
67+
# append the operation name to the span name
68+
opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}"
69+
6470
attributes = FrameworkSpanAttributes(**span_attributes)
65-
with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
71+
with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span:
6672
_set_input_attributes(span, kwargs, attributes)
6773

6874
try:
@@ -100,6 +106,12 @@ def traced_method(wrapped, instance, args, kwargs):
100106
**(extra_attributes if extra_attributes is not None else {}),
101107
}
102108

109+
# passed operation name
110+
opname = operation_name
111+
if extra_attributes is not None and "langtrace.span.name" in extra_attributes:
112+
# append the operation name to the span name
113+
opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}"
114+
103115
if instance.__class__.__name__:
104116
span_attributes["dspy.signature.name"] = instance.__class__.__name__
105117
span_attributes["dspy.signature"] = str(instance)
@@ -108,7 +120,7 @@ def traced_method(wrapped, instance, args, kwargs):
108120
span_attributes["dspy.signature.args"] = str(kwargs)
109121

110122
attributes = FrameworkSpanAttributes(**span_attributes)
111-
with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
123+
with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span:
112124
_set_input_attributes(span, kwargs, attributes)
113125

114126
try:
@@ -147,6 +159,12 @@ def traced_method(wrapped, instance, args, kwargs):
147159
**(extra_attributes if extra_attributes is not None else {}),
148160
}
149161

162+
# passed operation name
163+
opname = operation_name
164+
if extra_attributes is not None and "langtrace.span.name" in extra_attributes:
165+
# append the operation name to the span name
166+
opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}"
167+
150168
if hasattr(instance, "devset"):
151169
span_attributes["dspy.evaluate.devset"] = str(getattr(instance, "devset"))
152170
if hasattr(instance, "trainset"):
@@ -175,7 +193,7 @@ def traced_method(wrapped, instance, args, kwargs):
175193
span_attributes["dspy.evaluate.args"] = str(args)
176194

177195
attributes = FrameworkSpanAttributes(**span_attributes)
178-
with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
196+
with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span:
179197
_set_input_attributes(span, kwargs, attributes)
180198

181199
try:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.2.2"
1+
__version__ = "2.2.3"

0 commit comments

Comments
 (0)