diff --git a/src/examples/dspy_example/math_problems_cot_parallel.py b/src/examples/dspy_example/math_problems_cot_parallel.py index 8c5fabf7..b70890b5 100644 --- a/src/examples/dspy_example/math_problems_cot_parallel.py +++ b/src/examples/dspy_example/math_problems_cot_parallel.py @@ -1,8 +1,8 @@ +import contextvars import dspy from dspy.datasets.gsm8k import GSM8K, gsm8k_metric from dspy.teleprompt import BootstrapFewShot from concurrent.futures import ThreadPoolExecutor -from opentelemetry.context import get_current, attach, detach # flake8: noqa from langtrace_python_sdk import langtrace, with_langtrace_root_span @@ -22,7 +22,8 @@ def __init__(self): self.prog = dspy.ChainOfThought("question -> answer") def forward(self, question): - return self.prog(question=question) + result = inject_additional_attributes(lambda: self.prog(question=question), {'langtrace.span.name': 'MathProblemsCotParallel'}) + return result @with_langtrace_root_span(name="parallel_example") def example(): @@ -34,21 +35,12 @@ def example(): optimized_cot = teleprompter.compile(CoT(), trainset=gsm8k_trainset) questions = [ - "What is the cosine of 0?", - "What is the tangent of 0?", + "What is the sine of 0?", + "What is the tangent of 100?", ] - current_context = get_current() - - def run_with_context(context, func, *args, **kwargs): - token = attach(context) - try: - return func(*args, **kwargs) - finally: - detach(token) - with ThreadPoolExecutor(max_workers=2) as executor: - futures = [executor.submit(run_with_context, current_context, optimized_cot, question=q) for q in questions] + futures = [executor.submit(contextvars.copy_context().run, optimized_cot, question=q) for q in questions] for future in futures: ans = future.result() diff --git a/src/langtrace_python_sdk/instrumentation/dspy/patch.py b/src/langtrace_python_sdk/instrumentation/dspy/patch.py index 181b276d..4b57fe16 100644 --- a/src/langtrace_python_sdk/instrumentation/dspy/patch.py +++ b/src/langtrace_python_sdk/instrumentation/dspy/patch.py @@ -61,8 +61,14 @@ def traced_method(wrapped, instance, args, kwargs): if config and len(config) > 0: span_attributes["dspy.optimizer.config"] = json.dumps(config) + # passed operation name + opname = operation_name + if extra_attributes is not None and "langtrace.span.name" in extra_attributes: + # append the operation name to the span name + opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}" + attributes = FrameworkSpanAttributes(**span_attributes) - with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span: + with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span: _set_input_attributes(span, kwargs, attributes) try: @@ -100,6 +106,12 @@ def traced_method(wrapped, instance, args, kwargs): **(extra_attributes if extra_attributes is not None else {}), } + # passed operation name + opname = operation_name + if extra_attributes is not None and "langtrace.span.name" in extra_attributes: + # append the operation name to the span name + opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}" + if instance.__class__.__name__: span_attributes["dspy.signature.name"] = instance.__class__.__name__ span_attributes["dspy.signature"] = str(instance) @@ -108,7 +120,7 @@ def traced_method(wrapped, instance, args, kwargs): span_attributes["dspy.signature.args"] = str(kwargs) attributes = FrameworkSpanAttributes(**span_attributes) - with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span: + with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span: _set_input_attributes(span, kwargs, attributes) try: @@ -147,6 +159,12 @@ def traced_method(wrapped, instance, args, kwargs): **(extra_attributes if extra_attributes is not None else {}), } + # passed operation name + opname = operation_name + if extra_attributes is not None and "langtrace.span.name" in extra_attributes: + # append the operation name to the span name + opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}" + if hasattr(instance, "devset"): span_attributes["dspy.evaluate.devset"] = str(getattr(instance, "devset")) if hasattr(instance, "trainset"): @@ -175,7 +193,7 @@ def traced_method(wrapped, instance, args, kwargs): span_attributes["dspy.evaluate.args"] = str(args) attributes = FrameworkSpanAttributes(**span_attributes) - with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span: + with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span: _set_input_attributes(span, kwargs, attributes) try: diff --git a/src/langtrace_python_sdk/version.py b/src/langtrace_python_sdk/version.py index ba51cedf..f394e699 100644 --- a/src/langtrace_python_sdk/version.py +++ b/src/langtrace_python_sdk/version.py @@ -1 +1 @@ -__version__ = "2.2.2" +__version__ = "2.2.3"