From ea8f91f092c4ec8119c63ca44cda1c536ffc1bc7 Mon Sep 17 00:00:00 2001 From: Matthias Uhlig <126894819+DRXD1000@users.noreply.github.com> Date: Tue, 6 May 2025 17:04:59 +0200 Subject: [PATCH 1/3] Clean up Span Dict at span end --- .../instrumentation/langchain/callback_handler.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py index be81fa4031..bcf90aa44a 100644 --- a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py +++ b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py @@ -327,8 +327,13 @@ def _end_span(self, span: Span, run_id: UUID) -> None: child_span = self.spans[child_id].span if child_span.end_time is None: # avoid warning on ended spans child_span.end() + if child_id in self.spans: + del self.spans[child_id] span.end() + if run_id in self.spans: + del self.spans[run_id] + def _create_span( self, run_id: UUID, From 9d050ab031b1513cca5922696a4b295f5c943a8f Mon Sep 17 00:00:00 2001 From: Matthias Uhlig <126894819+DRXD1000@users.noreply.github.com> Date: Wed, 7 May 2025 12:22:28 +0200 Subject: [PATCH 2/3] Delete Child Span after span.end() --- .../instrumentation/langchain/callback_handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py index bcf90aa44a..c12d6d1aea 100644 --- a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py +++ b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py @@ -327,8 +327,8 @@ def _end_span(self, span: Span, run_id: UUID) -> None: child_span = self.spans[child_id].span if child_span.end_time is None: # avoid warning on ended spans child_span.end() - if child_id in self.spans: - del self.spans[child_id] + if child_id in self.spans: + del self.spans[child_id] span.end() if run_id in self.spans: From b17f4090156b985437f03bca0b6d27936a9113a3 Mon Sep 17 00:00:00 2001 From: Matthias Uhlig <126894819+DRXD1000@users.noreply.github.com> Date: Wed, 11 Jun 2025 11:45:15 +0200 Subject: [PATCH 3/3] using self.spans.pop --- .../instrumentation/langchain/callback_handler.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py index 227c05b6c0..b0f0a58da1 100644 --- a/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py +++ b/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py @@ -390,12 +390,10 @@ def _end_span(self, span: Span, run_id: UUID) -> None: child_span = self.spans[child_id].span if child_span.end_time is None: # avoid warning on ended spans child_span.end() - if child_id in self.spans: - del self.spans[child_id] + self.spans.pop(child_id, None) span.end() - if run_id in self.spans: - del self.spans[run_id] + self.spans.pop(run_id, None) def _create_span( self,