5454
5555import datetime
5656import typing
57- from typing import Optional
57+ from typing import Dict , Optional
5858
5959from rich .console import Console
6060from rich .syntax import Syntax
@@ -76,6 +76,11 @@ def _child_to_tree(child: Tree, span: ReadableSpan):
7676 child .add (
7777 Text .from_markup (f"[bold cyan]Kind :[/bold cyan] { span .kind .name } " )
7878 )
79+ _add_status (child , span )
80+ _child_add_optional_attributes (child , span )
81+
82+
83+ def _add_status (child : Tree , span : ReadableSpan ):
7984 if not span .status .is_unset :
8085 if not span .status .is_ok :
8186 child .add (
@@ -96,6 +101,8 @@ def _child_to_tree(child: Tree, span: ReadableSpan):
96101 )
97102 )
98103
104+
105+ def _child_add_optional_attributes (child : Tree , span : ReadableSpan ):
99106 if span .events :
100107 events = child .add (
101108 label = Text .from_markup ("[bold cyan]Events :[/bold cyan] " )
@@ -122,6 +129,16 @@ def _child_to_tree(child: Tree, span: ReadableSpan):
122129 f"[bold cyan]{ attribute } :[/bold cyan] { span .attributes [attribute ]} "
123130 )
124131 )
132+ if span .resource :
133+ resources = child .add (
134+ label = Text .from_markup ("[bold cyan]Resources :[/bold cyan] " )
135+ )
136+ for resource in span .resource .attributes :
137+ resources .add (
138+ Text .from_markup (
139+ f"[bold cyan]{ resource } :[/bold cyan] { span .resource .attributes [resource ]} "
140+ )
141+ )
125142
126143
127144class RichConsoleSpanExporter (SpanExporter ):
@@ -141,35 +158,39 @@ def __init__(
141158 def export (self , spans : typing .Sequence [ReadableSpan ]) -> SpanExportResult :
142159 if not spans :
143160 return SpanExportResult .SUCCESS
144- tree = Tree (
145- label = f"Trace { opentelemetry .trace .format_trace_id (spans [0 ].context .trace_id )} "
146- )
147- parents = {}
148- for span in spans :
149- child = tree .add (
150- label = Text .from_markup (
151- f"[blue][{ _ns_to_time (span .start_time )} ][/blue] [bold]{ span .name } [/bold], span { opentelemetry .trace .format_span_id (span .context .span_id )} "
152- )
153- )
154- parents [span .context .span_id ] = child
155- _child_to_tree (child , span )
156-
157- for span in spans :
158- if span .parent and span .parent .span_id in parents :
159- child = parents [span .parent .span_id ].add (
160- label = Text .from_markup (
161- f"[blue][{ _ns_to_time (span .start_time )} ][/blue] [bold]{ span .name } [/bold], span { opentelemetry .trace .format_span_id (span .context .span_id )} "
162- )
163- )
164- else :
165- child = tree .add (
166- label = Text .from_markup (
167- f"[blue][{ _ns_to_time (span .start_time )} ][/blue] [bold]{ span .name } [/bold], span { opentelemetry .trace .format_span_id (span .context .span_id )} "
168- )
169- )
170161
171- parents [ span . context . span_id ] = child
172- _child_to_tree ( child , span )
162+ for tree in self . spans_to_tree ( spans ). values ():
163+ self . console . print ( tree )
173164
174- self .console .print (tree )
175165 return SpanExportResult .SUCCESS
166+
167+ @staticmethod
168+ def spans_to_tree (spans : typing .Sequence [ReadableSpan ]) -> Dict [str , Tree ]:
169+ trees = {}
170+ parents = {}
171+ spans = list (spans )
172+ while spans :
173+ for span in spans :
174+ if not span .parent :
175+ trace_id = opentelemetry .trace .format_trace_id (
176+ span .context .trace_id
177+ )
178+ trees [trace_id ] = Tree (label = f"Trace { trace_id } " )
179+ child = trees [trace_id ].add (
180+ label = Text .from_markup (
181+ f"[blue][{ _ns_to_time (span .start_time )} ][/blue] [bold]{ span .name } [/bold], span { opentelemetry .trace .format_span_id (span .context .span_id )} "
182+ )
183+ )
184+ parents [span .context .span_id ] = child
185+ _child_to_tree (child , span )
186+ spans .remove (span )
187+ elif span .parent and span .parent .span_id in parents :
188+ child = parents [span .parent .span_id ].add (
189+ label = Text .from_markup (
190+ f"[blue][{ _ns_to_time (span .start_time )} ][/blue] [bold]{ span .name } [/bold], span { opentelemetry .trace .format_span_id (span .context .span_id )} "
191+ )
192+ )
193+ parents [span .context .span_id ] = child
194+ _child_to_tree (child , span )
195+ spans .remove (span )
196+ return trees
0 commit comments