Skip to content

Commit c5173fe

Browse files
committed
adding relationship assertion
1 parent 4dbd9ca commit c5173fe

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def _ns_to_time(nanoseconds):
7373

7474

7575
def _child_to_tree(child: Tree, span: ReadableSpan):
76+
print(span.name)
7677
child.add(
7778
Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}")
7879
)
@@ -152,15 +153,13 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
152153
if not spans:
153154
return SpanExportResult.SUCCESS
154155

155-
for tree in self.spans_to_tree(spans):
156+
for tree in self.spans_to_tree(spans).values():
156157
self.console.print(tree)
157158

158159
return SpanExportResult.SUCCESS
159160

160161
@staticmethod
161-
def spans_to_tree(
162-
self, spans: typing.Sequence[ReadableSpan]
163-
) -> Dict[str, Tree]:
162+
def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]:
164163
trees = dict()
165164
all_parent_ids = {span.context.span_id for span in spans}
166165
parents = {}

exporter/opentelemetry-exporter-richconsole/tests/test_rich_exporter.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
from opentelemetry.exporter.richconsole import RichConsoleSpanExporter
1818
from opentelemetry.sdk import trace
1919
from opentelemetry.sdk.trace import ReadableSpan
20-
from opentelemetry.sdk.trace.export import BatchSpanProcessor
20+
from opentelemetry.sdk.trace.export import (
21+
BatchSpanProcessor,
22+
SimpleSpanProcessor,
23+
)
2124
import opentelemetry.trace
2225
from rich.tree import Tree
2326

@@ -43,10 +46,12 @@ def fixture_tracer_provider(span_processor):
4346
def test_span_exporter(tracer_provider, span_processor, capsys):
4447
tracer = tracer_provider.get_tracer(__name__)
4548
span = tracer.start_span("test_span")
49+
4650
span.set_attribute("key", "V4LuE")
4751
span.end()
4852
span_processor.force_flush()
4953
captured = capsys.readouterr()
54+
5055
assert "V4LuE" in captured.out
5156

5257

@@ -70,12 +75,36 @@ def test_multiple_traces(tracer_provider):
7075
trees = exporter.spans_to_tree((parent_2, parent_1, child_1))
7176
# asserts that we have all traces
7277
assert len(trees) == 2
73-
assert (
74-
opentelemetry.trace.format_trace_id(parent_1.context.trace_id) in trees
75-
)
78+
traceid_1 = opentelemetry.trace.format_trace_id(parent_1.context.trace_id)
79+
80+
assert traceid_1 in trees
81+
7682
assert (
7783
opentelemetry.trace.format_trace_id(parent_2.context.trace_id) in trees
7884
)
7985

8086
# asserts that we have exactly the number of spans we exported
8187
assert sum([walk_tree(tree) for tree in trees.values()]) == 3
88+
89+
# assert that the relationship is correct
90+
assert parent_1.name in trees[traceid_1].children[0].label
91+
assert any(
92+
[
93+
child_1.name in child.label
94+
for child in trees[traceid_1].children[0].children
95+
]
96+
)
97+
assert not any(
98+
[
99+
parent_1.name in child.label
100+
for child in trees[traceid_1].children[0].children
101+
]
102+
)
103+
assert not any(
104+
[
105+
parent_2.name in child.label
106+
for child in trees[traceid_1].children[0].children
107+
]
108+
)
109+
110+
assert trees[traceid_1].children

0 commit comments

Comments
 (0)