Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/uipath/tracing/_otel_exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,10 @@ def __init__(self, file_path: str):

def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
try:
uipath_spans = [
_SpanUtils.otel_span_to_uipath_span(
span, serialize_attributes=True
).to_dict(serialize_attributes=True)
for span in spans
]

with open(self.file_path, "a") as f:
for span in uipath_spans:
f.write(json.dumps(span) + "\n")
for span in spans:
span_dict = _SpanUtils.readable_span_to_dict(span)
f.write(json.dumps(span_dict) + "\n")
return SpanExportResult.SUCCESS
except Exception as e:
logger.error(f"Failed to export spans to {self.file_path}: {e}")
Expand Down
50 changes: 50 additions & 0 deletions src/uipath/tracing/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,56 @@ def _has_ancestor_with_name(

return False

@staticmethod
def readable_span_to_dict(span: ReadableSpan) -> Dict[str, Any]:
"""Convert a ReadableSpan to a dictionary for direct serialization.

This method preserves the raw OpenTelemetry span structure without
UiPath-specific transformations.

Args:
span: The OpenTelemetry ReadableSpan to convert

Returns:
A dictionary representation of the span
"""
return {
"name": span.name,
"context": {
"trace_id": format(span.context.trace_id, "032x"),
"span_id": format(span.context.span_id, "016x"),
"trace_state": dict(span.context.trace_state) if span.context.trace_state else {},
},
"kind": str(span.kind),
"parent_id": format(span.parent.span_id, "016x") if span.parent else None,
"start_time": span.start_time,
"end_time": span.end_time,
"status": {
"status_code": str(span.status.status_code),
"description": span.status.description,
},
"attributes": dict(span.attributes) if span.attributes else {},
"events": [
{
"name": event.name,
"timestamp": event.timestamp,
"attributes": dict(event.attributes) if event.attributes else {},
}
for event in span.events
],
"links": [
{
"context": {
"trace_id": format(link.context.trace_id, "032x"),
"span_id": format(link.context.span_id, "016x"),
},
"attributes": dict(link.attributes) if link.attributes else {},
}
for link in span.links
],
"resource": dict(span.resource.attributes) if span.resource else {},
}

@staticmethod
def spans_to_llm_context(spans: list[ReadableSpan]) -> str:
"""Convert spans to a formatted conversation history string suitable for LLM context.
Expand Down
Loading