Skip to content

Commit c6225ea

Browse files
pmeieraabmass
andauthored
opentelemetry-exporter-richconsole: add option to suppress resource information (#3898)
* add option to suppress resource information with RichConsoleExporter * fix merge --------- Co-authored-by: Aaron Abbott <aaronabbott@google.com>
1 parent 1ee4a72 commit c6225ea

3 files changed

Lines changed: 57 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
([#4335](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4335))
2020
- Expand `AGENTS.md` with instrumentation/GenAI guidance and add PR review instructions.
2121
([#4457](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4457))
22+
- `opentelemetry-exporter-richconsole`: Add support for suppressing resource information
23+
([#3898](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3898))
2224

2325
### Fixed
2426

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ def _ns_to_time(nanoseconds):
7676
return ts.strftime("%H:%M:%S.%f")
7777

7878

79-
def _child_to_tree(child: Tree, span: ReadableSpan):
79+
def _child_to_tree(
80+
child: Tree, span: ReadableSpan, *, suppress_resource: bool
81+
):
8082
child.add(
8183
Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}")
8284
)
8385
_add_status(child, span)
84-
_child_add_optional_attributes(child, span)
86+
_child_add_optional_attributes(
87+
child, span, suppress_resource=suppress_resource
88+
)
8589

8690

8791
def _add_status(child: Tree, span: ReadableSpan):
@@ -106,7 +110,9 @@ def _add_status(child: Tree, span: ReadableSpan):
106110
)
107111

108112

109-
def _child_add_optional_attributes(child: Tree, span: ReadableSpan):
113+
def _child_add_optional_attributes(
114+
child: Tree, span: ReadableSpan, *, suppress_resource: bool
115+
):
110116
if span.events:
111117
events = child.add(
112118
label=Text.from_markup("[bold cyan]Events :[/bold cyan] ")
@@ -133,7 +139,7 @@ def _child_add_optional_attributes(child: Tree, span: ReadableSpan):
133139
f"[bold cyan]{attribute} :[/bold cyan] {span.attributes[attribute]}"
134140
)
135141
)
136-
if span.resource:
142+
if span.resource and not suppress_resource:
137143
resources = child.add(
138144
label=Text.from_markup("[bold cyan]Resources :[/bold cyan] ")
139145
)
@@ -155,21 +161,29 @@ class RichConsoleSpanExporter(SpanExporter):
155161
def __init__(
156162
self,
157163
service_name: Optional[str] = None,
164+
suppress_resource: bool = False,
158165
):
159166
self.service_name = service_name
167+
self.suppress_resource = suppress_resource
160168
self.console = Console()
161169

162170
def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
163171
if not spans:
164172
return SpanExportResult.SUCCESS
165173

166-
for tree in self.spans_to_tree(spans).values():
174+
for tree in self.spans_to_tree(
175+
spans, suppress_resource=self.suppress_resource
176+
).values():
167177
self.console.print(tree)
168178

169179
return SpanExportResult.SUCCESS
170180

171181
@staticmethod
172-
def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]:
182+
def spans_to_tree(
183+
spans: typing.Sequence[ReadableSpan],
184+
*,
185+
suppress_resource: bool = False,
186+
) -> Dict[str, Tree]:
173187
trees = {}
174188
parents = {}
175189
spans = list(spans)
@@ -189,7 +203,9 @@ def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]:
189203
)
190204
)
191205
parents[span.context.span_id] = child
192-
_child_to_tree(child, span)
206+
_child_to_tree(
207+
child, span, suppress_resource=suppress_resource
208+
)
193209
spans.remove(span)
194210
elif span.parent and span.parent.span_id in parents:
195211
child = parents[span.parent.span_id].add(
@@ -198,6 +214,8 @@ def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]:
198214
)
199215
)
200216
parents[span.context.span_id] = child
201-
_child_to_tree(child, span)
217+
_child_to_tree(
218+
child, span, suppress_resource=suppress_resource
219+
)
202220
spans.remove(span)
203221
return trees

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
# limitations under the License.
1414

1515
import pytest
16+
from rich.text import Text
1617
from rich.tree import Tree
1718

1819
import opentelemetry.trace
1920
from opentelemetry.exporter.richconsole import RichConsoleSpanExporter
2021
from opentelemetry.sdk import trace
22+
from opentelemetry.sdk.resources import Resource
2123
from opentelemetry.sdk.trace.export import BatchSpanProcessor
2224

2325

@@ -108,3 +110,30 @@ def test_no_deadlock(tracer_provider):
108110
pass
109111

110112
RichConsoleSpanExporter.spans_to_tree((child,))
113+
114+
115+
def test_suppress_resource(span_processor):
116+
attributes = {"resource.key": "resource.value"}
117+
resource = Resource(attributes)
118+
tracer_provider = trace.TracerProvider(resource=resource)
119+
tracer_provider.add_span_processor(span_processor)
120+
tracer = tracer_provider.get_tracer(__name__)
121+
122+
with tracer.start_as_current_span("parent") as parent:
123+
with tracer.start_as_current_span("child") as child:
124+
pass
125+
126+
trees = RichConsoleSpanExporter.spans_to_tree(
127+
(parent, child), suppress_resource=True
128+
)
129+
assert len(trees) == 1
130+
131+
nodes = [next(t for t in trees.values())]
132+
for node in nodes:
133+
label = node.label
134+
if isinstance(label, Text):
135+
label = label.plain
136+
137+
assert "resource" not in label.lower()
138+
139+
nodes.extend(node.children)

0 commit comments

Comments
 (0)