Skip to content

Commit 2274e0a

Browse files
committed
lcc add root_span_id
1 parent ef642f2 commit 2274e0a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

cozeloop/integration/langchain/trace_callback.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def __init__(
7676
self.tags_fn = tags_fn
7777
self._tags = tags if tags else {}
7878
self.trace_id: Optional[str] = None
79+
self.root_span_id: Optional[str] = None
7980
self._id_lock = threading.Lock()
8081
self._child_of = child_of
8182
self._state_span_ctx_key = state_span_ctx_key
@@ -357,11 +358,14 @@ def _new_flow_span(self, node_name: str, span_type: str, **kwargs: Any) -> Span:
357358
span_name = node_name
358359
# set parent span
359360
parent_span: Span = None
361+
is_root_span = False
360362
if 'parent_run_id' in kwargs and kwargs['parent_run_id'] is not None and str(kwargs['parent_run_id']) in self.run_map:
361363
parent_span = self.run_map[str(kwargs['parent_run_id'])].span
362364
# only root span use child_of
363-
if parent_span is None and self._child_of:
364-
parent_span = self._child_of
365+
if parent_span is None:
366+
is_root_span = True
367+
if self._child_of:
368+
parent_span = self._child_of
365369
# modify name
366370
error_tag = {}
367371
try:
@@ -373,6 +377,11 @@ def _new_flow_span(self, node_name: str, span_type: str, **kwargs: Any) -> Span:
373377
error_tag = {'error_info': f'name_fn error {repr(e)}', 'error_trace': traceback.format_exc()}
374378
# new span
375379
flow_span = self._client.start_span(span_name, span_type, child_of=parent_span)
380+
if is_root_span:
381+
if hasattr(flow_span, 'context'):
382+
self.root_span_id = flow_span.context.span_id
383+
else:
384+
self.trace_id = flow_span.span_id
376385
run_id = str(kwargs['run_id'])
377386
self.run_map[run_id] = Run(run_id, flow_span, span_type)
378387
# set runtime

0 commit comments

Comments
 (0)