Skip to content

Commit 3297cf0

Browse files
committed
removed start time from span state and moved error handler method to span manager
1 parent 34074b3 commit 3297cf0

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/callback_handler.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
from opentelemetry.semconv._incubating.attributes import (
2424
gen_ai_attributes as GenAI,
2525
)
26-
from opentelemetry.semconv.attributes import (
27-
error_attributes as ErrorAttributes,
28-
)
2926
from opentelemetry.trace import Tracer
30-
from opentelemetry.trace.status import Status, StatusCode
3127

3228

3329
class OpenTelemetryLangChainCallbackHandler(BaseCallbackHandler): # type: ignore[misc]
@@ -175,15 +171,4 @@ def on_llm_error(
175171
parent_run_id: Optional[UUID] = None,
176172
**kwargs: Any,
177173
) -> None:
178-
self._handle_error(error, run_id)
179-
180-
def _handle_error(self, error: BaseException, run_id: UUID):
181-
span = self.span_manager.get_span(run_id)
182-
if span is None:
183-
# If the span does not exist, we cannot set the error status
184-
return
185-
span.set_status(Status(StatusCode.ERROR, str(error)))
186-
span.set_attribute(
187-
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
188-
)
189-
self.span_manager.end_span(run_id)
174+
self.span_manager.handle_error(error, run_id)

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/span_manager.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
gen_ai_attributes as GenAI,
2323
)
2424
from opentelemetry.trace import Span, SpanKind, Tracer, set_span_in_context
25+
from opentelemetry.semconv.attributes import (
26+
error_attributes as ErrorAttributes,
27+
)
28+
from opentelemetry.trace.status import Status, StatusCode
2529

2630

2731
@dataclass
2832
class _SpanState:
2933
span: Span
3034
context: Context
31-
start_time: float = field(default_factory=time.time)
3235
children: List[UUID] = field(default_factory=list)
3336

3437

@@ -100,3 +103,14 @@ def end_span(self, run_id: UUID) -> None:
100103
def get_span(self, run_id: UUID) -> Optional[Span]:
101104
state = self.spans.get(run_id)
102105
return state.span if state else None
106+
107+
def handle_error(self, error: BaseException, run_id: UUID):
108+
span = self.get_span(run_id)
109+
if span is None:
110+
# If the span does not exist, we cannot set the error status
111+
return
112+
span.set_status(Status(StatusCode.ERROR, str(error)))
113+
span.set_attribute(
114+
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
115+
)
116+
self.end_span(run_id)

0 commit comments

Comments
 (0)