Skip to content

Commit de39b0d

Browse files
author
shambhavigoel-13
committed
fix for test_langchain_with_openai_single_trace
Signed-off-by: shambhavigoel-13 <shambhavi.goyal@okahu.in>
1 parent 72c4298 commit de39b0d

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

apptrace/tests/common/custom_exporter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ def get_captured_spans(self):
2323

2424
def reset(self):
2525
self.captured_spans.clear()
26+
27+
def force_flush(self, timeout_millis: int = 30000) -> bool:
28+
"""Force flush - returns True to indicate success."""
29+
return True

apptrace/tests/integration/test_langchain_and_openai.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
1616

1717

18-
@pytest.fixture(scope="module")
18+
@pytest.fixture(scope="function")
1919
def setup():
2020
try:
2121
custom_exporter = CustomConsoleSpanExporter()
@@ -76,6 +76,12 @@ def test_langchain_with_openai(setup):
7676

7777
# Test multiple chains with OpenAI APIs in between in a single trace Verify there only one workflow and all inference spans
7878
def test_langchain_with_openai_single_trace(setup):
79+
from opentelemetry import trace
80+
from monocle_apptrace.instrumentation.common.instrumentor import get_monocle_span_processor
81+
82+
# Reset exporter to clean state before test
83+
setup.reset()
84+
7985
chain1 = setup_simple_chain()
8086
chain2 = setup_simple_chain()
8187
openai = OpenAI()
@@ -95,6 +101,16 @@ def test_langchain_with_openai_single_trace(setup):
95101
)
96102
chain2.invoke("What is an coffee?")
97103
stop_trace(token)
104+
105+
# Force flush to ensure all spans are exported
106+
span_processor = get_monocle_span_processor()
107+
if span_processor:
108+
span_processor.force_flush()
109+
110+
tracer_provider = trace.get_tracer_provider()
111+
if hasattr(tracer_provider, 'force_flush'):
112+
tracer_provider.force_flush()
113+
98114
verify_spans(
99115
expected_langchain_inference_spans=2,
100116
expected_openai_inference_spans=1,

0 commit comments

Comments
 (0)