diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e9b87fc18..5e76c95a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3885](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3885)) - `opentelemetry-instrumentation-django`: improve readthedocs for sqlcommenter configuration. ([#3884](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3884)) +- `opentelemetry-exporter-richconsole`: Add support for suppressing resource information + ([#3898](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3898)) ### Fixed diff --git a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py index 3e3aab7c74..b1165ea043 100644 --- a/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py +++ b/exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py @@ -76,12 +76,16 @@ def _ns_to_time(nanoseconds): return ts.strftime("%H:%M:%S.%f") -def _child_to_tree(child: Tree, span: ReadableSpan): +def _child_to_tree( + child: Tree, span: ReadableSpan, *, suppress_resource: bool +): child.add( Text.from_markup(f"[bold cyan]Kind :[/bold cyan] {span.kind.name}") ) _add_status(child, span) - _child_add_optional_attributes(child, span) + _child_add_optional_attributes( + child, span, suppress_resource=suppress_resource + ) def _add_status(child: Tree, span: ReadableSpan): @@ -106,7 +110,9 @@ def _add_status(child: Tree, span: ReadableSpan): ) -def _child_add_optional_attributes(child: Tree, span: ReadableSpan): +def _child_add_optional_attributes( + child: Tree, span: ReadableSpan, *, suppress_resource: bool +): if span.events: events = child.add( label=Text.from_markup("[bold cyan]Events :[/bold cyan] ") @@ -133,7 +139,7 @@ def _child_add_optional_attributes(child: Tree, span: ReadableSpan): f"[bold cyan]{attribute} :[/bold cyan] {span.attributes[attribute]}" ) ) - if span.resource: + if span.resource and not suppress_resource: resources = child.add( label=Text.from_markup("[bold cyan]Resources :[/bold cyan] ") ) @@ -155,21 +161,29 @@ class RichConsoleSpanExporter(SpanExporter): def __init__( self, service_name: Optional[str] = None, + suppress_resource: bool = False, ): self.service_name = service_name + self.suppress_resource = suppress_resource self.console = Console() def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult: if not spans: return SpanExportResult.SUCCESS - for tree in self.spans_to_tree(spans).values(): + for tree in self.spans_to_tree( + spans, suppress_resource=self.suppress_resource + ).values(): self.console.print(tree) return SpanExportResult.SUCCESS @staticmethod - def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]: + def spans_to_tree( + spans: typing.Sequence[ReadableSpan], + *, + suppress_resource: bool = False, + ) -> Dict[str, Tree]: trees = {} parents = {} spans = list(spans) @@ -189,7 +203,9 @@ def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]: ) ) parents[span.context.span_id] = child - _child_to_tree(child, span) + _child_to_tree( + child, span, suppress_resource=suppress_resource + ) spans.remove(span) elif span.parent and span.parent.span_id in parents: child = parents[span.parent.span_id].add( @@ -198,6 +214,8 @@ def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]: ) ) parents[span.context.span_id] = child - _child_to_tree(child, span) + _child_to_tree( + child, span, suppress_resource=suppress_resource + ) spans.remove(span) return trees diff --git a/exporter/opentelemetry-exporter-richconsole/tests/test_rich_exporter.py b/exporter/opentelemetry-exporter-richconsole/tests/test_rich_exporter.py index 44bebb4839..bc204e6959 100644 --- a/exporter/opentelemetry-exporter-richconsole/tests/test_rich_exporter.py +++ b/exporter/opentelemetry-exporter-richconsole/tests/test_rich_exporter.py @@ -13,11 +13,13 @@ # limitations under the License. import pytest +from rich.text import Text from rich.tree import Tree import opentelemetry.trace from opentelemetry.exporter.richconsole import RichConsoleSpanExporter from opentelemetry.sdk import trace +from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace.export import BatchSpanProcessor @@ -108,3 +110,30 @@ def test_no_deadlock(tracer_provider): pass RichConsoleSpanExporter.spans_to_tree((child,)) + + +def test_suppress_resource(span_processor): + attributes = {"resource.key": "resource.value"} + resource = Resource(attributes) + tracer_provider = trace.TracerProvider(resource=resource) + tracer_provider.add_span_processor(span_processor) + tracer = tracer_provider.get_tracer(__name__) + + with tracer.start_as_current_span("parent") as parent: + with tracer.start_as_current_span("child") as child: + pass + + trees = RichConsoleSpanExporter.spans_to_tree( + (parent, child), suppress_resource=True + ) + assert len(trees) == 1 + + nodes = [next(t for t in trees.values())] + for node in nodes: + label = node.label + if isinstance(label, Text): + label = label.plain + + assert "resource" not in label.lower() + + nodes.extend(node.children)