54
54
55
55
import datetime
56
56
import typing
57
- from typing import Optional
57
+ from typing import Dict , Optional
58
58
59
59
from rich .console import Console
60
60
from rich .syntax import Syntax
@@ -76,6 +76,11 @@ def _child_to_tree(child: Tree, span: ReadableSpan):
76
76
child .add (
77
77
Text .from_markup (f"[bold cyan]Kind :[/bold cyan] { span .kind .name } " )
78
78
)
79
+ _add_status (child , span )
80
+ _child_add_optional_attributes (child , span )
81
+
82
+
83
+ def _add_status (child : Tree , span : ReadableSpan ):
79
84
if not span .status .is_unset :
80
85
if not span .status .is_ok :
81
86
child .add (
@@ -96,6 +101,8 @@ def _child_to_tree(child: Tree, span: ReadableSpan):
96
101
)
97
102
)
98
103
104
+
105
+ def _child_add_optional_attributes (child : Tree , span : ReadableSpan ):
99
106
if span .events :
100
107
events = child .add (
101
108
label = Text .from_markup ("[bold cyan]Events :[/bold cyan] " )
@@ -122,6 +129,16 @@ def _child_to_tree(child: Tree, span: ReadableSpan):
122
129
f"[bold cyan]{ attribute } :[/bold cyan] { span .attributes [attribute ]} "
123
130
)
124
131
)
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
+ )
125
142
126
143
127
144
class RichConsoleSpanExporter (SpanExporter ):
@@ -141,35 +158,39 @@ def __init__(
141
158
def export (self , spans : typing .Sequence [ReadableSpan ]) -> SpanExportResult :
142
159
if not spans :
143
160
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
- )
170
161
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 )
173
164
174
- self .console .print (tree )
175
165
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